In this tutorial, we’ll show you how to setup a Minecraft game server on a Debian 10 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 Debian 10 server with 2GB of RAM. As info, I’ve run an identically sized Minecraft server (with Minecraft capped at 1GB 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.
After imaging the VPS, install the Java Runtime Environment required to run Minecraft, as well as wget on the off change your particular Debian setup doesn’t come with it:
apt-get update apt-get -y install openjdk-11-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 wget the_url_you_copied_goes_here
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 -Xmx1024M -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 1024MB”. 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 /lib/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
Now start the Minecraft server:
systemctl daemon-reload systemctl start minecraft
Take a look in /minecraft/logs/latest.log and you’ll see the server starting up:
[09:43:34] [main/ERROR]: Failed to load properties from file: server.properties [09:43:41] [main/WARN]: Ambiguity between arguments [teleport, destination] and [teleport, targets] with inputs: [Player, 0123, @e, dd12be42-52a9-4a91-a8a1-11c01849e498] [09:43:41] [main/WARN]: Ambiguity between arguments [teleport, location] and [teleport, destination] with inputs: [0.1 -0.5 .9, 0 0 0] [09:43:41] [main/WARN]: Ambiguity between arguments [teleport, location] and [teleport, targets] with inputs: [0.1 -0.5 .9, 0 0 0] [09:43:41] [main/WARN]: Ambiguity between arguments [teleport, targets] and [teleport, destination] with inputs: [Player, 0123, dd12be42-52a9-4a91-a8a1-11c01849e498] [09:43:41] [main/WARN]: Ambiguity between arguments [teleport, targets, location] and [teleport, targets, destination] with inputs: [0.1 -0.5 .9, 0 0 0] [09:43:42] [Server thread/INFO]: Starting minecraft server version 1.15.2 [09:43:42] [Server thread/INFO]: Loading properties [09:43:42] [Server thread/INFO]: Default game type: SURVIVAL [09:43:42] [Server thread/INFO]: Generating keypair [09:43:42] [Server thread/INFO]: Starting Minecraft server on *:25565 [09:43:42] [Server thread/INFO]: Using epoll channel type [09:43:43] [Server thread/INFO]: Preparing level "world" [09:43:43] [Server thread/INFO]: Found new data pack vanilla, loading it automatically [09:43:43] [Server thread/INFO]: Reloading ResourceManager: Default [09:44:30] [Server thread/INFO]: Loaded 6 recipes [09:44:30] [Server thread/INFO]: Loaded 825 advancements [09:44:39] [Server thread/INFO]: Preparing start region for dimension minecraft:overworld [09:44:39] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:40] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:40] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:41] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:41] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:42] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:42] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:43] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:43] [Server-Worker-1/INFO]: Preparing spawn area: 0% [09:44:44] [Server-Worker-1/INFO]: Preparing spawn area: 1% [09:44:44] [Server-Worker-1/INFO]: Preparing spawn area: 1% [09:44:45] [Server-Worker-1/INFO]: Preparing spawn area: 1% [09:44:45] [Server-Worker-1/INFO]: Preparing spawn area: 2% [09:44:46] [Server-Worker-1/INFO]: Preparing spawn area: 3% <lines trimmed> [09:45:31] [Server-Worker-1/INFO]: Preparing spawn area: 96% [09:45:31] [Server-Worker-1/INFO]: Preparing spawn area: 98% [09:45:31] [Server thread/INFO]: Time elapsed: 52243 ms [09:45:31] [Server thread/INFO]: Done (108.563s)! For help, type "help"
The “ERROR” about no server.properties just means that no server.properties file existed. One is created automatically.
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
Related Posts:
- One Week From Tomorrow…THE WORLD WILL LOSE THEIR MINDS!Lines Are Already Forming! - November 21, 2024
- Crunchbits Discontinuing Popular Annual Plans – The Community Mourns! - November 20, 2024
- RackNerd’s Black Friday 2024: Bigger, Better, and Now in Dublin! - November 19, 2024
is this better than buying specific minecraft server??
Running a Minecraft server on your own VPS gives you more control and the ability to run more than one server on your VPS. It can also be less expensive than a dedicated game server service.
You should really talk about LGSM (Linux Game Server Manager) which is a great tool to host a lot of game servers.
Thanks for the suggestion, we will look into that.
I am looking to create my own Minecraft server and this will definitely help, thank you!