How to Install Terraria Server with TShock on Ubuntu 20.04

Updated on November 21, 2023
How to Install Terraria Server with TShock on Ubuntu 20.04 header image

Introduction

Terraria is a 2D action-adventure sandbox game developed by Re-Logic. TShock is a third-party modification to Terraria's server, adding lots of management tools to the official version without compromising the integrity or playability of the game.

This article walks you through the process of setting up a Terraria server of your own by installing TShock on a Vultr Ubuntu 20.04 LTS server instance.

Prerequisites

Before you dig in, you should have deployed a shiny new Ubuntu 20.04 LTS server instance with at least 4GB of RAM on Vultr. Find its preset credentials on the Server Details page from the Vultr Control Panel for later use. Say they are:

  • IPv4 address: 203.0.113.100
  • Username: root
  • Password: YourRootPassword (a 16-bit arbitrary string)

Tips for deploying a cloud server instance for running a TShock/Terraria server:

  • Choose the nearest location to you and other players to get the best connectivity and playing experiences.
  • Be aware that 4GB of RAM is only enough for hosting a few players in one large Terraria world. Choose a larger server size if you want to host more players.

1. Log in to Your Server Instance as Root

Fire up your favorite terminal application on your desktop machine, and then log in to your server instance with preset server credentials.

For example, if you prefer to use OpenSSH, a built-in SSH client on most modern operating systems, type the command listed below in a terminal window to log in to your server as root:

C:\> ssh -p22 root@203.0.113.100

Because you are connecting to the server for the first time, answer with yes to acknowledge its authenticity:

The authenticity of host' 203.0.113.100 (203.0.113.100)' can't be established.
ECDSA key fingerprint is SHA256:CrKyzKrUm0G4ZW2BZquhez9Bh3znDV39uWN0uO38Jjc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '203.0.113.100' (ECDSA) to the list of known hosts.

Input the preset password of root: (won't echo)

root@203.0.113.100's password:
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-91-generic x86_64)

2. Perform Basic Tasks to Harden the System

On this newly created Ubuntu 20.04 LTS server instance, it's necessary to perform some basic tasks to harden the system.

Set up a swap file to smooth system operation:

# fallocate -l 2g /swap
# chmod 600 /swap
# mkswap /swap
# swapon /swap
# echo '/swap    none swap defaults 0 0' >> /etc/fstab
# free -m

