How to create a Minecraft server on Ubuntu 20.04

s2 0

Minecraft is one of the most popular games of all time. It’s a sandbox video game where players explore infinite worlds and build various structures from simple houses to towering skyscrapers.

This tutorial explains how to create a Minecraft server on Ubuntu 20.04. We are using 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 cron job that performs regular server backups.

requirements

According to the official Minecraft site, 4GB of RAM is recommended as the minimum configuration for a typical setup.

Install the packages that are used to build the mcrcon Tool:

sudo apt updatesudo apt install git build-essential

Install the Java runtime environment

Minecraft requires Java 8 or higher. The Minecraft server doesn’t need a graphical user interface, so we’re installing the headless version of Java. This version is better suited for server applications because it has fewer dependencies and uses fewer system resources.

Run the following command to install the headless OpenJRE 11 package:

sudo apt install openjdk-11-jre-headless

Verify the installation by printing the Java version:

java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

Create Minecraft users

For security reasons, Minecraft should not run under the root user. We will create a new system user and a new group with a home directory /opt/minecraft. The user has the minimum required permissions to run the Minecraft server:

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

We will not set a password for this user. This way, the user cannot sign in using SSHand and cannot be compromised. To switch to minecraft user, you have to log into the server as root or user with sudo permissions.

Install Minecraft on Ubuntu

Before starting the installation process, switch to the minecraft User:

sudo su - minecraft

Run the following command to create three new directories in the user’s home directory:

mkdir -p ~/{backups,tools,server}
  • the backups Directory stores your Minecraft server backups. You can synchronize this directory with your remote backup server.
  • the tools Directory holds the mcrcon Client and the backup script.
  • the server The directory contains the actual Minecraft server and its data.

Download and compile mcrcon

RCON is a protocol that allows you to connect to Minecraft servers and execute commands. Micrometer
is an RCON client written in C.

We’ll download the source code from GitHub and create the mcrcon binary.

Clone the Tiiffi/mcrcon Repository from GitHub to the ~/tools/mcron Directory:

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

When you’re done, switch to the mcron Directory and create the utility:

cd ~/tools/mcrcongcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c

When done, check that mcrcon was successfully compiled by printing its version:

./mcrcon -v

The output looks something like this:

mcrcon 0.7.1 (built: Jun 23 2020 15:49:44) - https://github.com/Tiiffi/mcrcon
Bug reports:
	tiiffi+mcrcon at gmail
	https://github.com/Tiiffi/mcrcon/issues/

Download Minecraft Server

There are several Minecraft server mods such as Craftbukkit
or Cones
which enables you to add functions (plugins) to your server and to further adapt and optimize the server settings.

In this guide, we are going to install Mojang’s latest official vanilla Minecraft server. The same instructions apply to installing other server mods.

Go over to the Minecraft download page
to get the download link of the latest Java Archive (JAR) file from the Minecraft server. At the time of writing it is the latest version 1.16.

Download the JAR file from the ~/server Directory with wget
:

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

Configure Minecraft server

Once the download is complete, switch to the ~/server Directory and start the Minecraft server:

cd ~/serverjava -Xmx1024M -Xms1024M -jar server.jar nogui

When the server starts up for the first time, it performs some operations, creating the server.properties and eula.txt Files and stops.

[17:35:14] [main/ERROR]: Failed to load properties from file: server.properties
[17:35:15] [main/WARN]: Failed to load eula.txt
[17:35:15] [main/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.

In order to run the server, you’ll need to agree to the Minecraft EULA as stated in the output above. Open that eula.txt drop and change eula=false to eula=true:

nano ~/server/eula.txt
~ / server / eula.txt
eula=true

Close and save the file.

Next, open the server.properties File and activate the rcon protocol and set the rcon password:

nano ~/server/server.properties

Find the following lines and update their values ​​as shown below:

~ / server / server.properties
rcon.port=25575
rcon.password=strong-password
enable-rcon=true
Don’t forget to change them strong-password to something safer. If you don’t want to connect to the Minecraft server from remote locations, make sure the rcon port is blocked by your firewall.

Here you can also adjust the standard properties of the server. For more information on server settings, see the server.properties
Page.

Create systemd unit file

Instead of starting the Minecraft server manually, we create a systemd unit file and run Minecraft as a service.

Switch back to your sudo user by tapping exit.

Open your text editor and create a file called minecraft.service by doing /etc/systemd/system/ Directory:

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 -Xms1024M -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 you have the correct one rcon Port and password.

Save the file and reload the systemd manager configuration:

sudo systemctl daemon-reload

You can now start the Minecraft server by typing:

sudo systemctl start minecraft

When you first start the service, several configuration files and directories are generated, 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 Tue 2020-06-23 17:48:44 UTC; 8s ago
   Main PID: 1338035 (java)
      Tasks: 15 (limit: 1074)
     Memory: 465.3M
     CGroup: /system.slice/minecraft.service
             └─1338035 /usr/bin/java -Xmx1024M -Xms1024M -jar server.jar nogui

Finally, enable the Minecraft service to start automatically at boot:

sudo systemctl enable minecraft

Adjust firewall

Ubuntu comes with a firewall configuration tool called UFW. If the firewall is enabled on your system and you want to access the Minecraft server from outside your local network, you will need to open the port 25565:

sudo ufw allow 25565/tcp

Configure backups

In this section we will create a backup shell script and cron job to automatically backup the Minecraft server.

Switch to 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-%H-%M).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:

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

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

Open the crontab file by typing:

crontab -e

To run the backup script at 11:00 p.m. every day, add the following line:

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

Access to the Minecraft console

To access the Minecraft console, use the mcrcon Utility. You have to specify the host, the rcon port, the rcon password and the -t Switch that 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!
> 

If you are accessing the Minecraft console from a remote location, make sure that the rcon port is not blocked.

If you connect to the Minecraft console regularly, instead of typing that long command, create a bash alias.

diploma

We showed you how to create a Minecraft server on Ubuntu 20.04 and set up a daily backup.

You can now start yours Minecraft client
, connect to the server and start your Minecraft adventure.

If you run into any problem or have any feedback, please leave a comment below.