Setting up a TF2 server

Learn how to simply manage a TF2 server on Linux VPS or a local server with this very fine guide by BurningSmile. Please view the text instructions below as they are kept up to update with the newest changes.


Metamod | Sourcemod | LGSM

Alterively you can use this script to automate everything except the cfg.tf configuration. To run it do the following.


git clone https://github.com/BurningSmile/LinuxScripts.git
mv ./LinuxScripts/Auto-tf2serverinstall.sh .
rm -rf LinuxScripts
chmod +x Auto-tf2serverinstall.sh
./Auto-tf2serverinsall.sh
Then simply follow the steps for generating a cfg.tf server configuration. Finally reboot your VPS with the following commands.
su - tf2server -c '/home/tf2server/tf2server stop'
reboot

Command used in Video are as follows:
Updating:
apt update
apt upgrade
apt dist-upgrade
apt autoremove
apt clean

Turn off auto updates
vim /etc/apt/apt.conf.d/10periodic
Change 1 to 0
Save and exit file

Dependencies for LGSM


Ubuntu
dpkg --add-architecture i386; apt-get update; apt-get install mailutils postfix curl wget file bzip2 gzip unzip bsdmainutils python util-linux tmux lib32gcc1 libstdc++6 libstdc++6:i386 libcurl4-gnutls-dev:i386

Debian
dpkg --add-architecture i386; sudo apt-get update; sudo apt-get install mailutils postfix curl wget file bzip2 gzip unzip bsdmainutils python util-linux tmux lib32gcc1 libstdc++6 libstdc++6:i386 libcurl4-gnutls-dev:i386
Fedora
dnf install mailx postfix curl wget file bzip2 gzip unzippython tmux glibc.i686 libstdc++ libstdc++.i686 libcurl.i686

Centos
yum install mailx postfix curl wget bzip2 gzip unzip python tmux glibc.i686 libstdc++ libstdc++.i686 libcurl.i686

Create a tf2server account
useradd -m --password default tf2server

Create a password for the account
passwd tf2server

Change to the account
su - tf2server

Download the tf2 server management stack
wget -N --no-check-certificate https://gameservermanagers.com/dl/linuxgsm.sh && chmod +x linuxgsm.sh && bash linuxgsm.sh tf2server

Install the server
./tf2server auto-install

Lgsm has recently changed the way they handel server settings. You now copy them to a file in the directory $HOME/lgsm/configs-lgsm/tf2server/tf2server.cfg. Use the following commands to setup this file with the items you will most likely be changing.
vim /home/tf2server/lgsm/config-lgsm/tf2server/tf2server.cfg
Add the following
defaultmap="cp_badlands"
maxplayers="16"
updateonstart="on"
Save the file

Allow tf2server through the firewall.

Replace venet0 with your network interface. You can find this from the output of ip link show. Use the interface that is NOT 'lo'.
iptables -A INPUT -i venet0 -p tcp -m tcp --dport 27015 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i venet0 -p udp -m udp --dport 27015 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i venet0 -p tcp -m tcp --dport 27020 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i venet0 -p udp -m udp --dport 27020 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6

Start the server
./tf2server start

Test connecting to it
Connect ip-here in game console

Stop the server
./tf2server stop

Installing apache2 webserver
exit
apt install apache2
mkdir -p /var/www/html/fastdl/tf2/
cd /var/www/html/fastdl/tf2/
ln -s /home/tf2server/serverfiles/tf/maps maps
ls -la
systemctl status apache2.service
systemctl start apache2.service
systemctl enable apache2.service
Cd ../..
mv index.html index.html.bak


Refresh the page and test downloading a map

Address sftp:// ip here
User tf2server
Password The password you set up on the tf2 server account
Port 22

Change FileZilla directory to serverfiles/tf

Back in the ssh session
Su - tf2server
cd serverfiles/tf/

Now for metamod:
Go to the site and copy its link address for the Linux version.
https://www.metamodsource.net/downloads.php?branch=stable
Wget (link here)
Tar -xvf mmsource (Use tab completion)
Rm mmsource (tab complete)

Now for sourcemod:
Go to the souremod site and copy its link address for the Linux version.
https://www.sourcemod.net/downloads.php?branch=stable
Wget (link here)
Tar -xvf sourcemod (tab complete)
Rm sourcemod
( tab complete)

Setting up admin account
vim ./addons/souremod/config/admins_simple.ini

Go to a steamidfinder.com
Paste your steam url and paste the steam id. Follow the below example
“STEAMID” “99:z” //useraccountname

Go to cfg.tf and follow my example for configuring the server.

Custom settings used in video. Remember to replace IP-GOES-HERE with the ip of your vps.
// Server downloads and files
net_maxfilesize "64"
sv_downloadurl "http://IP-GOES-HERE/fastdl/tf2/"
sv_allowdownload "1"
sv_allowupload "1"
sv_consistency "1"


Unzip the file and upload it to your server in sftp to your serverfiles/tf directory, overwrite any files if asked.

Start the server via ssh
./tf2server start

Automate a restart at midnight. And automate the SRCDS instance starting on server start and restart.
Refer to crontab documentation if needed.
https://linux.die.net/man/5/crontab
https://help.ubuntu.com/community/CronHowto
https://hastebin.com/divuqexeri.rb

crontab -e
Choose 3 to use vim
Add the following in insert mode

0 0 * * * /home/tf2server/./tf2server restart
@reboot /home/tf2server/./tf2server start

Save the file and exit vim

Ensure the crontab file works
./tf2server stop
Exit
Reboot

After the reboot make sure your Srcds instance starts on boot.