Skip to content

Instantly share code, notes, and snippets.

@BalzGuenat
Last active February 5, 2024 03:24
Show Gist options
  • Save BalzGuenat/65c248d03895b5a030ba to your computer and use it in GitHub Desktop.
Save BalzGuenat/65c248d03895b5a030ba to your computer and use it in GitHub Desktop.
Installing and running a Terraria server with tShock on a Raspberry Pi

Installing and running a Terraria server with tShock on a Raspberry Pi

This was originally posted here on reddit. If you decide to try this, please post a comment either confirming that it works or stating any issues that came up. Thanks to reddit user /u/mat_storm1 for pointing out some typos and thanks to reddit user /u/supercore23 for pointing out potential issues with spaces in the world path.

What is this?

This guide explains how to install and run a Terraria server on your Raspberry Pi. I assume you have already installed Raspbian and have a very basic understanding of the command line. Installation on other Unix-based systems should be very similar. The goal is to reach a setup that only requires you to plug in your Pi to start the server. Connecting to the server should be easily done by using a hostname, not an IP. We should also be able to ssh into the server and do server-admin stuff at any time.

I describe only one way to achieve such a setup and I'm sure there are alternative ways that have advantages. Please post them to the comments. This guide is for a Raspberry Pi 2 Model B running Raspbian wheezy and using the following software versions:

  • mono: 3.2.8
  • tShock 4.3.8 (for Terraria 1.3.0.7)

Setup for other versions should be the same or very similar.

Binding your Pi to a hostname

We are going to use a dynamic DNS to bind the Pi to a hostname. I'm going to use the free service of no-ip for this. Sign up and get yourself a hostname you like. I'll use mypi.ddns.net for this explanation. Substitute your hostname wherever you see this.

Installing the no-ip dynamic update client (DUC)

  1. cd /usr/local/src
  2. sudo wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz
  3. sudo tar xf noip-duc-linux.tar.gz
  4. cd noip-2.1.9-1
  5. sudo make install and finish the initial configuration (enter credentials of your no-ip account, defaults should be fine).

Automatically starting the DUC on startup

  1. sudo nano /etc/init.d/noip
  2. Write the following into the file and save it:
	#!/bin/sh
	case "$1" in
		start)
			echo "Starting noip2."
			/usr/local/bin/noip2
		;;
		stop)
			echo -n "Shutting down noip2.\n"
			pkill noip
		;;
		*)
			echo "Usage: $0 {start|stop}"
			exit 1
	esac
	exit 0
  1. sudo chmod 755 /etc/init.d/noip
  2. sudo nano /etc/rc.local
  3. Add this on a new line before the exit 0 statement: sudo /etc/init.d/noip start

Now we have a hostname that points to the Pi from outside but your router will probably reject incomming connections to it. Log into your router (how this works depends on your router and home network setup) and forward the ports 22 (TCP for ssh) and 7777 (TCP/UDP for Terraria) to your Pi. This will allow players to connect to the server and you to log into your Pi even from outside your network.

Installing the Terraria server

Installing mono

Terraria with tShock is a Windows application and needs a little help from mono to run on Debian. Install mono with sudo apt-get install mono-complete.

Installing Terraria and tShock

  1. We want to download tShock (which includes the executable) from the tShock website onto the Pi. The easiest way to do this is to find the download link (the one that ends with ".zip"), copy it to the clipboard and then do wget http://...tshock-4.3.8-rel.zip. Of course you should supply the full link with the correct version. This will download the zip file into the current directory.
  2. Put the downloaded file in the ~ directory of your Pi. If you used wget from the ~ directory, you can skip this step. If you downloaded the file with another machine use scp or FileZilla to transfer the file to your Pi.
  3. On the Pi: cd ~
  4. unzip tshock-4.3.8-rel.zip (filename will change with version, change accordingly)

You should now already be able to start the server with mono TerrariaServer.exe. Note that this will generate some directories and files. If you don't want to use the default file locations (for worlds, logs etc.) either wait with starting the server or delete the generated files again afterwards. You can exit from the server with ctrl-C if you don't want to generate and load a world.

Setting up the server automatically at boot

Usually the server asks you at every start for some settings (world, max players etc.). To start the server automatically with a script, we need some config files to specify these settings.