Change the preset password of root to a new password: (won't echo)

# passwd root
New password:
Retype new password:
passwd: password updated successfully

For security purposes, restrict root from logging in through SSH:

# sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

Create a normal user named terraria with sudo privileges for system management:

# useradd -ms /bin/bash terraria
# passwd terraria
New password:
Retype password:
Passwd: password updated successfully
# echo 'terraria    ALL=(ALL)   NOPASSWD: ALL' | tee -a /etc/sudoers.d/designated
# chmod 0440 /etc/sudoers.d/designated

Set firewall rules to only open port 22 for SSH connections and port 7777 for communications between the Terraria server and clients:

# ufw default deny
# ufw allow 22
# ufw allow 7777
# ufw enable

Answer with `y' to confirm the operation:

Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Note: By default, the Terraria server listens on port 7777. If you want it to listen on a different port (any port within the range from 10000 to 20000 recommended), be sure to open the same port when setting firewall rules.

Update the system and then reboot:

# apt update
# apt upgrade -y
# apt autoremove -y
# shutdown -r now

After the server instance gets up and running again, log in as terraria to continue:

C:\> ssh -p22 terraria@203.0.113.100

Note: The security measures taken in this section are for beginners only. Consider taking more security measures in the future.

3. Install Mono 6.12.0 Stable (6.12.0.122)

Mono is an open-source cross-platform implementation of Microsoft's .NET Framework. You need to install it before you install and run TShock on your Ubuntu 20.04 LTS server instance.

Set up the Mono repository on the Ubuntu 20.04 LTS system:

$ sudo apt install gnupg ca-certificates -y
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
$ echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
$ sudo apt update

As required by TShock, install the latest stable version of the mono-complete package:

$ sudo apt install mono-complete -y

Confirm the installation of Mono, which is Mono 6.12.0.122 for now:

$ mono -V
Mono JIT compiler version 6.12.0.122 (tarball Mon Feb 22 17:33:28 UTC 2021)

Optionally, use Mono to run a quick Hello World console program to test its functionality.

Create a file named hello.cs with the Nano editor in the user's home directory:

$ cd
$ nano hello.cs

Input source code as follows in the Nano editor:

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("Hello, Mono World!");
    }
}

Press Ctrl+O to save the file, press Enter to confirm the filename, and then press Ctrl+X to exit.

Compile the hello.cs file using the csc program:

$ csc hello.cs

Run the compiled hello.exe program using Mono:

$ mono hello.exe

The program should print Hello, Mono World! on the screen if all goes well.

Besides, test HTTPS connectivity with the following command:

$ csharp -e 'new System.Net.WebClient ().DownloadString ("https://www.terraria.org")'

It should output the content of the Terraria website homepage.

4. Install TShock and its Plugins

Download TShock 4.5.12, the latest stable release of TShock for now:

$ cd
$ wget https://github.com/Pryaxis/TShock/releases/download/v4.5.12/TShock4.5.12_Terraria1.4.3.2.zip

Notice that the filename of the TShock archive tells the matched version of the Terraria server. Therefore, you need to update your Terraria client on your desktop to the same version of the Terraria server before connecting to this server.

Unzip the TShock4.5.12_Terraria1.4.3.2.zip archive to a directory with the same base name TShock4.5.12_Terraria1.4.3.2:

$ sudo apt install unzip
$ basename TShock*.zip .zip | xargs unzip TShock*.zip -d

Move all TShock 4.5.12 files to the /opt directory:

$ sudo mv ~/TShock4.5.12_Terraria1.4.3.2 /opt

If necessary, find and download TShock plugins you want from the TShock Plugin Repository. Then follow plugin-specific instructions to install them.

For example, use the commands listed below to install the autoteam plugin:

$ cd
$ wget https://github.com/TerraTrapezium/AutoTeam/releases/download/v1.0.0/AutoTeam.dll
$ sudo mv AutoTeam.dll /opt/TShock4.5.12_Terraria1.4.3.2/ServerPlugins

5. Test Run the Terraria Server

Having TShock and its plugins in place, it's time to test run the Terraria server using Mono:

$ cd /opt/TShock4.5.12_Terraria1.4.3.2
$ mono TerrariaServer.exe
TerrariaAPI Version: 2.1.0.0 (Protocol v1.4.3.2 (244), OTAPI 1.4.3.2)
TShock 4.5.12.0 (Herrscher of Logic) now running.
AutoSave Enabled
Backups Enabled
Welcome to TShock for Terraria!
TShock comes with no warranty & is free software.
You can modify & distribute it under the terms of the GNU GPLv3.
[Server API] Info Plugin TShock v4.5.12.0 (by The TShock Team) initiated.
Terraria Server v1.4.3.2

Input parameters as you wish to create a new world:

n               New World
d <number>      Delete World

Choose World: n:key_enter:

1       Small
2       Medium
3       Large

Choose size: 3:key_enter:

Terraria Server v1.4.3.2

1       Classic
2       Expert
3       Master
4       Journey

Choose difficulty: 3:key_enter:

Terraria Server v1.4.3.2

1       Random
2       Corrupt
3       Crimson

Choose world evil: 1:key_enter:

Terraria Server v1.4.3.2

Enter world name: YourWorldName:key_enter:

Enter Seed (Leave Blank For Random): :key_enter:

After the new world is ready, choose the new world to specify more configurations:

Terraria Server v1.4.3.2

1               YourWorldName
n               New World
d <number>      Delete World

Choose World: 1:key_enter:

Terraria Server v1.4.3.2

Max players (press enter for 16): 8:key_enter:

Terraria Server v1.4.3.2

Server port (press enter for 7777): 7777:key_enter:

Terraria Server v1.4.3.2

Automatically forward port? (y/n): n:key_enter:

Terraria Server v1.4.3.2

Server password (press enter for none): YourServerPassword:key_enter:

Terraria Server v1.4.3.2

Listening on port 7777
Type' help' for a list of commands.
...
To setup the server, join the game and type /setup 5100010
This token will display until disabled by verification. (/setup)
: Server started

Take note of the setup code 5100010 for later use.

Now, start your Terraria client named Terraria.exe and then connect to the server:

  1. Go to Multiplayer.
  2. Choose Join via IP.
  3. Select or create a player.
  4. Enter Server IP Address: 203.0.113.100.
  5. Enter Server Port: 7777.
  6. Enter Server Password: YourServerPassword.

In the game interface, type Enter to open the dialog box, and then input the following commands to set up an owner (super administrator) account on the server with the name owner and the password OwnerPassword:

/setup 5100010
/user add owner OwnerPassword owner
/login owner OwnerPassword
/setup

For security purposes, be sure to use a strong owner password.

That's all for the initial setup on the server. Now exit the game client from your desktop, and then input exit in the running Terraria server console to shut down the server.

If necessary, customize the TShock configuration file, which is /opt/TShock4.5.12_Terraria1.4.3.2/tshock/config.json:

$ nano /opt/TShock4.5.12_Terraria1.4.3.2/tshock/config.json

Don't forget to save your changes before exiting the Nano editor:

Press Ctrl+O to save the file, press Enter to confirm the filename, and then press Ctrl+X to exit.

6. Install Supervisor

To keep the Terraria server running without manual intervention, it's recommended to use the Supervisor program to watch and control the Terraria server processes.

Install Supervisor:

sudo apt install supervisor -y
supervisord -v

Start the Supervisor service:

sudo systemctl daemon-reload
sudo systemctl start supervisor.service
sudo systemctl enable supervisor.service

Create a Supervisor configuration file for the Terraria server:

sudo nano /etc/supervisor/conf.d/tshock.conf

Input the following content:

[program:tshock]
directory=/opt/TShock4.5.12_Terraria1.4.3.2/
command=mono TerrariaServer.exe -world /home/terraria/.local/share/Terraria/Worlds/YourWorldName.wld -config /opt/TShock4.5.12_Terraria1.4.3.2/tshock/config.json
user=terraria
autostart=true
autorestart=true
stderr_logfile=/var/log/supervisor/error_tshock.log
stderr_logfile_maxbytes=100MB
stdout_logfile=/var/log/supervisor/out_tshock.log
stdout_logfile_maxbytes=100MB

Press Ctrl+O to save the file, press Enter to confirm the filename, and then press Ctrl+X to exit.

Update the Supervisor configurations:

sudo supervisorctl reread
sudo supervisorctl update

Check the TShock log file to make sure that the Terraria server is up and running:

tail -f /var/log/supervisor/out_tshock.log

Then press Ctrl+C to quit the tail program.

That's all for setting up an unattended Terraria server. After that, all players are free to come and go as long as they know the server IP address, the server port, and the server password.

Next Steps

If you want to update TShock configurations later, use the following commands to stop the TShock process before editing the TShock configuration file /opt/TShock4.5.12_Terraria1.4.3.2/tshock/config.json:

$ sudo supervisorctl
supervisor> stop tshock
supervisor> quit

After saving your editing, start the TShock process with:

$ sudo supervisorctl
supervisor> start tshock
supervisor> quit

Some users reported that if your server doesn't allow players to join, try to install mono-devel rather than mono-complete.

When running the TerrariaServer.exe file using Mono, consider increasing Mono threads per CPU for better performance:

$ MONO_THREADS_PER_CPU=50 mono TerrariaServer.exe

Change the TShock Supervisor configuration file in the same fashion:

[program:tshock]
directory=/opt/TShock4.5.12_Terraria1.4.3.2/
command=MONO_THREADS_PER_CPU=50 mono TerrariaServer.exe -world /home/terraria/.local/share/Terraria/Worlds/YourWorldName.wld -config /opt/TShock4.5.12_Terraria1.4.3.2/tshock/config.json
user=terraria
autostart=true
autorestart=true
stderr_logfile=/var/log/supervisor/error_tshock.log
stderr_logfile_maxbytes=100MB
stdout_logfile=/var/log/supervisor/out_tshock.log
stdout_logfile_maxbytes=100MB

More Information