How to Set Up a Garry's Mod Dedicated Server

by Tom Richards

Sunday, January 04, 2015

After fights with the server browser and entirely too many buggy prop hunt servers, I decided to setup my own dedicated server for Garry’s Mod. Documentation for this does exist, but I had to start from scratch a few times in order to get everything just right. This is the information I wish I had when setting it up the first time.

Operating systems

This guide covers setup on Linux, but should be applicable to other platforms with minor tweaks.

0. Don’t run things as root

Steamcmd, dedicated servers, and other tools mentioned in this article should absolutely NOT be run as root. These programs are perfectly capable of running under an unprivileged user. Do yourself a favor and create an entirely separate user to run everything. We’re going to create a user that doesn’t have SSH or sudo privileges. If you absolutely must have SSH access for this user, consider using SSH keys instead of adding a password.

$ useradd -m -s /bin/bash archie
$ sudo -i -u archie

From this point forward, we’ll assume our current user is archie, with a home directory of /home/archie.

1. Get steamcmd

Steamcmd is the command-line version of the Steam client. You’ll need this tool if you want to download dedicated servers for steam games without using the graphical steam client.

Download the latest steamcmd for Linux. Need downloads and instructions for other platforms? See Steamcmd (Valve Developer Community Wiki). Since the .tar.gz file does not contain a directory of its own, I recommend creating a new directory and extracting the archive there.

$ mkdir ~/steamcmd
$ cd ~/steamcmd
$ wget http://media.steampowered.com/installer/steamcmd_linux.tar.gz
$ tar xvf steamcmd_linux.tar.gz
$ rm steamcmd_linux.tar.gz

2. Create directory for games

If you want most of the popular Garry’s Mod game types to work, you’ll need a few additional games. It’s nice to explicitly set the install directories for each of them, in my opinion. Ultimately, we’ll end up with something like this:

$ tree ~/source-games
/home/archie/source-games/
├── counter-strike-source-ds
├── garrys-mod-ds
└── team-fortress-2-ds

However, we’ll just make the containing directory for now:

$ mkdir ~/source-games

For reasons that will become clear later, refrain from using spaces and capital letters in your directory names.

3. Update steamcmd

When running steamcmd for the first time, it will likely require an update before you can download the dedicated server programs.

$ cd
$ ./steamcmd/steamcmd.sh

Once you reach the Steam> prompt, simply type quit or press Ctrl+C to end the program.

4. Download games

This step requires approximately 13GB of network download & disk space. Please mind your bandwidth caps (ugh) or disk space limitations for those of us on tiny cloud servers.

We’ll be downloading the dedicated servers for three games: Garry’s Mod, Counter-Strike: Source, and Team Fortress 2. For convenience, I’ve prepared a steamcmd script that downloads and installs these games. You’ll need to tweak the force_install_dir paths to match your chosen user. Again, refrain from spaces and capital letters in directory names.

//
// Filename: garrys-mod-setup.steamcmd-script.txt
// Remarks: Garry's Mod setup script for steamcmd.
//

@ShutdownOnFailedCommand 1
@NoPromptForPassword 1

login anonymous

// Garry's Mod Dedicated Server
force_install_dir /home/archie/source-games/garrys-mod-ds
app_update 4020
app_update 4020 validate

// Counter-Strike: Source Dedicated Server
force_install_dir /home/archie/source-games/counter-strike-source-ds
app_update 232330
app_update 232330 validate

// Team Fortress 2 Dedicated Server
force_install_dir /home/archie/source-games/team-fortress-2-ds
app_update 232250
app_update 232250 validate

// Done!
quit

Download the above script, and use the +runscript argument for steamcmd to run all the commands in the file. Note that steamcmd will complain if you don’t specify an absolute path to the script file.

$ cd
$ ./steamcmd/steamcmd.sh +runscript /home/archie/garrys-mod-setup.steamcmd-script.txt

Wait patiently. Since you’re at the mercy of both your internet connection and Valve’s content distribution speed, you might as well take a caffeine break. The script will automatically exit the steamcmd shell upon completion.

5. Configure server

Q: So what’s your deal with the spaces and capital letters?

A: Here are some caveats regarding paths in the mount.cfg file on Linux:

Mount paths

