How to Setup a Terraria Linux Server
Updated by Linode Contributed by Tyler Langlois
Terraria is a two-dimensional sandbox game, similar to Minecraft, which allows players to explore, build, and battle in an open world. In 2015, the Terraria developers announced support for Linux, which means that players can host their own standalone Terraria servers.
This guide outlines the steps required to run a Terraria server for yourself and others to play on. These steps are compatible with any Linux distribution that uses systemd. This includes recent versions of CentOS, Debian and Ubuntu, Arch Linux and Fedora.
Due to Terraria’s system requirements, a Linode with at least two CPU cores and adequate RAM is required. For this reason, we recommend using our 4GB plan or higher when following this guide. If your Linode does not meet Terraria’s minimum requirements, the process will crash intermittently.
Before You Begin
Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.
This guide will use
sudowherever possible. Complete the sections of our Securing Your Server guide to create a standard user account, harden SSH access and remove unnecessary network services. Do not follow the Configuring a Firewall section in the Securing Your Server Guide–we will configure the firewall for a Terraria server in the next section.
Update your operating system’s packages.
Configure a Firewall for Terraria
NoteTerraria only uses IPv4 and does not use IPv6.
Firewalld is the default iptables controller in CentOS 7+ and Fedora. See our guide on using firewalld for more information.
Enable and start firewalld:
sudo systemctl enable firewalld && sudo systemctl start firewalld
You should be using the public zone by default. Verify with:
sudo firewall-cmd --get-active-zones
Create a firewalld service file for Terraria:
<?xml version="1.0" encoding="utf-8"?> <service> <short>Terraria</short> <description>Open TCP port 7777 for incoming Terraria client connections.</description> <port protocol="tcp" port="7777"/> </service>
Enable the firewalld service, reload firewalld and verify that the Terraria service is being used:
sudo firewall-cmd --zone=public --permanent --add-service=terraria sudo firewall-cmd --reload sudo firewall-cmd --zone=public --permanent --list-services
The output of the last command should be similar to:
dhcpv6-client ssh terraria
UFW (Uncomplicated Firewall) is an iptables controller packaged with Ubuntu, but it’s not installed in Debian by default.
If needed, install UFW:
sudo apt install ufw
Add SSH and a rule for Terraria. It’s important you add rules before enabling UFW. If you don’t, you’ll terminate your SSH session and will need to access your Linode using Lish:
sudo ufw allow ssh sudo ufw allow 7777/tcp
After your rules are added, enable UFW. Next, remove the Terraria rule for IPv6 since it’s not needed:
sudo ufw enable sudo ufw delete 4
NoteThe second command in this step,
sudo ufw delete 4references the fourth rule in your UFW ruleset. If you need to configure additional rules for different services, adjust this as necessary. You can see your UFW ruleset with
sudo ufw statusto make sure you’re removing the correct rule.
To manually configure iptables without using a controller, see our iptables guide for a general ruleset.
You’ll also want to add the rule below for Terraria:
sudo iptables -A INPUT -p tcp --dport 7777 -j ACCEPT
sudo iptables -vL
Install and Configure Terraria
Change your working directory to
/optand download the Terraria tarball. You’ll need to check Terraria’s website for the current release version. Right-click and copy the link to use with
wget. We’ll use 18.104.22.168 as an example in this guide:
cd /opt && sudo curl -O http://terraria.org/server/terraria-server-1344.zip
NoteBefore you install Terraria, be sure the version you download is the same as the clients that will be connecting to it.
You will need the
unziputility to decompress the .zip file. Install it using your distribution’s package manager:
sudo apt install unzip
sudo yum install unzip
Extract the archive and set the necessary permissions:
sudo unzip terraria-server-1344.zip sudo mv /opt/Dedicated\ Server/Linux /opt/terraria sudo rm -rf Dedicated\ Server/ sudo chown -R root:root /opt/terraria sudo chmod +x /opt/terraria/TerrariaServer.bin.x86_64
Running daemons under discrete users is a good practice. Create a
terrariauser from which to run the game server:
sudo useradd -r -m -d /srv/terraria terraria
Terraria has a server configuration file that you can edit with options such as automatic world creation, server passwords, difficulty, and other options. Create a copy of the default file so you have something to revert back to if you run into problems:
sudo mv /opt/terraria/serverconfig.txt /opt/terraria/serverconfig.txt.bak
Create a new server configuration file for yourself. The options below will automatically create and serve
MyWorldwhen the game server starts up. Note that you should change
MyWorldto a world name of your choice.
world=/srv/terraria/Worlds/MyWorld.wld autocreate=1 worldname=MyWorld worldpath=/srv/terraria/Worlds
Managing the Terraria Service
Terraria runs an interactive console as part of its server process. While useful, accessing this console can be challenging when operating game servers under service managers. The problem can be solved by running Terraria in a screen session that will enable you to send arbitrary commands to the listening admin console within Screen.
Install Screen with the system’s package manager:
sudo yum install screen
sudo apt install screen
It’s useful to have an automated way to start, stop, and bring up Terraria on boot. This is important if the system restarts unexpectedly.
Create the following file to define the
terraria systemd service:
1 2 3 4 5 6 7 8 9101112
[Unit] Description=server daemon for terraria [Service] Type=forking User=terraria KillMode=none ExecStart=/usr/bin/screen -dmS terraria /bin/bash -c "/opt/terraria/TerrariaServer.bin.x86_64 -config /opt/terraria/serverconfig.txt" ExecStop=/usr/local/bin/terrariad exit [Install] WantedBy=multi-user.target
ExecStart instructs systemd to spawn a screen session containing the 64-bit
TerrariaServerbinary, which starts the daemon.
KillMode=noneis used to ensure that systemd does not prematurely kill the server before it has had a chance to save and shut down gracefully.
ExecStop calls a script to send the
exitcommand to Terraria, which tell the server to ensure that the world is saved before shutting down. In the next section, we’ll create a script which will send the necessary commands to the running Terraria server.
CautionThis script is intended to save your world in the event that you reboot the operating system within the Linode. It is not intended to save your progress if you reboot your Linode from the Linode Manager. If you must reboot your Linode, first stop the Terraria service using
sudo systemctl stop terraria. This will save your world, and then you can reboot from the Linode Manager.
Create a Script for Basic Terraria Administration
The Terraria administration script needs two primary functions:
- Attaching to the running screen session, which offers a helpful administration console.
- The ability to broadcast input into the screen session so the script can be run to save the world, exit the server, etc.
terrariadfile, enter the following script, then save and close:
1 2 3 4 5 6 7 8 91011121314
#!/usr/bin/env bash send="`printf \"$*\r\"`" attach='script /dev/null -qc "screen -r terraria"' inject="screen -S terraria -X stuff $send" if [ "$1" = "attach" ] ; then cmd="$attach" ; else cmd="$inject" ; fi if [ "`stat -c '%u' /var/run/screen/S-terraria/`" = "$UID" ] then $cmd else su - terraria -c "$cmd" fi
Verify that you can execute the script:
sudo chmod +x /usr/local/bin/terrariad
This script permits you to both:
- Attach to the console for direct administration, and
- Send the console commands like
exitwhile it’s running without needing to attach at all (useful when services like systemd need to send server commands).
NoteThroughout the rest of this guide, you may encounter “command not found” errors when running the
terrariadcommand. This may result from the directory
/usr/local/bin/not being found in the
$PATHwhen running sudo commands, which can occur with some Linux distributions. You can work around this problem by calling the script with the full path. For example, instead of running
sudo terrariad attach, use
sudo /usr/local/bin/terrariad attach.
Start and Enable the Terraria Server
Now that the game server is installed, the scripts are written, and the service is ready, the server can be started with a single command:
sudo systemctl start terraria
The first time you run the server, it will generate the world defined earlier. This will take a while, so give it time before trying to connect. To watch the world generation progress, use:
sudo terrariad attach
In addition to starting and stopping the
terraria service, systemd can also use the service file created earlier to automatically start Terraria on boot.
To enable the service at startup:
sudo systemctl enable terraria
If the operating system is restarted for any reason, Terraria will launch itself on reboot.
To check if the server is running, use the command:
sudo systemctl status terraria
The output should be similar to:
● terraria.service Loaded: loaded (/etc/systemd/system/terraria.service; disabled) Active: active (running) since Tue 2017-03-07 17:37:03 UTC; 7s ago Process: 31143 ExecStart=/usr/bin/screen -dmS terraria /bin/bash -c /opt/terraria/TerrariaServer.bin.x86_64 -config /opt/terraria/serverconfig.txt (code=exited, status=0/SUCCESS) Main PID: 31144 (screen) CGroup: /system.slice/terraria.service ├─31144 /usr/bin/SCREEN -dmS terraria /bin/bash -c /opt/terraria/TerrariaServer.bin.x86_64 -config /opt/terraria/serverconfig.txt └─31145 /opt/terraria/TerrariaServer.bin.x86_64 -config /opt/terraria/serverconfig.txt
Stop the Server
If you ever need to shut down Terraria, use the following command to save the world and shut down the game server:
sudo systemctl stop terraria
Attach to the Console
In the course of running your server, you may need to attach to the console to do things like kick players or change the message of the day (MOTD). To enter the Terraria server console with the
terrariad script use:
sudo terrariad attach
help to get a list of commands. Once you’re done, use the keyboard shortcut CTRL+A then D to detach from the screen session and leave it running in the background. More keyboard shortcuts for Screen can be found in the Screen default key bindings documentation.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
- Install Black Mesa on Debian or Ubuntu
- Install SteamCMD for a Steam Game Server
- Deploy a Just Cause 2 Multiplayer Server on Ubuntu 14.04
- How to Set Up BungeeCord to Link Spigot Servers
- Running a Spigot Minecraft Server on Ubuntu 14.04 and 14.10
This guide is published under a CC BY-ND 4.0 license.