DGuard

Tools

systemd service for a Minecraft server

Enter the user, server folder and start script to get a systemd unit that starts the server on boot, keeps the console in tmux and, on shutdown, sends stop and waits for the world to save.

systemd service

Relative to the server folder or absolute. Build it with the start script generator — without auto-restart, since systemd does that.

How long to wait for the world to save after stop. Longer for a big world or a slow disk.

Restart

/etc/systemd/system/minecraft.service

[Unit]
Description=Minecraft server (minecraft)
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
User=minecraft
WorkingDirectory=/opt/minecraft
ExecStart=/usr/bin/tmux new-session -d -s mc /opt/minecraft/start.sh
ExecStop=/bin/sh -c 'tmux send-keys -t mc "stop" Enter; while tmux has-session -t mc 2>/dev/null; do sleep 1; done'
TimeoutStopSec=180
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
Save the file, then run the commands below. tmux must be installed.
Install and manage
sudo nano /etc/systemd/system/minecraft.service sudo systemctl daemon-reload sudo systemctl enable --now minecraft systemctl status minecraft sudo -u minecraft tmux attach -t mc

The last command opens the server console; detach without stopping it with Ctrl+B, then D.

Why a server needs systemd

A server started right in an SSH session dies with it. systemd runs it as a service: on boot without anyone logging in, and again if the process exits. The unit follows the setup from our Linux server article: the console lives in tmux and you can attach to it at any time.

The key part is a clean stop. On systemctl stop the unit sends stop to the console and waits until the server saves the worlds and closes the session. TimeoutStopSec is how long to wait: after it systemd kills the process, so raise it for a big world.

With Restart=always, stop from inside the game acts as a restart: systemd brings the server back after 10 seconds. To shut it down for good, use systemctl stop. Run the server as its own user without root rights.

A server that gets back up after a crash is the foundation. Protection from cheaters is the next step: DGuard, the best anticheat for a modern Minecraft server, uses 0.03% of one core on the main thread.

Read in the knowledge base

FAQ

How do I autostart a Minecraft server on Linux?

With a systemd unit: save the generator's file to /etc/systemd/system/, run systemctl daemon-reload, then systemctl enable --now with the service name.

How do I open the console of a server run by systemd?

With sudo -u <user> tmux attach -t <session>. Detach without stopping the server: Ctrl+B, then D.

How do I stop the server cleanly?

systemctl stop with the service name: the unit sends stop and waits for the world to save.

Do I need auto-restart in the start script?

No: with systemd, Restart= does it. A loop in the script would get in the way of stopping — the server would start again inside the session.

The tools are free. So is anti-cheat protection

DGuard — the best anticheat for a modern Minecraft server — catches all 47 cheat types in our comparison; the nearest rival, Intave, catches 27. Free up to 30 players online, with the same protection as on paid plans.