Skip to content

Instantly share code, notes, and snippets.

@Denperidge
Last active November 21, 2022 12:03
Show Gist options
  • Save Denperidge/c56fe9fd822143d4c77c36a207b6249b to your computer and use it in GitHub Desktop.
Save Denperidge/c56fe9fd822143d4c77c36a207b6249b to your computer and use it in GitHub Desktop.
Setup a Terraria server on Raspbian with one shell script!
Archived!
This has been moved to a dedicated repo: https://github.com/denperidge/terraria-raspbian-server
------------------------------------------
------------------------------------------
#!/bin/bash
VERSION=1449
DEST="/opt/terraria-server/"
CFG="${DEST}/Server.cfg"
# Install prerequisites
sudo apt-get install -y mono-complete tmux wget unzip
# Download and unzip
wget "https://terraria.org/api/download/pc-dedicated-server/terraria-server-${VERSION}.zip"
unzip terraria-server-${VERSION}.zip
# Move needed files away, remove unneeded files
sudo mkdir -p "${DEST}"
sudo chown -R $USER "${DEST}"
sudo chgrp -R $USER "${DEST}"
cp -r "$(pwd)/${VERSION}/Linux"/* "${DEST}"
rm -rf "${VERSION}/"
rm "terraria-server-${VERSION}.zip"
cd "${DEST}"
rm System* Mono* monoconfig mscorlib.dll
WORLDPATH="${DEST}Worlds"
mkdir -p "$WORLDPATH"
# Setup server config file
echo "Creating config file..."
# Static values
echo "worldpath=${WORLDPATH}" >> "$CFG"
echo "secure=1" >> "$CFG"
# Prompt values
# Prompt user, add default value if necessary, add to cfg file
prompt() {
config_command=$1
prompt=$2
default_value=$3
read -p "$prompt [$default_value]: " user_input
echo "${config_command}=${user_input:-$default_value}" >> "$CFG"
}
prompt autocreate "World size (1: small, 2: medium, 3: large)" 1
prompt worldname "World name" World
prompt difficulty "Difficulty (0: classic, 1: expert, 2: master, 3: journey)" 0
prompt maxplayers "Max players (1-255)" 16
prompt port "Port" 7777
prompt language "Server language (Available options: en-US, de-DE, it-IT, fr-FR, es-ES, ru-RU, zh-Hans, pt-BR, pl-PL)" en-US
prompt upnp "Automatically attempt to port-forward (1: yes, 0: no)" 1
echo "world=${WORLDPATH}/${worldname}.wld" >> "$CFG"
# Optional values
read -sp "Password (leave empty for none. WARNING: will be stored insecurely/plaintext): " password
if [ -z "$password" ]
then
echo "No password set!"
else
echo "password=${password}" >> "$CFG"
echo "Password set!"
fi
read -p "Message of the day (leave empty for none): " motd
if [ -z "$motd" ]
then
echo "No MOTD set!"
else
echo "motd=${motd}" >> "$CFG"
echo "MOTD set!"
fi
# Values that can be set later for advanced users/specific cases
echo "#seed=RandomSeed" >> "$CFG"
echo "#banlist=banlist.txt" >> "$CFG"
echo "#npcstream=60" >> "$CFG"
echo "#priority=1" >> "$CFG"
echo "# From the wiki (https://terraria.fandom.com/wiki/Server#serverconfig): Journey Mode power permissions for every individual power. 0: Locked for everyone, 1: Can only be changed by host, 2: Can be changed by everyone."
echo "#journeypermission_time_setfrozen=2" >> "$CFG"
echo "#journeypermission_time_setdawn=2" >> "$CFG"
echo "#journeypermission_time_setnoon=2" >> "$CFG"
echo "#journeypermission_time_setdusk=2" >> "$CFG"
echo "#journeypermission_time_setmidnight=2" >> "$CFG"
echo "#journeypermission_godmode=2" >> "$CFG"
echo "#journeypermission_wind_setstrength=2" >> "$CFG"
echo "#journeypermission_rain_setstrength=2" >> "$CFG"
echo "#journeypermission_time_setspeed=2" >> "$CFG"
echo "#journeypermission_rain_setfrozen=2" >> "$CFG"
echo "#journeypermission_wind_setfrozen=2" >> "$CFG"
echo "#journeypermission_increaseplacementrange=2" >> "$CFG"
echo "#journeypermission_setdifficulty=2" >> "$CFG"
echo "#journeypermission_biomespread_setfrozen=2" >> "$CFG"
echo "#journeypermission_setspawnrate=2" >> "$CFG"
# Create systemd services for automatic startup periodic & save
cd /etc/systemd/system/
# Terraria Server automatic start & shutdown
sudo wget https://gist.github.com/Denperidge/c56fe9fd822143d4c77c36a207b6249b/raw/terrariaserver.service
# Terraria periodic save
sudo wget https://gist.github.com/Denperidge/c56fe9fd822143d4c77c36a207b6249b/raw/terrariaserversave.service
sudo wget https://gist.github.com/Denperidge/c56fe9fd822143d4c77c36a207b6249b/raw/terrariaserversave.timer
# Reload systemd daemon, enable service & timer
sudo systemctl daemon-reload
sudo systemctl enable terrariaserver.service && sudo systemctl start terrariaserver.service
sudo systemctl enable terrariaserversave.timer && sudo systemctl start terrariaserversave.timer

Terraria Server (Raspbian)

Install & setup

(If you don't have a proper swapfile already, do that first! It can be needed for world generation, but it also just improves the Raspberry Pi's performance in general.

wget "https://gist.github.com/Denperidge/c56fe9fd822143d4c77c36a207b6249b/raw/setup.sh"
chmod +x setup.sh
./setup.sh

If necessary, update the version in setup.sh! Last updated at 21/11/2022.

Simply run setup.sh, complete the prompts and you're done! The server will be configured, automatically start on reboot, and save every 15 minutes to be safe.

You can attach to the servers console by using sudo tmux attach-session -t terraria

Uninstall

wget "https://gist.github.com/Denperidge/c56fe9fd822143d4c77c36a207b6249b/raw/uninstall.sh"
chmod +x uninstall.sh
./uninstall.sh
[Unit]
Description=Server for (Desktop) Terraria
After=network-online.target
[Service]
Type=forking
Restart=always
ExecStart=/usr/bin/tmux new-session -d -s terraria "mono --server --gc=sgen -O=all /opt/terraria-server/TerrariaServer.exe -config /opt/terraria-server/Server.cfg"
ExecStop=/usr/bin/tmux send-keys -t terraria "exit" ENTER
[Install]
WantedBy=multi-user.target
[Unit]
Description=Save world in Terraria server
[Service]
Type=forking
ExecStart=/usr/bin/tmux send-keys -t terraria "save" ENTER
[Unit]
Description=Periodically save Terraria server
[Timer]
OnBootSec=15min
OnUnitActiveSec=15min
[Install]
WantedBy=timers.target
# Disable and remove services
sudo systemctl stop terrariaserver.service && sudo systemctl disable terrariaserver.service
sudo systemctl stop terrariaserversave.timer && sudo systemctl disable terrariaserversave.timer
sudo rm /etc/systemd/system/terrariaserver.service /etc/systemd/system/terrariaserversave.service /etc/systemd/system/terrariaserversave.timer
sudo rm /etc/systemd/system/multi-user.target.wants/terrariaserver.service /etc/systemd/system/timers.target.wants/terrariaserversave.timer
sudo systemctl daemon-reload
sudo systemctl reset-failed
sudo rm -rf /opt/terraria-server/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment