Skip to content

Instantly share code, notes, and snippets.

@thelegendofbrian
Last active October 26, 2024 18:44
Show Gist options
  • Save thelegendofbrian/b9d06348cedc686b59c42534c7809983 to your computer and use it in GitHub Desktop.
Save thelegendofbrian/b9d06348cedc686b59c42534c7809983 to your computer and use it in GitHub Desktop.
Set up a Minecraft server on Linux server using systemd

Set up a Minecraft server on Linux server using systemd

Set up server daemon

Make a user and group for the Minecraft server

sudo useradd -d /var/minecraft -m -r -U minecraft
sudo adduser brian minecraft
sudo chmod g+s /var/minecraft

Make the file structure

sudo su minecraft
cd ~
mkdir servers
mkdir build
mkdir build/mcrcon
cd build/mcrcon

Build a tool to send commands to the server using RCON

git clone git://git.code.sf.net/p/mcrcon/code mcrcon-code
cd mcrcon-code
gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c
cp mcrcon ~/

Enable RCON on the minecraft server

nano ~/servers/technic-blightfall-2.1.5/server.properties

Add the following lines

enable-rcon=true
rcon.port=25575
rcon.password=<password>

Make it unreadable to the public since it contains the RCON password

chmod 770  ~/servers/technic-blightfall-2.1.5/server.properties

Add a stop script that utilizes RCON

nano ~/servers/technic-blightfall-2.1.5/stop.sh

And add the following lines

#!/bin/bash
/var/minecraft/mcrcon -H localhost -P 25575 -p <password> stop

Make it executable, but unreadable to the public since it contains the RCON password

chmod 770  ~/servers/technic-blightfall-2.1.5/stop.sh

Exit from the su command add a firewall exception

exit
sudo ufw allow 25575

Add a daemon

sudo nano /etc/systemd/system/minecraft-blightfall.service

Paste the following in

[Unit]
Description=Blightfall Minecraft Server
Documentation=

Wants=network.target
After=network.target

[Service]
User=minecraft
Group=minecraft
Nice=5
KillMode=none

ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
PrivateTmp=true
InaccessibleDirectories=/root /sys /srv -/opt /media -/lost+found
ReadWriteDirectories=/var/minecraft/servers/technic-blightfall-2.1.5

WorkingDirectory=/var/minecraft/servers/technic-blightfall-2.1.5
ExecStart=/var/minecraft/servers/technic-blightfall-2.1.5/start.sh
ExecStop=/var/minecraft/servers/technic-blightfall-2.1.5/stop.sh

[Install]
WantedBy=multi-user.target

Reload and enable the daemon

systemctl daemon-reload
systemctl enable minecraft-blightfall.service

Test the daemon

systemctl start minecraft-blightfall.service
systemctl status minecraft-blightfall.service
journalctl -u minecraft-blightfall.service -f

Set up automated backups

Make a symlink to a backup drive and set necessary permissions

ln -s /path/to/backups/folder /var/minecraft/backups
sudo chown -R minecraft:minecraft /path/to/backups/folder
sudo chmod -R 770 /path/to/backups/folder

Download my Minecraft backup utility and configure it

cd /var/minecraft
wget -O mc-backup-util.jar https://github.com/thelegendofbrian/mc-server-backup-util/releases/download/v0.0.2/mc-server-backup-util-0.0.2.jar
java -jar mc-backup-util.jar
nano config.ini

Edit the config values for the following

serversDirectory=servers
backupsDirectory=backups

Test to make sure it works

java -jar mc-backup-util.jar
ls backups/*

Install cronic so you only get emailed about errors from cron tasks

sudo apt install cronic

Schedule the backup

sudo crontab -u minecraft -e

Add the following lines to back up every 12 hours and get emails about errors

MAILTO="[email protected]"
0 0  * * * cd /var/minecraft && cronic java -jar mc-backup-util.jar
0 12 * * * cd /var/minecraft && cronic java -jar mc-backup-util.jar

Usage

View server log

journalctl -u minecraft-blightfall.service -f

Start, stop, restart server

systemctl start minecraft-blightfall.service
systemctl stop minecraft-blightfall.service
systemctl restart minecraft-blightfall.service

Open interactive prompt to send commands to the server

/var/minecraft/mcrcon -H localhost -P 25575 -p <password> -t
@thelegendofbrian
Copy link
Author

You didn't include what start.sh should look like

It entirely depends on what you're running. If it's for vanilla, it might start with something like java -jar server.jar. Mod packs often include their own script to start.

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