LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Roll Your Own: How to Setup and Maintain a Minecraft Server on Your VPS

Minecraft ServerIn this tutorial, we’ll show you how to setup a Minecraft game server on a Ubuntu VPS. But we won’t stop at just starting the server but also will cover

  • setting it up in systemd so it automatically starts/stops with the VPS boot
  • securing your Minecraft server with whitelisting
  • maintaining logs
  • backing up your Minecraft server so you can roll back to earlier versions

Prepping the Server

I’m using a fresh Ubuntu 25.04 server with 2GB of RAM.  As info, I’ve run a this size system (with Minecraft capped at 1.2GB of RAM) for 3-4 simultaneous players and have never had an issue. If you expect to host more players than that, you may want more RAM.

We’ll be using Ubuntu 25.04 because it ships with OpenJDK 21, which is the minimum for the current version of the Minecraft Java server as of this writing.

After imaging the VM, install the Java Runtime Environment required to run Minecraft, as well as wget on the off chance your particular Ubuntu setup doesn’t come with it:

apt -y install openjdk-25-jre-headless wget

There is no reason to run the Minecraft server as root, so we’ll create an unprivileged users called (cleverly) “minecraft”:

useradd -m -s /bin/bash minecraft && passwd minecraft

I’m going to setup things in /minecraft, so create that directory and make it owned by user minecraft.  Then we’ll swich to the minecraft account:

mkdir /minecraft
chown minecraft:minecraft /minecraft
su - minecraft

Downloading the Minecraft Software

Visit the Minecraft server page and you should see a page like the one shown below. Make note of hte “Download minecraft_server_VERSION.jar” link and copy it. Because the server is periodically uploaded, you’ll need to copy the link from this page rather than copying it from this tutorial.

Now execute:

cd /minecraft
# replace this URL with one from the Minecraft server page
wget https://piston-data.mojang.com/v1/objects/6bce4ef400e4efaa63a13d5e6f6b500be969ef81/server.jar

Let’s tell the server we read the EULA so it doesn’t ask us:

echo "eula=TRUE" > /minecraft/eula.txt

Now we’ll create a couple start/stop scripts. Put hte following in /minecraft/server_start.sh:

#!/bin/bash 
cd /minecraft
/usr/bin/java -Xms512M -Xmx1228M -jar server.jar nogui

Note the -Xms and X-mx parameters. Those are “minimum RAM for Java” and “maximum RAM for Java”. In this case, we’re saying “you get a minimum of 512MB and a maximum of 1228MB” (approximately 1.2GB). You should adjust these for your particular environment and needs.

Place the following in server_stop.sh:

#!/bin/bash
kill $(cat /minecraft/server.pid)

That command simply says “kill the process ID that the minecraft recorded as its process ID”.

Make both scripts executable:

chmod 755 /minecraft/start_server.sh
chmod 755 /minecraft/stop_server.sh

Then type “exit” to get out of your su session and go back to root.

Put the following in /etc/systemd/system/minecraft.service:

[Unit]
Description=Minecraft Server
After=network.target

[Service]
ExecStart=/minecraft/server_start.sh
ExecStop=/minecraft/server_stop.sh
Restart=always
PIDFile=/minecraft/server.pid
TimeoutStartSec=30
User=minecraft
Group=minecraft

[Install]
WantedBy=multi-user.target

Open the Minecraft port on your firewall:

ufw allow 25565

Now start the Minecraft server:

systemctl daemon-reload
systemctl start minecraft

After a few moments, a server.properties files and logs will be created in /minecraft.

Securing Minecraft

To prevent any random Internet user from joining your Minecraft, you can setup a whitelist so that only people you designate are allowed to join.

First, edit your /minecraft/server.properties file and change this line to true:

white-list=true

Next, gather the username and UUID from the players you want to join. Players can find their UUIDs via this guide.
https://www.minecraftforum.net/forums/support/java-edition-support/2392452-finding-your-uuid-using-your-minecraft-client

Next, edit /minecraft/whitelist.json and include those you wish to allow. Here is an example:

[
  {
    "uuid": "852d0812-f394-4cb8-91f4-fa5161ab0f3b",
    "name": "example1"
  },
  {
    "uuid": "ff042255-2be5-41b6-8404-f1899e3dcef5",
    "name": "example2"
  }
]

