Skip to content

Instantly share code, notes, and snippets.

@claudio4
Last active December 5, 2019 23:52
Show Gist options
  • Save claudio4/fee4cc3e3fff9d468cdc261aa25b0756 to your computer and use it in GitHub Desktop.
Save claudio4/fee4cc3e3fff9d468cdc261aa25b0756 to your computer and use it in GitHub Desktop.
Minecraft Systemd service

Based on chungy's system service

Place minecraft@.service in /lib/systemd/system, and run systemctl daemon-reload. mc-< your server name > should go into /etc/conf.d or an equivalent directory (change the path in the service too). /etc/conf.d/mc-<your server name> should not have world permissions. chown root:root and chmod 600 the file. Your server should be at /opt/minecraft/<your server name> and your server executable should be called server.jar.

The user and group minecraft should exist. The minecraft user should only be a member of the minecraft group, for system security. The service is set up as such that security exploits of the Minecraft server and/or Java do not severely affect the rest of the system. /home is entirely inaccessible, /tmp and /var/tmp are sandboxed, no real device access is possible, and system directories like /etc cannot be written to. This takes hold even if there are otherwise file modes that would allow it.

In order to access the server terminal rcon should be enabled in you server. In server.properties:

enable-rcon=true
rcon.port=<your desired port>
rcon.password=<A stron password>

To connec to the rcon terminal mcrcon is recomended. mcrcon -H localhost -P <your rcon port> -p <your rcon password>

Tested on minecraft 1.14.4 (Vanilla and PaperMC)

[Unit]
Description=Minecraft server: %i
Wants=network.target
After=local-fs.target network.target
[Service]
User=minecraft
Group=minecraft
UMask=0027
EnvironmentFile=/etc/conf.d/mc-%i
#KillMode=none
KillSignal=SIGINT
TimeoutStopSec=15
SuccessExitStatus=0 1 255
NoNewPrivileges=true
PrivateDevices=true
PrivateTmp=true
ProtectHome=true
ProtectSystem=full
WorkingDirectory=/opt/minecraft/%i
ExecStart=/usr/bin/java $ARGS -jar server.jar
#ExecStop=/usr/local/bin/mcrcon -H localhost -P ${RCON_PORT} -p ${RCON_PASSWD} stop
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment