A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #

Valheim - Dedicated Linux Server

Written by Griffen8280   /   Feb 8, 2021    


This is a guide to setting up a linux based Valheim dedicated server with a systemd service that can update/start the server and report on it's status within the operating system.

You can also click the link below to order your own game server!


Guide to Dedicated Linux Server



Assumptions



It is assumed you have at least a working knowledge of Linux and can navigate around the operating system somewhat proficiently to complete this. Also I prefer to use the nano text editor on my system however if you use something else that is fine. Finally you will need to have steamcmd installed on your system in order for many of the things in this guide to work so ensure you have completed the steps outlined on the valve developer site to get it setup.


Main Install of the Server



Warning: Make sure you replace all instances of "username" with your own in the code blocks!

After you have steamcmd setup on your machine ensure you are back in the main directory of your account, this can be accomplished by typing:

cd ~/

Now create a folder at this location, I called mine Valheim (original right!)

mkdir Valheim

We are now ready to install the server files, using steamcmd by calling the whole thing from the command line with the following:

/home/username/.steam/steamcmd/steamcmd.sh +login anonymous +force_install_dir
/home/username/Valheim +app_update 896660 validate +exit


This will install the server files into the Valheim directory we created earlier.

Modify the Server Start Script



Make sure you are in the Valheim directory and can see the files by executing the following:

cd Valheim
ls


Something like this file list will appear...



Now we get into the fun stuff, you need to make a copy of the server_start.sh file in the Valheim directory so that when steamcmd updates the server at some point your own changes are not overwritten and your service will still work. To do this execute the following on the command line:

cp start_server.sh start_valheim.sh

This will make a copy of the file and rename it start_valheim.sh, once that is done then we need to open the file and make a few edits to it.

nano start_valheim.sh

Once it is open copy the following block of code and paste it into the nano window using whatever method is convenient for you, or type it all in if you prefer. Note, this text should replace what is in the file. Or you could go line by line and just update your file with the stuff from below that is missing from yours.

#!/bin/bash

export templdpath=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=./linux64:$LD_LIBRARY_PATH
export SteamAppId=892970

# Tip: Make a local copy of this script to avoid it being overwritten by steam.
# NOTE: Minimum password length is 5 characters & Password cant be in the server name.
# NOTE: You need to make sure the ports 2456-2458 is being forwarded to your server through your local router & firewall.
/home/username/.steam/steamcmd/steamcmd.sh +login anonymous +force_install_dir /home/username/Valheim +app_update 896660 +quit

./valheim_server.x86_64 -name "Your Server Name" -port 2456 -world "Dedicated" -password "Your Password" -public 1 > /dev/null &

export LD_LIBRARY_PATH=$templdpath

echo "Server started"
echo ""
#read -p "Press RETURN to stop server"
#echo 1 > server_exit.drp

#echo "Server exit signal set"
#echo "You can now close this terminal"

while :
do
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
echo "valheim.service: timestamp ${TIMESTAMP}"
sleep 60
done


Again be sure to change any instance of "username" you find in the directory calls with your own username and change the server name and password to your own. When you are done with the file from nano you type in ctl+x and save it.

A few things to note about the changes in this file are the while statement at the bottom. This is for the systemd journalctl service so that a running timestamp of how long the server is up can be written to the log. Also at the start of the script after the environment variables are set for the server to run correctly I am calling the steamcmd script to ensure that the server is the current version by running an update action. This makes it easy for you to create a cron to restart your server daily and when the service is started it will automatically check if there is an update for the server available from steam. Finally we comment out a few things at the bottom (echo statements) because they will not be needed to stop the server.

Now we will need to create and register the service that will get this file running.

Create and Register the Service



Now we need to make a service file and do some editing to it in order to start this server in a nice way with the operating system.

nano valheim.service

That will create a file called "valheim.service" and open it automatically in the nano editor. Now paste the following contents into it...

[Unit]
Description=Valheim service
Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
Restart=on-failure
RestartSec=10
User=username
WorkingDirectory=/home/username/Valheim
ExecStart=/home/username/Valheim/start_valheim.sh

[Install]
WantedBy=multi-user.target


What this is doing is creating the variables needed by the systemd daemon in order to start the server and manage it for you. In this we are telling the server not to attempt to start the service until after the network is online and logging has been started. In the [Service] section we are telling the system what type of service it is and what to do if it fails or crashes. Then we are telling it where the run script can be found/executed and what directory to work out of. Finally, since you never want to run a server (typically) as a root user we are defining the username to run the service as which should be your limited account. Again make sure you replace any instance of "username" you find with your own or this will not work as expected. Finally we need to copy this script into the proper directory and register it with the systemd daemon in order for it to be able to run.

sudo cp valheim.service /etc/systemd/system

Sudo is needed because you are copying this file to a protected system folder. Now register it with the systemd daemon

sudo systemctl daemon-reload

And finally we can start the service and see if everything is functional

sudo systemctl start valheim

Wait 10-15 seconds then

sudo systemctl status valheim

Once you execute all that you should see something similar to the following screenshot.



Of course you will see a few things after the name and password parts of the text, I blacked those out to protect my server :).

Finally if you want to stop the server for any reason you can do the following

sudo systemctl stop valheim

Lastly if you want this to start auto-magically with the operating system just run the following

sudo systemctl enable valheim.service

Wrap Up



After you see that pretty green text that says active (running) that is it, your Valheim dedicated server is online. Finally you will need to forward the appropriate ports to your server which is outside the scope of this guide and when you log into the game you should be able to find your server in the server browser list and connect to it. Happy playing with your friends in a persistent dedicated world!

Bonus: On my server I have set it up to check for OS updates and install them nightly then reboot the server, I have accomplished this with cron jobs and the following code will outline how to do that if you want a hands off type of install.

sudo crontab -e

This will open the system cron, then at the bottom of the file insert the following information.

0 2 * * * apt update && apt upgrade -y
30 2 * * * reboot
30 3 * * * apt autoremove && apt clean


This will do the following on your server daily...

  • Line 1 - Updates the repos on the system and then upgrades available packages at 2am
  • Line 2 - Reboots the machine at 2:30am after the update
  • Line 3 - Cleans up the apt system and removes deprecated packages at 3:30am

This should keep your system updated and clean as well as restart the Valheim server on a nightly basis and check for updates for it.

Written by Griffen8280.


Game:   Valheim