Valheim – Dedicated Linux Server

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!

How to Host Your Own Server (Rental Service Provider)

You can host a server at game server hosting provider:

Note: Every gaming server goes online immediately! A welcome email including your connection information (guide) will be sent to you when your gaming server is created.

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.

Volodymyr Azimoff
About Volodymyr Azimoff 13600 Articles
I love games and I live games. Video games are my passion, my hobby and my job. My experience with games started back in 1994 with the Metal Mutant game on ZX Spectrum computer. And since then, I’ve been playing on anything from consoles, to mobile devices. My first official job in the game industry started back in 2005, and I'm still doing what I love to do.

15 Comments

  1. I can’t get the valheim.service to go right: am I missing something? Running the script myself manually works, but I can’t get it to run via the service.

    ● valheim.service – Valheim service
    Loaded: loaded (/etc/systemd/system/valheim.service; disabled; vendor preset: enabled)
    Active: activating (auto-restart) (Result: exit-code) since Wed 2021-11-10 16:39:56 CET; 3s ago
    Process: 16731 ExecStart=/usr/bin/sh /home/speijker/.steam/steamapps/common/Valheim dedicated server/start_speijker>
    Main PID: 16731 (code=exited, status=200/CHDIR)

    Nov 10 16:39:56 speijkernet systemd[1]: valheim.service: Main process exited, code=exited, status=200/CHDIR
    Nov 10 16:39:56 speijkernet systemd[1]: valheim.service: Failed with result ‘exit-code’.

  2. As a neophyte to Linux, I had a fun time following along, and learning this, had it set up and running great inside a day. I’m very curious though, how I can take a peek at the server console?

  3. installation worked great so far, process is running… How can I transfer my world from my PC to the server? (i.e. which directory are the two files to be put in?)
    Also, any ide what this error comes up? (seems to be the update?)
    Mär 09 14:43:23 h1884536.stratoserver.net start_valheim.sh[2715]: Waiting for user info…OK
    Mär 09 14:43:23 h1884536.stratoserver.net start_valheim.sh[2715]: ERROR! Failed to install app ‘896660’ (Disk write failure)
    Mär 09 14:43:23 h1884536.stratoserver.net start_valheim.sh[2715]: Server started

    all files within /home/steam belong to the user

    Edit: ok, copied the world successfully (for anyone searching, it’s in /home/steam/.config/unity3d/IronGate/Valheim/worlds), and that write error also is gone

  4. The cronjob didn’t work for me so made some changes, and I wanted data/time in a log.

    sudo apt install moreutils
    To add ts

    user@spelserver-ubuntu:~$ sudo -i
    root@spelserver-ubuntu:~# crontab -l

    0 3 * * * /usr/bin/apt update -q -y | ts >> /var/log/apt/automaticupdates.log
    1 3 * * * /usr/bin/apt upgrade -q -y | ts >> /var/log/apt/automaticupdates.log
    30 3 * * * /sbin/shutdown -r now
    30 4 * * * /usr/bin/apt apt autoremove -q -y | ts >> /var/log/apt/automaticupdates.log
    0 5 * * * /usr/bin/apt apt clean -q -y | ts >> /var/log/apt/automaticupdates.log

    (Dirty but works)

  5. I would like to use screen on my script, but everytime I simply add screen before the valheimserver binary, just no process runs afterwards. Any toughts on that?

  6. Followed along exactly (as far as I can tell) my friends logged in built some stuff and then when they came back everything they did was gone. any idea what’s causing that?

  7. Cant get it working. Recieve: [S_API FAIL] Tried to access Steam interface SteamNetworkingUtils003 before SteamAPI_Init succeeded.
    ?

    • Feb 27 21:47:43 spelserver-ubuntu systemd[1]: Started Valheim service.
      Feb 27 21:47:45 spelserver-ubuntu start_valheim.sh[7971]: Redirecting stderr to '/home/valheim/.steam/logs/stderr.txt'
      Feb 27 21:47:45 spelserver-ubuntu start_valheim.sh[7971]: [ 0%] Checking for available updates...
      Feb 27 21:47:45 spelserver-ubuntu start_valheim.sh[7971]: [----] Verifying installation...
      Feb 27 21:47:45 spelserver-ubuntu start_valheim.sh[7971]: Steam Console Client (c) Valve Corporation
      Feb 27 21:47:45 spelserver-ubuntu start_valheim.sh[7971]: -- type 'quit' to exit --
      Feb 27 21:47:45 spelserver-ubuntu start_valheim.sh[7971]: Loading Steam API...OK.
      Feb 27 21:47:45 spelserver-ubuntu start_valheim.sh[7971]: Connecting anonymously to Steam Public...Logged in OK
      Feb 27 21:47:48 spelserver-ubuntu start_valheim.sh[7971]: Waiting for user info...OK
      Feb 27 21:47:48 spelserver-ubuntu start_valheim.sh[7971]: Success! App '896660' already up to date.
      Feb 27 21:47:48 spelserver-ubuntu start_valheim.sh[7955]: Server started
      Feb 27 21:47:48 spelserver-ubuntu start_valheim.sh[7955]: valheim.service: timestamp 2021-02-27 21:47:48
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: [S_API] SteamAPI_Init(): Loaded local 'steamclient.so' OK.
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: CAppInfoCacheReadFromDiskThread took 2 milliseconds to initialize
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: CApplicationManagerPopulateThread took 0 milliseconds to initialize (will have waited on CAppInfoCacheReadFromDiskThread)
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamGameServer013 /
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamUtils009 /
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: Setting breakpad minidump AppID = 892970
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamGameServer013 / GameServer
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamUtils009 / Utils
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamNetworking006 / Networking
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamGameServerStats001 / GameServerStats
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): STEAMHTTP_INTERFACE_VERSION003 / HTTP
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): STEAMINVENTORY_INTERFACE_V003 / Inventory
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): STEAMUGC_INTERFACE_VERSION014 / UGC
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): STEAMAPPS_INTERFACE_VERSION008 / Apps
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: [S_API FAIL] Tried to access Steam interface SteamNetworkingUtils003 before SteamAPI_Init succeeded.
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamNetworkingUtils003 /
      Feb 27 21:48:05 spelserver-ubuntu start_valheim.sh[7992]: RecordSteamInterfaceCreation (PID 7992): SteamNetworkingSockets008 /

  8. Also add “KillSignal=SIGINT” to [Service] section of valheim.service to gracefuly end server on stop/restart (to save world before restart).

    • There is more to it than just adding this line. Instead, I did the following:

      Create a stop_server.sh in the same place as start_server.sh. Remember to chmod +x the file after saving it. Code is as follows:

      #!/bin/bash
      echo "Stopping server."
      echo 1 > server_exit.drp

      In the valheim.service file add these lines undernearth ExecStart in the [Service] section:

      ExecStop=/home/username/Valheim/stop_server.sh
      KillSignal=SIGINT

      Make sure the script is executable and that the path definitely points to the script. Remember to systemctl daemon-reload after editing the service file.

      Stop the server. Start it, wait a moment then stop it again. Check the systemctl status to make sure everything is working.

  9. If you’re using a distro utilising SELinux you will get an error 203/EXEC when you try to start the service eventho’ the script would work as a standalone. Because I have no idea how SELinux works my only tip is to run ‘setenforce 0’. This might not be a good idea from security point of view so do at your own risk. The better solution would be to allow the new service to execute.

  10. I don’t see my server on the ‘Join Server’ menu, but I can access it directly through Steam -> View -> Servers. Any thoughts on this? I configured a custom name for both the -name and -world parameter, neither of which show up.

Leave a Reply

Your email address will not be published.


*