The Terraria server config file

This is the file used by the Terraria server, not by tShock.

  1. nano ~/serverconfig
  2. Write (something like) the following into the file and save it:
	maxplayers=8
	port=7777
	worldpath=Worlds/
	world=Worlds/yourWorld.wld

Do not use the default location for world files. The default world path includes the folder "My Games". The space in the name is likely causing problems and you should choose a path with no spaces. Be sure that the world you specified exists in the right place (the path is relative to the server executable, not the world path). You can also use a prexisting world like from your Windows PC. Just copy it into the world directory on the Pi and set world accordingly.

The tShock config file

This file is located at ~/tshock/config.json. You don't have to change much here. Just make sure that ServerPort and MaxSlots agree with the serverconfig file from above. That is all.

Starting the server by script

For ease of use and later scripts, we write a script to start the server. Enter nano ~/server-start.sh and write the following:

#!/bin/bash
mono TerrariaServer.exe -config serverconfig

Save the file and make it executable with sudo chmod 755 ~/server-start.sh.

Keeping control of the server across SSH sessions with tmux

If you just ssh into your Pi, start the server and then get disconnected, there is no easy way to reconnect and enter server commands. We need persistent sessions and tmux will help us (a great tool by the way). Install tmux with sudo apt-get install tmux. You can read up on it some other time if you want. The important things for us are:

  • Enter tmux to start a new session.
  • Press ctrl-B (and release), then press D to detach the tmux session.
  • Enter tmux a to attach to the session.

You can now do this (just to illustrate why tmux is cool, not necessary for the server setup):

  1. ssh into the Pi.
  2. tmux
  3. ~/server-start.sh. The server is running now.
  4. ctrl-B, D. Detach from the tmux session.
  5. ctrl-D. End the SSH session. You are now disconnected from the Pi on which the server is still running.

Now to regain control!

  1. ssh into the Pi.
  2. tmux a. Attach to the tmux session. You should now be back at the server console, being able to give controls (like shutting it down properly with /off).

Autostart the server in a tmux session

Ok, almost there now.

1. sudo nano /etc/init.d/terraria-autostart
2. Write the following into the file and save it:

#!/bin/sh
su - pi -c 'tmux new-session -d "sh /home/pi/server-start.sh" '
exit 0

This will, at startup of the Pi, create a tmux session, run the server-start script and detach from the session. It will do so as the user "pi" which is the default user on a Raspbian install. Change both occurrenes of pi to your username if you've changed it.

3. sudo chmod 755 /etc/init.d/terraria-autostart
4. sudo nano /etc/rc.local
5. Add this line after your noip-start line and before the exit 0 line:

/etc/init.d/terraria-autostart

That's it!

We're done! sudo reboot to fire it off. When you now start your Pi, the following happens automatically:

  1. rc.local runs the script that starts the no-ip DUC which tells no-ip the IP of your Pi.
  2. rc.local runs the terraria-autostart script. A new (detached) tmux session is created and the server-start script is run inside it.
  3. The server-start.sh runs TerrariaServer.exe which takes all its configuration from the serverconfig file. The server is ready to play!

Here's what you and your friends do if they wanna play:

  1. Start Terraria and connect to mypi.ddns.net. Done.

Here's what you do if you wanna play god (or just administrate the server):

  1. ssh into the Pi from anywhere with ssh [email protected].
  2. tmux a. You are now at the server console. Go administrate!

One more cool thing!

You might be interested in how taxing your battles are on the Pi. Here's how you can have your system monitor right next to your server console:

1. sudo apt-get install htop. top is also fine but htop is cooler.
2. Change the line in /etc/init.d/terraria-autostart to the following:

su - pi -c 'tmux new-session -d "sh /home/pi/server-start.sh"; tmux split-window -h "htop"; tmux last-pane'

This splits the tmux session into two panes. Left is the server console and right is the htop monitor that tells you CPU, RAM usage etc. Two additional tmux commands you should know about (remember to press ctrl-B first):

  • left-arrow: change to the pane to the left of the current pane.
  • right-arrow: change to the pane to the right (duh...).

Alright, that is all. Suggestions and criticism are very welcome!!

@AlsoSylv
Copy link

I keep getting the crash "Sever api crashed due to an unhandled expectation"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment