Systemd Unit file for Minecraft Server
- Connect to your (v)root server or if you want to run the server on your machine, open a terminal.
- Become root using su or sudo. To check if you are root run "echo $EUID" if it returns "0" you are root.
- Next install the necessary packages "apt install openjdk-8-jre-headless curl screen nano bash grep"
- Create the /opt folder if it doesn't already exist "mkdir /opt"
- Now you need to create the user for the service: "adduser --system --shell /bin/bash --home /opt/minecraft --group minecraft"
- Create the Systemd Unit file "nano /etc/systemd/system/[email protected]" : "curl https://raw.githubusercontent.com/agowa338/MinecraftSystemdUnit/master/minecraft%40.service > /etc/systemd/system/[email protected]"
Now you can Upload your FTB Modpacks into an subfolder of /opt/minecraft/. For example you would place the modpack "FTB Beyond" in "/opt/minecraft/FTBBeyond" (without spaces in the name).
If you want to run vanilla instances, just create a folder within /opt/minecraft and upload the minecraft_server.jar and create the eula.txt file (using: echo "eula=true" > /opt/minecraft/vanilla/eula.txt).
After you uploaded the minecraft server files, make sure, that "minecraft" is the owner and owning group. To do so just run "ls -la /opt/minecraft". If it is not, run "chown minecraft:minecraft /opt/minecraft/FTBBeyond".
You may also require to complete the installation. For current FTB packages you would run:
cd /opt/minecraft/FTBBeyond echo "eula=true" > /opt/minecraft/FTBBeyond/eula.txt su -c "/opt/minecraft/FTBBeyond/FTBInstall.sh" -s "/bin/bash" minecraft
systemctl enable minecraft@FTBBeyond
systemctl disable minecraft@FTBBeyond
systemctl start minecraft@FTBBeyond
systemctl stop minecraft@FTBBeyond
Entering commands into the console is somehow complicated thorugh the nature of systemd.
So to enter the console "screen" is used (that's why it is in the script).
Note: To detach (exit) screen press [STRG] + [A] followed by [D].
chmod 666 $(tty) su -c "/usr/bin/screen -r" minecraft chmod 430 $(tty)
The first command allows otter users to read and write to your terminal.
The second switches the user context to the minecraft service account.
The last one restores the original terminal access rights for security. It is not very recommended to set the tty access to 666, but it seam to be the only way to enter a screen session within a su session.