Minecraft Server Setup on Ubuntu Last updated: 2021.12.05

Overview

Ubuntu 20.04, Ubuntu 18.04

Create a Minecraft server and complete initial configuration.

Assumptions

Update & Upgrade

Before getting started, run an obligatory system update. This will make sure you have the latest updates and patches for all installed packages.

sudo apt update && sudo apt upgrade
  • apt update downloads updated package information from all the configured repositories on your system.
  • apt upgrade upgrades all installed packages to the most recent versions available.

You may also run the command apt autoremove to uninstall any package dependencies that are no longer needed.

Install Java

Minecraft utilizes Java and requires a Java Runtime Environment (JRE) to be installed on the system. If you intend on running Minecraft 1.17, make sure you install Java version 16 or higher.

OpenJDK

OpenJDK is an open-source implementation of the Java Platform SE, distributed under the GPLv2 license. If you wish to use OracleJDK instead, be aware you must purchase a license for commercial and production use. Select the OpenJDK version below for your desired Minecraft release.

# Minecraft 1.16.5 - 1.17 (OpenJDK 17 LTS)
sudo apt install openjdk-17-jre-headless
# Minecraft 1.12 - 1.16.4 (OpenJDK 11 LTS)
sudo apt install openjdk-11-jre-headless
# Minecraft 1.8 - 1.11 (OpenJDK 8 LTS)
sudo apt install openjdk-8-jre-headless

Verify

Verify that Java installed without issue by checking the version.

java -version

If all is well, the status should resemble the following.

openjdk version "17-ea" 2021-09-14
OpenJDK Runtime Environment (build 17-ea+19-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 17-ea+19-Ubuntu-1, mixed mode, sharing)

Install Minecraft

With Java installed, and your server properly caffeinated, its time to install Minecraft. This bit covers the process for installing PaperMC, instead of the vanilla release from Mojang. PaperMC provides extra configuration options, offers performance improvements, and allows you to install Paper / Spigot / Bukkit plugins. This is a server-side change, and requires no additional configuration for users client-side. Learn more about PaperMC at papermc.io.

Create Minecraft User

Add a new system user for Minecraft Server application to run under. This is done for security, as you do not want the application running as the root or administrative user. The following will add a new system user named minecraft and create its home directory /opt/minecraft.

sudo useradd --system --user-group --create-home --home /opt/minecraft --shell /bin/bash minecraft

Switch over to the newly created user using the su command. All following steps should be executed as the minecraft user until indicated otherwise.

sudo su - minecraft

Create a few required directories (backups, server, utility) in the /opt/minecraft directory.

mkdir /opt/minecraft/{backups,server,utility}

Install PaperMC

As stated previously, PaperMC is an alternative Minecraft Server release that provides increased functionality. Should you wish to use the official release instead, the latest version can be found at minecraft.net/download/server.

Download PaperMC to the /opt/minecraft/server directory. The latest version can be found at papermc.io/downloads, and a list of download links for previous versions can be found at papermc.io/legacy. A few different versions are listed below. The rest of this bit will assume version 1.17.1 was used, but the process is the same for all modern releases.

# Minecraft 1.17.1
wget https://papermc.io/api/v2/projects/paper/versions/1.17.1/builds/385/downloads/paper-1.17.1-385.jar --directory-prefix /opt/minecraft/server
# Minecraft 1.16.5
wget https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/790/downloads/paper-1.16.5-790.jar --directory-prefix /opt/minecraft/server
# Minecraft 1.12.2
wget https://papermc.io/api/v2/projects/paper/versions/1.12.2/builds/1618/downloads/paper-1.12.2-1618.jar --directory-prefix /opt/minecraft/server

Rename the downloaded jar file to paper.jar for ease of use in commands and configuration.

mv /opt/minecraft/server/paper-1.17.1-385.jar /opt/minecraft/server/paper.jar

Start Paper to create the eula.txt and server.properties files. You will receive warning and error messages, this is expected behavior. The -DPaper.IgnoreJavaVersion=true flag is necessary because Paper does not officially support Java 17 yet.

java -Xmx1024M -Xms512M -DPaper.IgnoreJavaVersion=true -jar /opt/minecraft/server/paper.jar nogui

The output will resemble the following.