Now restart your Minecraft server:

systemctl restart minecraft

Maintaining Logs

The logs in /minecraft/logs will rotate automatically, so you don’t need to manage that. Over 3 months, I find that logs are a few MB at most. However if you want to prune old logs, you can add a job line this in your crontab. In this example, all logs older than 30 days will be removed every night at 1am. The MAILTO directive will send all output (both stdout and stderr) to that email address.

MAILTO=someone@who.cares
0 1 * * * find /minecraft/logs -type f -mtime +30 -exec rm -f {} ;

Backing Up Your Minecraft Server

The script below will backup your Minecraft server so that if something bad happens, you can roll back to an earlier day simply by removing /minecraft and replacing it with the contents of a backup tarball.  It works by stopping Minecraft, creating a compressed tar of the /minecraft directory, and then restarting Minecraft.

First, create a backup directory:

mkdir /minecraft_backups
chown minecraft:minecraft /minecraft_backups

Then save this script as /minecraft_backups/backup_minecraft.sh. Update EMAIL to point to your email address and set DAYS_RETAIN to how many days back you want to save backups:

#!/bin/bash

EMAIL=someone@who.cares
DAYS_RETAIN=30

MINECRAFT_DIR=/minecraft
BACKUP_DIR=/minecraft_backups
LOG="/minecraft_backups/backup_minecraft.`date '+%Y%m%d'`.log"

echo "`date` starting" > $LOG
systemctl stop minecraft >> $LOG 2>&1
rc=$?
echo "`date` minecraft stop rc: $rc" >> $LOG
if [ $rc -ne 0 ] ; then
  echo "ERROR: could not stop minecraft!" >> $LOG
  mailx -s "backup minecraft failed" $EMAIL < $LOG
  exit 1
fi

tarfile="${BACKUP_DIR}/minecraft.`date '+%Y%m%d'`.tar.gz"
echo "`date` tarfile is $tarfile" >> $LOG
tar czf ${tarfile} ${MINECRAFT_DIR} >> $LOG 2>&1
ls -l ${tarfile} >> $LOG 2>&1

systemctl start minecraft >> $LOG 2>&1
rc=$?
echo "`date` minecraft start rc: $rc" >> $LOG
if [ $rc -ne 0 ] ; then
  echo "ERROR: could not start minecraft!" >> $LOG
  mailx -s "backup minecraft failed" $EMAIL < $LOG
  exit 1
fi

echo "`date` here are backup files to be removed:" >> $LOG
find ${BACKUP_DIR} -mtime +${DAYS_RETAIN} -print >> $LOG 2>&1
echo "`date` cleaning up files" >> $LOG
find ${BACKUP_DIR} -mtime +${DAYS_RETAIN} -exec rm -f {} ; >> $LOG 2>&1
echo "`date` finished" >> $LOG

Make it executable:

chmod 755 /minecraft_backups/backup_minecraft.sh

Here is an example of the log output from a backup:

$ cat /minecraft_backups/backup_minecraft.20200513.log 
Wed 13 May 2020 02:00:01 AM PDT starting
Wed 13 May 2020 02:00:01 AM PDT minecraft stop rc: 0
Wed 13 May 2020 02:00:01 AM PDT tarfile is /minecraft_backups/minecraft.20200513.tar.gz
tar: Removing leading `/' from member names
-rw-r--r-- 1 root root 195796750 May 13 02:00 /minecraft_backups/minecraft.20200513.tar.gz
Wed 13 May 2020 02:00:08 AM PDT minecraft start rc: 0
Wed 13 May 2020 02:00:08 AM PDT here are files to be removed:
-rw-r--r-- 1 root root 195796630 Apr 12 02:00 /minecraft_backups/minecraft.20200412.tar.gz
Wed 13 May 2020 02:00:08 AM PDT cleaning up files
Wed 13 May 2020 02:00:08 AM PDT finished

To set this job to run automatically, put the following in your crontab.

0 2 * * * /minecraft_backups/backup_minecraft.sh

No Comments

    Leave a Reply

    Some notes on commenting on LowEndBox:

    • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
    • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
    • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

    Your email address will not be published. Required fields are marked *