Skip to content

Instantly share code, notes, and snippets.

@BalzGuenat
Last active February 5, 2024 03:24
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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 pi@mypi.ddns.net.
  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!!

@AntonMadness
Copy link

AntonMadness commented May 18, 2020

Thanks man! Awesome tutorial and kuddo's for going the extra length with no-ip and tmux!!!!

The problem with me for autostarting were because of typing ect instead of etc. Then I had to change the paths to more absolute paths. Also to keep home clean, I've put everything in a folder tShock.
The path in server-start.sh:

#!/bin/bash
sudo mono /home/pi/tShock/TerrariaServer.exe -config /home/pi/tShock/serverconfig

and serverconfig:

maxplayers=4
port=7777
worldpath=/home/pi/tShock/Worlds/
world=/home/pi/tShock/Worlds/MediumExpert1.wld

Because my internet broadband is limited to 100kb/s up, I decided to add smeedometer to the monitor tmux:

#!/bin/sh
su - pi -c 'tmux new-session -d "sh /home/pi/tShock/server-start.sh"; tmux split-window -h "htop"; tmux split-window -v "sudo speedometer -l  -r eth0 -t eth0 -m $(( 768 * 1024 * 12 / 10))"; tmux last-pane'
exit 0

Running Raspberry Pi4
Debian 10.4
TShock 4.4.0 Pre 1 for Terraria 1.4.0.2

The only thing not working now is, I can't seem to find the config.json and auth files. But that's more a TShock thing than overall installation.

@AntonMadness
Copy link

I've found config.json in tshock folder in stead of tShock. Maybe its an idea to add a step in "Installing Terraria and tShock" to upack all files in tshock instead of the home folder. Just to keep everything neat in one folder. Makes it more neat and easy to back up everything. Especially in these times where Terraria and tShock get regular updates, I only have to execute one mv command to backup everything and one sudo mv -r -v drop in the update.

@BalzGuenat
Copy link
Author

@AntonMadness if you are willing to write that step, I'll gladly add it.

@Dedave2
Copy link

Dedave2 commented Mar 10, 2021

I really need help. So, I extracted the file and everything to downloads (the default directory it extracts files to) and then I tried doing mono ./TerrariaServer.exe (i did do the cd command in downloads folder) and for some reason it says "No such file or directory". I've tried everything, looked everywhere for solutions, nothing. Please help.

@BalzGuenat
Copy link
Author

@Dedave2 can you post the exact command and output that gives you trouble?

@Dedave2
Copy link

Dedave2 commented Mar 10, 2021

@BalzGuenat I tried both paths,
cd /home/pi/Downloads
then im in downloads
I do
mono TerrariaServer.exe
Then it says
Cannot open assembely 'TerrariaServer.exe': No such file or directory.
I try the folder I actually extract
cd /home/pi/Downloads/TShock-4.4.0-pre15
Then im in the TShock folder
Same thing.
Cannot open assembely 'TerrariaServer.exe': No such file or directory.
Please help

@BalzGuenat
Copy link
Author

Looks like you are trying this with a pre-release of TShock. Please try to get it to run with a stable release first. The latest is 4.3.26. But that shouldn't be what's causing your issues.

What are the contents of your TShock folder? Can you do the following and copy-paste the contents of the terminal here, including the commands, please?

cd /home/pi/Downloads/TShock-4.4.0-pre15
ls
mono TerrariaServer.exe

@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