How to Install Minecraft Server on Debian 9

Updated 

7 min read

Minecraft is one of the most popular games of all time. It is a sandbox video game about building various structures and going on adventures.

This tutorial explains how to install and configure Minecraft Server on Debian 9. We'll use Systemd to run the Minecraft server and the mcrcon utility to connect to the running instance. We'll also show you how to create a cronjob that will perform regular server backups.

Prerequisites

The user you are logged in as must have sudo privileges.

Install the packages required to build the mcrcon tool:

sudo apt update
sudo apt install git build-essential

Installing Java Runtime Environment

Minecraft requires Java 8 to be installed on the system.

We'll install the headless version of the JRE. This version is more suitable for server applications since it has fewer dependencies and uses less system resources:

sudo apt install openjdk-8-jre-headless

Verify the installation by running:

java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1~deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.212-b01, mixed mode)

Creating Minecraft User

The next step is to create a new system user and group with home directory /opt/minecraft that will run the Minecraft server. To do so run the following command:

sudo useradd -r -m -U -d /opt/minecraft -s /bin/bash minecraft

The user will have no password. This is good security practice because this user will not be able to login via SSH.

Installing Minecraft on Debian

Before starting with the installation process, make sure you switch to minecraft user.

sudo su - minecraft

Create three new directories inside the user home directory:

mkdir -p ~/{backups,tools,server}
  • The backups directory will store your server backup. You can later synchronize this directory to your remote backup server.
  • The tools directory will store the mcrcon client and the backup script.
  • The server directory will contain the actual Minecraft server and its data.

Downloading and Compiling mcrcon

RCON is a protocol that allows you to connect to the Minecraft servers and execute commands. mcron is RCON client built in C.

We'll download the source code from GitHub and build the mcrcon binary.

Start by navigating to the ~/tools directory and clone the Tiiffi/mcrcon repository from GitHub using the following command:

cd ~/tools && git clone https://github.com/Tiiffi/mcrcon.git

Switch to the repository directory:

cd ~/tools/mcrcon

Start the compilation of the mcrcon utility by running:

gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c

Once completed, you can test it by typing:

./mcrcon -h

The output will look something like this:

Usage: mcrcon [OPTIONS]... [COMMANDS]...
Sends rcon commands to Minecraft server.

Option:
  -h		Print usage
  -H		Server address
  -P		Port (default is 25575)
  -p		Rcon password
  -t		Interactive terminal mode
  -s		Silent mode (do not print received packets)
  -c		Disable colors
  -r		Output raw packets (debugging and custom handling)
  -v		Output version information

Server address, port and password can be set using following environment variables:
  MCRCON_HOST
  MCRCON_PORT
  MCRCON_PASS

Command-line options will override environment variables.
Rcon commands with arguments must be enclosed in quotes.

Example:
	mcrcon -H my.minecraft.server -p password "say Server is restarting!" save-all stop

mcrcon 0.6.1 (built: May 19 2019 23:39:16)
Report bugs to tiiffi_at_gmail_dot_com or https://github.com/Tiiffi/mcrcon/issues/

Downloading Minecraft Server

There are several Minecraft server mods like Craftbukkit or Spigot that allows you to add features (plugins) on your server and further customize and tweak the server settings. In this tutorial, we will install the latest Mojang's official vanilla Minecraft server.

The latest Minecraft server's Java archive file (JAR) is available for download from the Minecraft download page.

At the time of writing, the latest version is 1.14.1. Before continuing with the next step you should check the download page for a new version.

Run the following wget command to download the Minecraft jar file in the ~/server directory:

wget https://launcher.mojang.com/v1/objects/ed76d597a44c5266be2a7fcd77a8270f1f0bc118/server.jar -P ~/server

Configuring Minecraft Server

Once the download is completed, navigate to the ~/server directory and start the Minecraft server:

cd ~/server
java -Xmx1024M -Xms512M -jar server.jar nogui

When the server is started for the first time it executes some operations and creates the server.properties and eula.txt files and stops.

