Hearing my colleagues Charlie & James talking about building a Minecraft server, and James saying that he never built one on Linux made me put this guide together. This guide was redone from another that I can’t find the link to anymore, shoutout to whoever built it initially. Let’s grab a coffee and start building :)

You can go with any Linux flavour really, I went with Debian Bookworm.

1. SSH to our server:

We’ll check first if there is a swap file or swap partition in use:

swapon -s

If so, feel free to go to the next task. Otherwise, set up a swap file of 2GB to smooth system operations:

fallocate -l 2g /swap
chmod 0600 /swap
mkswap /swap
swapon /swap
echo '/swap    none swap defaults 0 0' | tee -a /etc/fstab
free -m

Optionally we can harden the system and change root password:

echo 'root:SuperSecretPassword' | chpasswd && history -d -1

Create a non-root user account with sudo privileges, such as minecraft, and then also set up a strong password.

useradd -ms /bin/bash minecraft
echo 'minecraft:SuperSecretPassword' | chpasswd && history -d -1
echo 'minecraft    ALL=(ALL)   NOPASSWD: ALL' | tee -a /etc/sudoers.d/localusers
chmod 0440 /etc/sudoers.d/localusers

Setup UFW firewall rules to allow only traffic on SSH (22) and the default Minecraft (25565) ports:

apt install ufw -y
ufw default deny
ufw allow 22
ufw allow 25565
ufw enable
ufw status verbose

Update the system, and then reboot:

apt update
apt upgrade -y
apt autoremove -y
reboot

After the server instance gets up and running again, log in as the newly created sudo user minecraft for the following tasks.

2. Install OpenJDK 21

Minecraft Java Edition server 1.21.x and above requires Java 21. Among various Java 21 distributions, this article chooses to install openJDK binaries:

Additional reference to install Java from here.

Make sure required packages are in place:

sudo apt install vim wget apt-transport-https -y

Download the openJDK 21 package:

wget https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2_linux-x64_bin.tar.gz

Extract the package:

tar xvf openjdk-21.0.2_linux-x64_bin.tar.gz

Move the binary in place:

sudo mv jdk-21.0.2/ /usr/local/jdk-21

Setup the JAVA_HOME environment variable, and add openJDK PATH environment variable:

echo "export JAVA_HOME=/usr/local/jdk-21" | sudo tee -a /etc/profile && source /etc/profile
echo "export PATH=$PATH:$JAVA_HOME/bin" | sudo tee -a /etc/profile && source /etc/profile
echo $JAVA_HOME
echo $PATH

Confirm the installation of OpenJDK:

java --version

Above should return something like this:

openjdk 21.0.2 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13-58)
OpenJDK 64-Bit Server VM (build 21.0.2+13-58, mixed mode, sharing)

3. Install Minecraft Java Edition Server

Create a directory for Minecraft Java Edition server:

sudo mkdir /srv/minecraft
sudo chown minecraft:minecraft /srv/minecraft

Download Minecraft Java Edition server using the download link found from the official Minecraft Java Edition Server Download Page.

cd /srv/minecraft
wget https://piston-data.mojang.com/v1/objects/4707d00eb834b446575d89a61a11b5d548d8c001/server.jar -O minecraft_server.1.21.4.jar

Create a text file to confirm that you agree to the Minecraft end-user license agreement:

echo "eula=true" > /srv/minecraft/eula.txt

Test drive Minecraft Java Edition server:

java -Xms2048M -Xmx2048M -jar /srv/minecraft/minecraft_server.1.21.4.jar nogui

If the server starts with no issues we should see the following line at the bottom of the start sequence:

...
[06:59:58] [Server thread/INFO]: Done (62.240s)! For help, type "help"

Note: In the previous command, the -Xms2048M and -Xmx2048M flags define initial and maximum memory allocation values for the Minecraft Java Edition server. Both are 2048M in this case. The two values are suitable for a Debian 11 server instance with 4GB of memory. Consider increasing the values if your server instance has more memory.

When you see the server prompts Done!, which means it is up and running:

  1. Launch your Minecraft client.
  2. Add a server name and the server’s IPv4 address, such as 192.168.0.200 for example.
  3. Join the server to explore the newly created world.

After the test drive, press Ctrl+C in the SSH terminal to stop Minecraft Java Edition server.

To customize your Minecraft Java Edition server, you need to edit a newly generated file named server.properties within the Minecraft Java Edition server directory.

For example, if you want to allow non-premium players to log in to your Minecraft server:

vim /srv/minecraft/server.properties

Find the line:

online-mode=true

Change it to:

online-mode=false

To learn more about configuring Minecraft server properties, visit the Minecraft Wiki page.

4. Install Supervisor

To keep the Minecraft Java Edition server running, it’s recommended to use the Supervisor program to start and restart the Minecraft Java Edition server processes automatically.

Install the Supervisor program:

sudo apt install supervisor -y
supervisord -v

Start the Supervisor service:

sudo systemctl daemon-reload
sudo systemctl enable --now supervisor.service

Create a Supervisor configuration file for the Minecraft Java Edition server:

sudo vim /etc/supervisor/conf.d/minecraft.conf

Populate the file with:

[program:minecraft]
directory=/srv/minecraft/
command=/usr/local/jdk-21/bin/java -Xms2048M -Xmx2048M -jar /srv/minecraft/minecraft_server.1.21.4.jar nogui
user=minecraft
autostart=true
autorestart=true
stderr_logfile=/var/log/supervisor/error_minecraft.log
stderr_logfile_maxbytes=100MB
stdout_logfile=/var/log/supervisor/out_minecraft.log
stdout_logfile_maxbytes=100MB

Load the Minecraft Java Edition server configuration file:

sudo supervisorctl reread
sudo supervisorctl update

Determine if the Minecraft Java Edition server is up and running:

tail -f /var/log/supervisor/out_minecraft.log

Below output should be seen from the log:

...
[08:15:49] [Server thread/INFO]: Done (123.024s)! For help, type "help"

Press Ctrl+C to quit the tail program at any time.

Create a service to for the server:

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

with the following config:

[Unit]
Description=Minecraft Server
After=network.target
[Service]
User=minecraft
Nice=1
KillMode=mixed
SuccessExitStatus=0 1
ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
WorkingDirectory=/srv/minecraft
ExecStart=/usr/local/jdk-21/bin/java -Xmx2048M -Xms2048M -jar minecraft_server.1.21.4.jar nogui

[Install]
WantedBy=multi-user.target

Once the service file has been created we need to reload the daemons and start the service.

sudo systemctl daemon-reload
sudo systemctl start minecraft
sudo systemctl status minecraft

Enjoy peeps! creeper