Linux

How to Make a Minecraft Server on Ubuntu 20.04

0
How to Make a Minecraft Server on Ubuntu 20.04

Minecraft is one of the most popular games of all time. This is a video game sandbox where players explore a world without limits and build different structures from simple houses to towering skyscrapers.

This tutorial explains how to create a Minecraft Server on Ubuntu 20.04. We will use Systemd to run the Minecraft server and the mconcon utility to connect to the running instance. We will also show you how to create a cronjob that performs regular server backups.

Precondition

According to the official Minecraft website, 4GB RAM is recommended as a minimum configuration for typical settings.

Install the packages needed to build the mcrcon tool:
sudo apt update
sudo apt install git build-essential

Install the Java Runtime Environment

Minecraft requires Java 8 or higher. The Minecraft server does not require a graphical user interface, so we will install a headless version of Java. This version is more suitable for server applications because it has less dependency and uses less 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

Output :

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)

Creating Minecraft Users

For security reasons, Minecraft may not run under the root user. We will create a new user and group system with the home / opt / minecraft directory. Users will have the minimum permissions needed 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. In this way, users will not be able to log in via SSHand cannot be compromised. To change to a minecraft user, you must log in to the server as root or as a user with sudo privileges.

Install Minecraft on Ubuntu

Before starting with the installation process, switch to minecraft users:

sudo su - minecraft

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

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

Download and Compile Mccon

RCON is a protocol that allows you to connect to a Minecraft server and run commands. mcron is an RCON client written in C.

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

Tiiffi / mcrcon repository clones from GitHub to the ~ / tools / mcron directory:

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

When finished, switch to the mcron directory and build the utility:
cd ~/tools/mcrcon
gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c

When finished, verify that the mcrcon has been successfully compiled by printing its version:

./mcrcon -v

The output will look like this:

Output :

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 Spigot that allow you to add features (plugins) on your server and further adjust and change server settings.

In this guide, we will install the latest official Mojang Minecraft vanilla server. The same instructions apply for installing other mod servers.

Open the Minecraft download page to get the download link from the latest Minecraft server Java (JAR) archive file. At the time of writing, the latest version is 1.16.

Download the jar file in the ~ / server directory with wget:

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

Configuring a Minecraft Server

After the download is complete, switch to the ~ / server directory and start the Minecraft server:
cd ~/server
java -Xmx1024M -Xms1024M -jar server.jar nogui

When it starts for the first time, the server performs several operations, creates the server.prop 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.

To run the server, you must agree to the Minecraft EULA, as indicated by the output above. Open the eula.txt file 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 line and update the value, as shown below:

~/server/server.properties
rcon.port=25575
rcon.password=strong-password
enable-rcon=true

Don’t forget to change a strong password into something safer. If you don’t want to connect to a Minecraft server from a remote location, make sure the Rcon port is blocked by your firewall.

While here, you can also adjust server default properties. For more information about server settings, visit server.properties page.

Creating a Systemd Unit File

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

Switch back to your sudo user by typing exit.

Open your text editor and create a file called minecraft.service in the / 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

Change the Xmx and Xms flags according to your server’s resources. The Xmx flag defines the maximum memory allocation pool for Java virtual machines (JVM), while Xms defines the initial memory allocation pool. Also, make sure you are using the correct port and Rcon password.

Save the file and reload the system manager configuration:

sudo systemctl daemon-reload

You can now start the Minecraft server by typing:

sudo systemctl start minecraft

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

Check the service status with the following command:

sudo systemctl status minecraft

Output :

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 automatically start at boot:

sudo systemctl enable minecraft

Adjust Firewall

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

sudo ufw allow 25565/tcp

Configure Backup

In this section, we will create a backup shell script and cronjob to automatically back up 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 once a day automatically at a fixed time.

Open the crontab file by typing:

crontab -e

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

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

Access the Minecraft Console

To access the Minecraft Console, use the mcrcon utility. You need to specify the host, rcon port, rcon password and use -t which allows the mcrcon terminal mode:

/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password -t

Output :

Logged in. Type "Q" to quit!
> 

When accessing the Minecraft Console from a remote location, ensure that the port is not blocked.

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

Conclusion

We have shown you how to create a Minecraft server on Ubuntu 20.04 and manage daily backups.

You can now launch your Minecraft client, connect to a server, and start your Minecraft adventure.

If you experience problems or get feedback, leave a comment below.

15 Best Retailer Apps To Make Money Online (Android & iOS)

Previous article

15 Best Money Transfer Apps For Android & iOS

Next article

You may also like

More in Linux