KF2 Dedicated Server on Linux: systemd User Service Unit File
This is a rewritten version of a community guide that shows how to manage a Killing Floor 2 dedicated server on Linux with a systemd user service. It was written on a Gentoo Hardened system but should work similarly on other systemd distributions. The author stresses that nothing is guaranteed: read and understand every command before running it. For installing the server itself see KF2 dedicated server with SteamCMD.
Prerequisites
- A systemd-based distribution.
- A dedicated, unprivileged user for the server (not root, and preferably not in the wheel group, since that is a needless risk).
- SteamCMD and the KF2 dedicated server already installed, and the binary able to start by hand without errors, for example:
exec /path/to/killingfloor2_dedicated_server/Binaries/Win64/KFGameSteamServer.bin.x86_64. - Firewall and port access already prepared (not covered in the guide).
- Root access once, for the linger command.
Notation: commands with $ run as the server user, commands with # as root; drop the prefix when typing.
Example names
User steamserver, home /home/steamserver, unit file kf2-server.service in the user's systemd directory, SteamCMD in /mnt/server/steam/steamcmd and the game in /mnt/server/steam/games/killingfloor2. Change these to suit your system.
Creating the unit file
As the server user, create the directory and file:
$ mkdir -p ~/.config/systemd/user
$ touch ~/.config/systemd/user/kf2-server.service
Content of the file:
[Unit]
Description=Killing Floor 2 Dedicated Server
[Service]
Type=simple
ExecStart=/mnt/server/steam/games/killingfloor2/Binaries/Win64/KFGameSteamServer.bin.x86_64
Restart=on-failure
TimeoutStopSec=30
KillSignal=SIGINT
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
- Description is free text.
- ExecStart must be the full path to the binary, without
execor extra syntax. - Restart=on-failure restarts the server after a crash; the stop signal is SIGINT (Ctrl-C), with 30 seconds to shut down.
First start as a service
- Linger. As root, run
# loginctl enable-linger steamserveronce. The user's processes then survive after the shell is closed, so you do not needscreen. - Do not use su. Open the user session with
$ machinectl shell --uid steamserver(run from a user in the wheel group), or log in from a TTY or the desktop. Withsu, systemd reports there is no medium to connect to. You must use machinectl on every login to manage the service; an alias helps. - Reload.
$ systemctl --user daemon-reload. Do this after every change of the unit file, otherwise the old version keeps being used and debugging wastes hours. - Start.
$ systemctl --user start kf2-server.service. The shell should print nothing; on errors usejournalctl --user -u kf2-server.service. - Check.
$ systemctl --user status kf2-server.service. A healthy status shows "active (running)", the main PID and the server log lines such as DevOnline advertising settings. Then try to join the server in game.
Start at boot
$ systemctl --user enable kf2-server.service or enable and start in one step with enable --now. The author notes a harmless warning that multi-user.target is a non-existent unit for a user instance; reboot to confirm everything starts as intended.
Credits
The author thanks Gentoo and TripWire community helpers, and refers to the systemd documentation and an outdated example unit file from the days before the Linux KF2 server binary existed. The text is released with no restrictions.
FAQ
- Why use systemd instead of screen?
- A user service with lingering enabled keeps running after you log out, can restart on failure, and starts at boot, so a screen session is unnecessary.
- Why not use su to switch to the server user?
- Without a login session systemctl --user has no medium to connect to; use machinectl shell --uid <user> or log in on a TTY or desktop instead.
- How do I stop the server cleanly?
- The unit sends SIGINT (Ctrl-C) as the kill signal, with a 30 second stop timeout.