Self-hosting a Minecraft server on Linux
First of all, this is not the recommended route for 99% of people that just want to play some Minecraft with friends. There are a lot of reliable and proven hosting companies out there that will do just fine for this. But if you want to set up a scaleable, modern and cheap Minecraft server while also learning a thing or two about linux server administration, this article is for you.
Step 1: Setup
The first step is getting a VPS up and running. Prices for this will vary depending on your location, but in general you should get one with at least 4Gb of RAM, otherwise your server might crash with as little as two players(I’ve tried this myself). Also, try to pick a location that’s physically close to you, or if your host supports it, check your ping to the location beforehand. If you are anywhere in Germany I can only recommend Hetzner for this. They offer great performance for the price and are very reliable. Their CX21 instances for 5,68€ per month should be fine for at least 8 players.
The next steps will assume you are logged in with an SSH session as root.
Step 2: Installing Java
Your server probably does‘t have Java installed, so we have to do it manually. Be sure to install at least version 8 or later.
apt update && apt upgrade
apt install openjdk-11-jre-headless
Step 3: Create user
To add some security to our setup we create a second user called “minecraft”. The server process will be ran by it, so in case an attacker manages to get access to the server via something like a bug in the server software, the rest of the system will still be fine.
We will also store all server-related files in the folder /var/minecraft
.
groupadd -r minecraft
mkdir -p /var/minecraft/server
useradd -r -g minecraft -d "/var/minecraft" -s "/bin/bash" minecraft
Step 4: Installing PaperMC
Here you could also use the Mojang-provided server, but for performance reasons we are using PaperMC. It’s not only faster than using a vanilla server, it also supports plugins and blocks x-ray hacks out of the box.
We are going to download the PaperMC jar file to the /var/minecraft/server
directory. You can get a link to the most recent version from their website.
cd /var/minecraft/server
wget -O paper.jar https://papermc.io/api/v1/paper/1.16.3/latest/download
# let the minecraft user access the files
chown minecraft:minecraft -R /var/minecraft
Now we can try starting the server as the minecraft user. This should generate an error message prompting us to agree Mojang’s EULA.
su minecraft
cd /var/minecraft/server
java -jar paper.jar
You should now see a “eula.txt” file in the server directory. Edit it and replace false with true in the last line. Now you can try starting the server again and you should see the server starting to generate the world. Let it finish, then stop it.
Now we need to adjust some settings. In order for us to be able to stop the server gracefully and without losing data, we will need to enable rcon access.
enable-rcon=true
rcon.password=yourRconPassword
#You don't have to change it, but I would recommend setting the view distance to a value between 6 and 8 for better performance
view-distance=8
I would also suggest changing your query.port
and your server-port
to a different value. This way your server is less likely to be found by people trying to find minecraft servers by malicious port scanning.
For performance reasons I would recommend you to also change your view-distance
to something between 6 to 8, as the server will have to load way less chunks this way.
You can go back to the root prompt by typing exit
.
Step 5: Setting up mcrcon
In order to prevent data loss while shutting down the server, we will send a “stop”-signal to the server to shut it down. This will give the server enough time to kick players and write the world data to disk.
For us to be able to send a command to the server instance, we need to use an rcon-client. We will be using mcrcon for this.
We will get the latest version as a release from Github and put it in the /var/minecraft
directory.
cd /var/minecraft
wget -O mcrcon.tar.gz https://github.com/Tiiffi/mcrcon/releases/download/v0.7.1/mcrcon-0.7.1-linux-x86-64.tar.gztar xvzf mcrcon.tar.gz
mv mcrcon-0.7.1-linux-x86-64/mcrcon ./
chmod +x ./mcrcon
We can verify the install now.
/var/minecraft/mcrcon -v
If the version of mcrcon is displayed, everything should work fine. As a last step we will make sure all files can be properly accessed by the minecraft
user.
chown minecraft:minecraft -R /var/minecraft/
Step 6: Auto-restarting with systemd
To make sure the Minecraft server gets started whenever the actual server starts, we have to create a systemd file. Here we define where the files for the server are located, as which user it is executed, that it should restart automatically in case of an error and how to stop it properly (with mcrcon). It also sets up a few important security measures, for example protecting the rest of the system in case an attacker manages to get access to it.
Create a file called minecraft.service
and paste the following into it:
Now we have to move it to a place where systemd, the program which runs other programs at startup, is able to find it. Then we can start the service and “enable” it so it always runs at system startup.
cp minecraft.service /etc/systemd/system
systemctl daemon-reload
systemctl start minecraft.service
systemctl enable minecraft.service
You can view the live log of your Minecraft server using journalctl
journalctl -u minecraft.service -f
Step 7: Setup a firewall
To prevent malicious access to our server, we will restrict the ports at which it can be accessed with a firewall. You have to enable the SSH-port otherwise you will lock yourself out of the server, and also the port of your Minecraft server. The rcon-port should not be accessible from outside the system.
apt install ufw
ufw allow ssh
ufw allow (The port of your server)
ufw enable
If you did everything correctly, you should still have access to the system. To check if the firewall is working correctly, use the status
command.
ufw status
Conclusion
It’s relatively straightforward to setup a modern minecraft server, and definitely an interesting way to learn about linux system administration. I’ve been using this setup for about a week now and so far it’s working great. If you have any questions, feel free to ask in the comments.
I also tried using fabric with Phosphor and Lithium, but the RAM usage was noticeably higher, so I went with PaperMC instead.
Addendum
There are two important things i didn’t mention in this article: backups and permission management. Permission management is not needed for our server, as we are playing completely vanilla and no one is given operator rights. Backups are handled by our hosting company, Hetzner, who have a simple option for it in the backend.
I’m currently working on jumpstarting Spacifik, a web agency startup based in Berlin. If you need a custom-made, fresh, fast and customer focused website, hit us up at spacifik.de.