[23:41:44] [main/ERROR]: Failed to load properties from file: server.properties
[23:41:45] [main/WARN]: Failed to load eula.txt
[23:41:45] [main/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.

As you can see from the output above you'll need to agree to the Minecraft EULA in order to run the server. Open the eula.txt file and change eula=false to eula=true:

nano ~/server/eula.txt
~/server/eula.txt
#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).
#Sun May 19 23:41:45 PDT 2019
eula=true

Close and save the file.

Next, enable the rcon protocol and set the rcon password by editing the server.properties file:

nano ~/server/server.properties

Locate the following lines and update their values as shown below:

~/server/server.properties
rcon.port=25575
rcon.password=strong-password
enable-rcon=true
Do not forget to change the strong-password to something more secure. If you don't want to connect to the Minecraft server from remote locations make sure the rcon port is blocked by your firewall.

You can also adjust the server's default properties. For more information about the possible settings visit the server.properties page.

Creating Systemd Unit File

Switch back to your sudo user by typing exit.

Open your text editor and create a file named minecraft.service in the /etc/systemd/system/:

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

Paste the following configuration:

/etc/systemd/system/minecraft.service
[Unit]
Description=Minecraft Server
After=network.target

[Service]
User=minecraft
Nice=1
KillMode=none
SuccessExitStatus=0 1
ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
WorkingDirectory=/opt/minecraft/server
ExecStart=/usr/bin/java -Xmx1024M -Xms512M -jar server.jar nogui
ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password stop

[Install]
WantedBy=multi-user.target

Modify the Xmx and Xms flags according to your server resources. The Xmx flag defines the maximum memory allocation pool for a Java virtual machine (JVM), while Xms defines the initial memory allocation pool. Also, make sure that you are using the correct rcon port and password.

Save and close the file and notify systemd that we created a new unit file:

sudo systemctl daemon-reload

Start and enable the Minecraft server by executing:

sudo systemctl start minecraft
sudo systemctl enable minecraft

The first time you start the service it will generate several configuration files and directories including the Minecraft world.

Check the service status with the following command:

sudo systemctl status minecraft
● minecraft.service - Minecraft Server
   Loaded: loaded (/etc/systemd/system/minecraft.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-05-19 23:49:18 PDT; 9min ago
 Main PID: 7262 (java)
    Tasks: 19 (limit: 4915)
   CGroup: /system.slice/minecraft.service
           └─7262 /usr/bin/java -Xmx1024M -Xms512M -jar server.jar nogui

Adjusting Firewall

If your server is protected by a firewall and you want to access Minecraft server from the outside of your local network you need to open port 25565.

To allow traffic on the default Minecraft port 25565 type the following command:

sudo ufw allow 25565/tcp

Configuring Backups

In this section, we'll create a backup shell script and cronjob to automatically backup the Minecraft server.

Start by switching to user minecraft:

sudo su - minecraft

Open your text editor and create the following file:

nano /opt/minecraft/tools/backup.sh

Paste the following configuration:

/opt/minecraft/tools/backup.sh
#!/bin/bash

function rcon {
  /opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password "$1"
}

rcon "save-off"
rcon "save-all"
tar -cvpzf /opt/minecraft/backups/server-$(date +%F_%R).tar.gz /opt/minecraft/server
rcon "save-on"

## Delete older backups
find /opt/minecraft/backups/ -type f -mtime +7 -name '*.gz' -delete

Save the file and make the script executable by issuing the following chmod command:

chmod +x /opt/minecraft/tools/backup.sh

Next, create a cron job that will run once in a day automatically at a fixed time.

Open the crontab file by typing:

crontab -e

To run the backup script every day at 23:00 paste the following line:

0 23 * * * /opt/minecraft/tools/backup.sh

Accessing Minecraft Console

To access the Minecraft Console you can use the mcrcon utility. The syntax is as follows, you need to specify the host, rcon port, rcon password and use the -t switch which enables the mcrcon terminal mode:

/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password -t
Logged in. Type "Q" to quit!
> 

To access the Minecraft Console from a remote location make sure the rcon port is not blocked.

If you are regularly connecting to the Minecraft console, instead of typing this long command you can create a bash alias.

Conclusion

You have successfully installed Minecraft server on your Debian 9 system and set up a daily backup.

If you hit a problem or have feedback, leave a comment below.