Valheim

Valheim

61 ratings
Valheim Dedicated Linux Server
By Griffen8280
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.
5
7
   
Award
Favorite
Favorited
Unfavorite
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.

https://developer.valvesoftware.com/wiki/SteamCMD

Please ensure you have followed the wiki guide, you will need the 32 bit packages installed (covered by the guide) for this to work correctly for all server systems.
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/${USER}/.steam/steamcmd/steamcmd.sh +login anonymous +force_install_dir /home/${USER}/Valheim +app_update 896660 validate +exit
-Provided by Kirbo, the ${USER} will automatically supply the username

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

Note:
If you get the "No subscription" error when attempting to pull the files, then try to login using your normal steam credentials first. This will cache your account and then all calls using anonymous should work after that.
- Thank you Tander for this tip!
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/$USER/.steam/steamcmd/steamcmd.sh +force_install_dir /home/$USER/Valheim +login anonymous +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

Be sure to 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.

Credit:
Moved the force directory command per suggestion from Renegrade.
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 KillSignal=SIGINT [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.

*NOTE*
In some cases, like your default interpreter is set to something other than bash/sh, you may need to specify the bash environment to start the script properly. You would need to change the line:
ExecStart=/home/username/Valheim/start_valheim.sh
to read like this:
ExecStart=/bin/bash /home/username/Valheim/start_valheim.sh
This will let the system know that bash should be the interpreter for the script it is about to execute.

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

Credit:
Thank you andrew_slav47 for the SIGINT suggestion, added it to the guide.
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...
  1. Line 1 - Updates the repos on the system and then upgrades available packages at 2am
  2. Line 2 - Reboots the machine at 2:30am after the update
  3. 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.

Finally you will need to forward the ports through your router to your server in order for this to work for others outside your network. The following link will take you to a list of routers in alphabetical order and as long as you find your router on the list click it and there will be a guide that should help get you started.
Port Forwarding by Router[portforward.com]

Renegrade has found that only the UDP ports are needed with this server, for security sake it would be a good idea to only forward them through to the server.

As a side note from Tomatoes, there is also the option to use the new systemd timers infrastructure to accomplish what the cron in this section is doing. Completely up to you if you want to go that route and to read more about it check out the following link.
https://wiki.archlinux.org/title/Systemd/Timers

Thank you for the update TwistedForHire! For those wanting to use the crossplay features of the game you will need to run the following before starting the server up:
sudo apt-get install libpulse-dev libatomic1 libc6
Source discussion:
https://www.reddit.com/r/valheim/comments/xpw0b5/comment/irlz5ig/?utm_source=share&utm_medium=web2x&context=3
Troubleshooting
If you are getting a segfault issue when the server is trying to start it may be due to a bad or unneeded module that is used by crossplay. Simply renaming the socket in the server files directory should fix the issue:
libparty.so -> libparty.bak
- Renegrade
61 Comments
Arathorne84 29 Jul @ 11:21am 
Hi, sorry for my late reply, the firewall was disabled, the ports opened, either way it didnt function. Ill later try with different distribution
Griffen8280  [author] 18 Jul @ 6:25am 
Check if the OS your using has a firewall built in and turned on. If so it may be blocking the ports/communication which would cause an immediate disconnect.
Arathorne84 17 Jul @ 7:53am 
Hi, i cannot connect to server i made, step by step using this guide, problem is it dosent even ask for passowrd, just disconnects, i made tcp and udp ports for it but no help;-;
Griffen8280  [author] 10 Jun @ 5:35am 
For Revan7even I don't know. This server should use the standard Steam reporting protocols and should be browse able in the server finder built into Steam under View -> Game Servers. As long as the port forwarding is working and your ISP doesn't have you in a private VLAN group (My ISP does that by default and I had to call and have it removed) then you should see your server in Steam.
For MrMiyagi, I myself don't run any mods on this game either so don't really have any insights into that unfortunately. From the little searching I did online it seems like there is a helper/loader that is needed to get mods running, one I saw was BepInEx. There is a guide on installing it to a server on dathost.net you might want to check out. It's written for Windows based Servers but may provide insight in how to get it running.
MrMiyagi 7 Jun @ 5:42pm 
This guide was awesome and helped me setup my own server for my son and friends to play together!

Do you have any pointers/advice on how to set up mods (bepex) as a separate world on the same server (+force_install_dir /home/$USER/Valheim-Modded +login anonymous +app_update 896660 +quit) ?

I believe you can install into a different path then pop in the mods in there so you can have a vanilla server and modded (running one at time of course). However I don't know how to setup the mods and not many guides out there.
Revan7even 6 Jun @ 7:58pm 
Is there a way to get an invite code from the server? Or how to check the connection? None of my friends can connect to the server, and I know it's not the PC or the router because I run a Minecraft server on the same PC and the port forwarding and everything works for that.
Griffen8280  [author] 23 Apr @ 6:30am 
Could be an issue with the naming of your savedir in the server startup config.



First off thanks to the OP, so I could pinpoint the problem and solve this quicker.

If anyone stumbles upon this post just as I did, I just think it's worth mentioning.

This is on linux.
First I used:
-savedir "~/valheim/worlds/"
(~/ should point to user home directory root)
This did not give intended result as it created the directories in the valheim root folder instead with the result of:
"~/valheim/~/valheim/worlds/"
or simplified
"/home/username/valheim/~/valheim/worlds/"

To get proper results use either:
-savedir "/home/username/valheim/worlds/"
or
-savedir "worlds/

Obviously adjust the above to where your Valheim install-folder is located.

Found this at https://www.reddit.com/r/valheim/comments/r4wu7p/help_dedicated_server_error_nullreferenceexception/

May be worth a try.
gaber212 16 Apr @ 8:10pm 
I just updated by signing into steamcmd using 'login anonymous' and then 'app_update 896660' and am now receiving this error message when trying to boot up my server using './myserverstart,sh/'

NullReferenceException: Object reference not set to an instance of an object at ZInput.GetButtonDown (System.String name) [0x00000]

Any ideas?
Chaoskatze 28 Jan @ 4:15pm 
Very helpful my friend. Thank you very much.
Griffen8280  [author] 24 Jan @ 2:33pm 
My guess is your possibly running out of memory. I never tried running this from a rpi. The only game server I've ever run from a rpi was Minecraft and it would run out of memory and just freeze kind of the way you're describing.