Loading libraries, please wait...
[02:22:20 ERROR]: Failed to load properties from file: server.properties
[02:22:20 WARN]: Failed to load eula.txt
[02:22:20 INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.

Install MCRCON

MCRCON is an implementation of the RCON (Remote CONsole) protocol designed specifically for Minecraft. MCRCON will allow you to execute console commands via scripts, as other system users, and from remote systems.

Create an mcrcon directory in the /opt/minecraft/utility directory. As the path suggests MCRCON will be installed here.

mkdir /opt/minecraft/utility/mcrcon

Download the latest release from MCRON’s GitHub repository. At the time of writing this was version 0.7.2. The most recent version can be found at github.com/Tiiffi/mcrcon/releases/latest.

wget https://github.com/Tiiffi/mcrcon/releases/download/v0.7.2/mcrcon-0.7.2-linux-x86-64.tar.gz --directory-prefix /tmp

Extract the downloaded release using tar to the /opt/minecraft/utility/mcrcon directory.

tar -xvf /tmp/mcrcon-0.7.2-linux-x86-64.tar.gz -C /opt/minecraft/utility/mcrcon

Set the permissions of the mcrcon directory so only the minecraft user / group can interact with the utility.

chmod -R 750 /opt/minecraft/utility/mcrcon

Configure Minecraft

With Minecraft installed on your server, its time to configure a few things.

EULA

Start by accepting the Minecraft EULA. Open the eula.txt file in your text editor of choice.

nano /opt/minecraft/server/eula.txt

After reading the Minecraft EULA, set eula=true.

#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).
#You also agree that tacos are tasty, and the best food in the world.
#Sun Nov 14 02:22:20 EST 2021
eula=true

Server Properties

Next up is the server.properties file. This is where you will find all the default configurable options for your server. Configuration for enabling RCON is detailed below as it relates to MCRCON. Full customization of this is outside of the scope of this bit, please refer to the Minecraft Wiki for more information on what each option does. Open the server.properties file in your text editor of choice.

nano server.properties

Find the lines relating to RCON and set them as follows. This will set the port, the password used to connect, and enable the remote console.

rcon.port=25575
rcon.password=GenerateSecurePassword
enable-rcon=true

Setup Backups

Backups are important for a Minecraft server, especially if you are using plugins or opening it up to the public. In this section we will automate the process of creating daily backups that are stored for one week.

Start by creating a backup script in the /opt/minecraft/utility directory.

nano /opt/minecraft/utility/backup.sh

Enter in the following template script and adjust it to suit your server. As written, the script will create a backup /opt/minecraft/server as an archive named server-YYYY-MM-DD-HH-MM.tar.gz and store it in /opt/minecraft/backups. It will also delete any tar.gz files in the backups directory that are older than 7 days.

  • Replace GenerateSecurePassword in the consolemc function to match the password set in the server.properties file.
  • Change the serverDir, backupDir, and dateFormat if necessary.
#!/bin/bash
# Minecraft Backup Script

# MCRCON Command
function consolemc {
  /opt/minecraft/utility/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p GenerateSecurePassword "$1"
}

# Server Directory
serverDir="/opt/minecraft/server"
# Backup Directory
backupDir="/opt/minecraft/backups"
# Date Format (YYYY-MM-DD-HH-MM)
dateFormat="date +%F-%H-%M"

# Disable Disk Writes
consolemc "save-off"
# Flush Pending Writes
consolemc "save-all"
# Create Backup Archive
tar -cpzf $backupDir/server-$dateFormat.tar.gz $serverDir
# Enable Disk Writes
consolemc "save-on"

# Delete Backups (> 7 days old)
find $backupDir -type f -mtime +7 -name "*.tar.gz" -exec rm {} \;

Change the permissions of the script so only the minecraft user can interact with it.

chmod 750 /opt/minecraft/utility/backup.sh

With the script setup, create a cron job to execute the script once a day. Open up the crontab to configure this.

crontab -e

Add the following to the bottom of the file. This will execute the script you just created everyday at 3:00 AM, and create a log of the most recent script execution in /opt/minecraft/utility/backup.log.

# Minecraft Backup Script
0 3 * * * /opt/minecraft/utility/backup.sh > /opt/minecraft/utility/backup.log 2>&1

Configure Server

Time to configure some server processes. With Minecraft installed and basic setup complete, logout of the minecraft user and switch back to your administrative account.

logout

Firewall

Before being able to connect to the server from a Minecraft game client, you must allow connections to port 25565.

sudo ufw allow 25565/tcp

Systemd

To better control the state of the Minecraft application, setup Minecraft as a service with a systemd unit file. Create the file using your text editor of choice.

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

Enter in the following configuration and adjust it to suit your server. For more information about systemd unit configuration, refer to the freedesktop.org documentation.

  • Verify the User= and WorkingDirectory= definitions are correct.
  • Set the flags Xmx1024M and Xms512M in ExecStart= based on your available system RAM. The Xmx flag is the maximum amount of RAM the Minecraft application can use, and the Xms flag is the minimum amount. To allow Minecraft to use 8GB of RAM you could use Xmx8192M and Xms2048M. Set the maximum value to be at least 1GB lower than the total amount of RAM available on your system.
  • Replace GenerateSecurePassword in ExecStop= to match the password set in the server.properties file.
[Unit]
Description=Minecraft
After=network.target

[Service]
User=minecraft
WorkingDirectory=/opt/minecraft/server
Nice=1
SuccessExitStatus=0 1
NoNewPrivileges=true
ProtectSystem=full
ProtectHome=true
PrivateDevices=true
ExecStart=/usr/bin/java -Xmx1024M -Xms512M -DPaper.IgnoreJavaVersion=true -jar paper.jar nogui
ExecStop=/opt/minecraft/utility/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p GenerateSecurePassword stop

[Install]
WantedBy=multi-user.target

Reload systemd so it is aware of the changes made.

sudo systemctl daemon-reload

The Minecraft application can now be controlled with the systemctl command. The restart directive will not work, use the stop and start commands separately if you with to restart the server.

# Start Minecraft
sudo systemctl start minecraft
# Stop Minecraft
sudo systemctl stop minecraft

Connect

The Minecraft server is now setup and ready to go. Start installing plugins, and configuring everything to your heart’s desire.

Console

To start a Minecraft console session with MCRCON, use the following command with the -t flag. Replace GenerateSecurePassword with the password set in the server.properties file.

/opt/minecraft/utility/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p GenerateSecurePassword -t

To issue a single command without opening a console session, enter the command you wish to run instead of the -t flag.

/opt/minecraft/utility/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p GenerateSecurePassword "stop"

Game Client

Spin up the Minecraft launcher and make sure to use the same game version as your server. Head over to the Multiplayer tab and select Add Server. In the Server Address field put the IP address of your server, or the domain name pointed at it, and select Done. If you configured any port besides 25565 in the server.properties file, you must specify that after the address. Your server will now be on the Play Multiplayer list, simply select it and hit Join Server.