Did you use spaces or capital letters in any of those install directories in steps two or four? Start over. Seriously.

Otherwise, head on over to /home/archie/source-games/garrys-mod-ds/garrysmod/cfg and open up mount.cfg in your favorite editor. Fill in the entries for cstrike and tf as shown below.

//
// Filename: mount.cfg
// Remarks:
// Use this file to mount additional paths to the filesystem
// DO NOT add a slash to the end of the filename
//

"mountcfg"
{
    "cstrike" "/home/archie/source-games/counter-strike-source-ds/cstrike"
    "tf"      "/home/archie/source-games/team-fortress-2-ds/tf"
}

In the same directory, open server.cfg and insert the following barebones settings. You’ll need to substitute hostname and ip with your server’s hostname and public IP address, respectively.

//
// Filename: server.cfg
// Remarks: Barebones configuration for main game server
//

hostname example.com
ip 127.0.0.1
sv_allowdownload 1
sv_allowupload 1

6. Run server

You can find the Garry’s Mod dedicated server binary at /home/archie/source-games/garrys-mod-ds/srcds_run. If you’d like to host workshop content, check out Workshop for Dedicated Servers. Here’s an example launcher for the “Prop Hunt” gametype:

#!/usr/bin/env bash
#
# Filename: prop-hunt-launcher.sh
# Remarks: Garry's Mod dedicated server with "Prop Hunt" workshop collection
#

exec nohup \
    /home/archie/source-games/garrys-mod-ds/srcds_run \
    -authkey <your auth key here> \
    -console \
    -condebug \
    -debug \
    -game garrysmod \
    +maxplayers 16 \
    +host_workshop_collection 201918150 \
    +gamemode prop_hunt \
    +map ph_warehouse_v2 \
    &> /dev/null &

7. Configure fast downloads using sv_downloadurl (optional)

SharpGMad is a tool that can be used to extract individual files from .gma (Garry’s Mod Addon) files. In order to run it on Linux, you’ll need to install mono and xvfb from your distribution’s package manager.

You can download and compile SharpGMad as follows:

$ git clone https://github.com/whisperity/SharpGMad
$ cd SharpGMad
$ xbuild SharpGMad.sln

Download the following helper script that automatically extracts .gma addons and compresses them in a sv_downloadurl compatible fashion. Per usual, adjust the paths at the top to fit your preference.

#!/usr/bin/env bash
#
# Filename: gma-extract.sh
# Remarks: Bulk extract-and-compress tool for Garry's Mod addon files
#

: ${DISPLAY:?"A DISPLAY is required, try running with xvfb-run"}
ADDON_DIR=/home/archie/source-games/garrys-mod-ds/garrysmod/addons
EXTRACT_DIR=/path/to/web/root
SHARP_GMAD_PATH=/home/archie/SharpGMad/SharpGMad/bin/Debug/SharpGMad.exe

rm -rf "${EXTRACT_DIR}"
mkdir "${EXTRACT_DIR}"

for file in ${ADDON_DIR}/*.gma;
do
    mono "${SHARP_GMAD_PATH}" extract -file "${file}" -out "${EXTRACT_DIR}"
done

find "${EXTRACT_DIR}" -type f -exec bzip2 "{}" \;

Using your favorite webserver, create a virtualhost that points to EXTRACT_DIR from the script above. Be sure to enable directory indexes. Finally, add the sv_downloadurl configuration option to your server.cfg file.

hostname example.com
ip 127.0.0.1
sv_allowdownload 1
sv_allowupload 1
sv_downloadurl "https://svdownload.example.com/"

Run the gma-extract.sh script and wait patiently.

$ xvfb-run ./gma-extract.sh

Now that you’ve got a boatload of .bz2 files in your EXTRACT_DIR, clients that connect to your server should be able to download files from your server with blazing speed!

8. Updates

Team Fortress 2 and Garry’s Mod push out updates pretty frequently. You can stay up-to-date by simply running the steamcmd setup script again.

$ cd
$ ./steamcmd/steamcmd.sh +runscript /home/archie/garrys-mod-setup.steamcmd-script.txt

Don’t forget to restart your dedicated server after the update. (Kill and restart the srcds_run binary, that is. You don’t need to restart your whole system.)

Happy gaming!