Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

User:Webb/Steam dedicated server: Difference between revisions

From TF2 Classified Wiki
Webb (talk | contribs)
No edit summary
Webb (talk | contribs)
Make redirect
Tag: New redirect
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
:''This article is based on a guide written by Raizo, the original of which can be found [https://blog.raizo.dev/posts/tf2-classic-linux-server-tutorial/ here].''
#REDIRECT [[Dedicated_Linux_server]]
 
== Reading this article ==
<!-- Originally from https://wiki.teamfortress.com/wiki/Linux_dedicated_server#Reading_this_article -->
A command prefixed by <code>#</code> is meant to be run as '''root'''.
 
A command prefixed by <code>$</code> is meant to be run as a '''regular user''' without root permissions. In this case the <code>srcds</code> user.
 
A command prefixed by <code>Steam></code> is meant to be run inside of a SteamCMD shell.
 
Some commands are listed in-line with the rest of a paragraph and lack this symbol, in which case you should run the command as whichever account you're currently logged in with.
 
== Prerequisites ==
 
* A dedicated server software key from Eminoma
* A Linux server running Ubuntu Server<sup>*</sup> on an amd64 CPU<sup>**</sup> with root access
* At least 25GB of free storage
* A minimum of a ten megabit upload speed if you intend on hosting a server over the Internet, more will be needed if you host custom content
* ''Optional'': An opened port on your firewall for the server if you want a persistent IP address
* ''Optional'': An SSH/SFTP client if doing remote access/file management
* ''Optional'': A port that's been forwarded for the server if you wish to have it accessible over the internet
 
<sup>*</sup> This guide was written for and tested on Ubuntu Server 24.04 LTS Minimal. Other distros may use different package names and conventions.
 
<sup>**</sup>Architectures other than amd64 (including 32-bit x86) are not supported. Using an x86 compatibility layer like Box86 for another architecture may not work.
 
== Registering your dedicated server key ==
 
As of writing this TF2C is still in the prerelease phase. As such you'll need to get a key from the developers to run the server. This is separate from the game key for the client.
You can either register this using a dedicated Steam account (recommended for security reasons) or your own.
 
To register, go to Steam's library tab, click "Add a game" in the bottom left corner, and click "Activate a Product on Steam". You can enter your server key there, after which it will
be added to the signed in account's library.
 
== Preparing for install ==
 
=== Dependencies ===
 
We'll need to install some required software and tools for our server first. Let's start with SteamCMD, which we'll use to download and update the server software.
 
Since SteamCMD is built for 32-bit, we'll need to enable 32-bit packages. We'll also add the <code>multiverse</code> repository which has SteamCMD.
 
<pre>
# add-apt-repository multiverse
# dpkg --add-architecture i386
# apt update
</pre>
 
Next, we'll install the <code>dialog</code> package so the Steam Subscriber Agreement dialog works, and then the rest of the packages we need.
 
<pre>
# apt install dialog
# apt install steamcmd p7zip aria2 tilde lib32z1 libbz2-1.0:i386 lib32gcc-s1 lib32stdc++6 libcurl3-gnutls:i386 libsdl2-2.0-0:i386 wget
</pre>
 
If your version of Ubuntu doesn't come with a text editor, you may install one now. For this guide we'll use <code>nano</code>.
 
<pre># apt install nano</pre>
 
=== Creating a user for the server ===
 
Pick a directory to install your server into. Many put it under a directory in <code>/opt</code>, but for this guide we'll be using <code>/home/srcds</code>.
 
Create a disabled user with a home directory.
 
<pre>
# useradd -s /bin/false -mr srcds
</pre>
 
<code>-m</code> creates a home directory for the new user, <code>-r</code> specifies that it's a system account that will not have a password, and <code>-s /bin/false</code> prevents the account from having a default shell.
 
=== Installing the live TF2 server ===
 
The live version of TF2's dedicated server is required to run a Classified server.
 
<pre>
# sudo -Hu srcds bash
$ cd ~
$ . /etc/environment
$ steamcmd
Steam>force_install_dir /home/srcds/tf
Steam>login anonymous
Steam>app_update 232250 validate
Steam>quit
</pre>
 
== Installing the dedicated server ==
 
Ensure you've quit SteamCMD before continuing.
 
We'll need to sign in to the Steam account which has access to the dedicated server using SteamCMD. This is done by using <code>login <steam account name></code>
instead of <code>login anonymous</code>. Remember to replace <code>glenda</code> here with your account name. Also ensure you have your Steam Guard ready as there's
a timeout if you take too long to approve the login.
 
<pre>
$ cd ~
$ . /etc/environment
$ steamcmd
Steam>force_install_dir /home/srcds/classified
Steam>login glenda
Steam>app_update 3557020 validate
Steam>quit
</pre>
 
You shouldn't need to log in again, as your session will be cached locally.
 
Next we'll need to make a symlink to fix a library error. If this isn't done we'll face issues
with audio and weapons.
 
<pre>
$ cd ~/classified/bin/linux64
$ rm libvstdlib.so
$ ln -s libvstdlib_srv.so libvstdlib.so
</pre>
 
== Testing the server ==
 
Before proceeding, we can manually run the server to make sure the install was successful.
 
<pre>
$ cd ~/classified
$ ./srcds.sh -tf_path ~/tf +map ctf_2fort +sv_password changethis
</pre>
 
If all goes well, it should start and be accessible over the internet.
 
== Server configuration ==
 
For a basic, private server, you should only need to set a password in <code>classified/tf2classic/cfg/server.cfg</code> by appending the file with <code>sv_password yourpassword</code>.
You can also make it LAN-only by appending <code>sv_lan 1</code> in the same file.
 
If you want a more complicated setup, consider using [https://cfg.tf/ cfg.tf]. If you don't need this you can skip to the next section.
 
Make sure the server type is set to “Internet and LAN” if you want players outside your LAN to be able to join (you may need to port forward if you’re on consumer broadband or open ports on your firewall).
 
Upload the generated ZIP file to your server, perhaps using SFTP, and unzip the folder.
 
<pre>
$ cd /tmp
$ unzip $PATH_TO_ARCHIVE</pre>
 
And merge the <code>cfg</code> folder with <code>/home/srcds/sdk/tf2classic/cfg/</code>.
 
<pre>
$ rsync /tmp/cfg ~/classified/tf2classic/cfg
$ rm -rf /tmp/cfg
</pre>
 
== Managing the server via Systemd ==
 
Systemd is a program that will automatically handle things like logging, restarts, and starting your server when your machine boots. We'll use this to run our server, instead of manually running commands.
 
=== Creating an update script ===
 
We're going to create a script that will update the dedicated server for us instead of typing it out manually each time. Later, we'll optionally use this script to check and update our server automatically when the server starts.
 
First, let's create a directory for the script.
 
<pre>
$ mkdir ~/bin
$ nano ~/bin/update-classified.steamcmd
</pre>
 
Fill the file with the following contents:
 
<pre>
@ShutdownOnFailedCommand 1 //set to 0 if updating multiple servers at once
@NoPromptForPassword 1
force_install_dir /home/srcds/classified
login glenda
app_update 3557020
quit
</pre>
 
You can execute this script manually by using the +runscript argument on SteamCMD.
 
<pre>
$ . /etc/environment
$ steamcmd +runscript /home/srcds/bin/update-classified.steamcmd
</pre>
 
Similarly, we will create one for the live TF2 server as that will need to be updated occasionally. We'll put
it at <code>/home/srcds/bin/update-tf.steamcmd</code>
 
<pre>
@ShutdownOnFailedCommand 1 //set to 0 if updating multiple servers at once
@NoPromptForPassword 1
force_install_dir /home/srcds/tf
login anonymous
app_update 232250
quit
</pre>
 
=== Creating a service file ===
 
Create a service file in <code>/etc/systemd/system/</code> as root. For this guide we'll use <code>/etc/systemd/system/tf2c.service</code>.
 
<pre>
[Unit]
Description=TF2C Server
After=network-online.target
Wants=network-online.target
 
[Service]
Type=simple
User=srcds
StandardError=journal
StandardOutput=journal
WorkingDirectory=/home/srcds/classified
RemainAfterExit=no
ExecStartPre=/usr/games/steamcmd +runscript /home/srcds/bin/update-tf.steamcmd # Exclude or comment this and the next line if you don't want to check for an update before (re)starting
ExecStartPre=/usr/games/steamcmd +runscript /home/srcds/bin/update-classified.steamcmd
Environment="LD_LIBRARY_PATH=".:bin/linux64:$LD_LIBRARY_PATH""
ExecStart=/usr/bin/script -e -c "/home/srcds/classified/srcds_linux -tf_path /home/srcds/tf +map ctf_2fort +maxplayers 24" /dev/null
TimeoutStartSec=infinity
Restart=always
 
[Install]
WantedBy=multi-user.target
</pre>
 
Then, enable the service to start with Systemd on boot.
 
<pre>
# systemctl enable tf2c.service
</pre>
 
You may create as many services as you have TF2C servers but remember to name the service files uniquely. When modifying existing service files, remember to run <code>systemctl daemon-reload</code> before restarting the service.
 
=== Automatic restarts using crontab ===
 
Cron is a program built into most Linux distros that runs scheduled commands.
 
As root, execute <code>crontab -e</code>. This will open a special file in your editor where you can declare what jobs you want to run and when. Let's add a job to run at 04:00 that will restart our server.
 
<pre>
0 4 * * * systemctl restart tf2c.service
</pre>
 
=== Manually managing services ===
 
If you don't wish to use the service files above to automatically boot servers, or you need to perform these actions for maintenance: you may issue commands to manually ''start'', ''stop'', ''restart'', or ''update'' the server(s) through systemd.
 
<pre>
# systemctl restart tf2c # in case you need to restart manually or to grab updates!!
# systemctl stop tf2c # in case you need to stop the server manually
# systemctl start tf2c # in case you need to start the server manually
# systemctl disable tf2c # in case you need to stop the server from booting as your system initializes
# systemctl enable tf2c # in case you need to start the server to boot as your system initializes
</pre>
 
[[Category:Guides]]