Set up a Minecraft server on Ubuntu 20.04 LTS

ohhh, fun time

So, we will be talking about the most awesome game ever made...well maybe not ever, but the game is pretty cool in my opinion :)
Minecraft Server on Ubuntu 20.04 LTS edition.
Let's get strait into the build process:

First, we need a fresh installation on Ubuntu 20.04 LTS. Depending on where do you host your machine or VM, start with the installation, when you are redy come back to the next step here.

  1. Standard update of fresh system:
sudo apt update 
sudo apt upgrade -y
  1. Time to install some dependencies:
sudo apt-get install git build-essential -y
  1. Java is the main element here, install using this command:
sudo apt-get install openjdk-11-jre-headless -y
  1. Check the version of java just installed:
java --version

You should see something like:

OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

If yes, all is okay. If you don't see similar output check if the installation of Java was successful.

  1. Now we will need to install the Minecraft server, few steps here:

Create a user to use by the Minecraft server:

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


Next, switch to the user:

su - minecraft


Create the required directories for the Server:

mkdir ~/backups ~/tools ~/server


Now we will need to install mcrcon, it will be used to connect to your Minecraft Server, download it to the 'tools' directory just created in previouse step:

git clone https://github.com/Tiiffi/mcrcon.git ~/tools/mcrcon
cd ~/tools/mcrcon
gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c


Now the actual Minecraft files, we need to download and configure:

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


Now change the directory to the 'server' one and run the following command:

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


This should display an error like this:

[16:09:46] [main/ERROR]: Failed to load properties from file: server.properties
[16:09:47] [main/WARN]: Failed to load eula.txt
[16:09:47] [main/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.


If you see the same, that mean that all is okay and we need now to agree to the Minecraft EULA, edit the eula.txt file in the server directory:

vim ~/server/eula.txt


change the eula line from false to true and save the file:

eula=true


Now we need to edit the properties file and set the connection password:

vim ~/server/server.properties


Edit the rcon.password line as set your password:

rcon.password=your-password
enable-rcon=true 


We are almost there. Hold on :)

  1. Now we need to create a systsemd process, let's start with the symlink:
    Create the file:
sudo vim /etc/systemd/system/minecraft.service


Paste into the new file:

[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 your-password stop

[Install]
WantedBy=multi-user.target


Before we continue, just a quick explanation here:
The line 'ExecStart=/usr/bin/java -Xmx1024M -Xms1024M -jar server.jar nogui' has some setting that nees to be adjusted depending on your machine. The value '-Xmx' is the max values of RAM that the server is allowed to use, and the '-Xms' is the values of RAM that the server starts with, like a minimum value. Adjust depending on your server.
You will need to replace 'your-password' with the password you set few steps before.

Now we need to reload the systemd daemon:

sudo systemctl daemon-reload


Next, start the Minecraft Server and enable the service:

sudo systemctl start minecraft
sudo systemctl enable minecraft


You can check the ststaus of the server by running:

sudo systemctl status minecraft



Your Minecraft server is now running on your server on port 25575



To connect under console to the server use this command (replace your-password with the password you set for the server):

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


\
Enjoy!