Setup Minecraft Server

Table of contents

Setup your VPS

DigitalOcean Referral Badge

I use Digital Ocean. Create project, droplet. Log into VPS as root using ssh. (should be using key)

$ ssh root@serverip

Add user

$ adduser user

Grant administrative privilege

$ usermod -aG sudo user

SSH

Change SSH port

$ vim /etc/ssh/sshd_config

Uncomment the #Port 22 line and change it to what port you want.

Copy the .ssh folder to your users home folder to be able to log in from that account:

$ rsync --archive --chown=user:user ~/.ssh /home/user

Now you can log out of root and log in using your new user.

Update

After successful login, update the system:

$ sudo apt update && sudo apt upgrade

Set Timezone

$ sudo timedatectl list-timezones

$ sudo timedatectl set-timezone TIMEZONE

Edit sshd config:

$ sudo vim /etc/ssh/sshd_config

Change PermitRootLogin yes to no.

Probably a good idea to reboot at this point

$ sudo reboot

Firewall

Change firewall rules:

$ sudo ufw default deny incoming

$ sudo ufw allow SSHPORT

$ sudo ufw enable

Install logwatch

$ sudo apt install logwatch

Then edit config at /usr/share/logwatch/default.conf/logwatch.conf and change Mailto = user and Detail = High.

Install Fail2ban

Install:

$ sudo apt get install fail2ban

Edit config:

$ cd /etc/fail2ban

$ sudo cp jail.conf jail.local

Replace the [sshd] section with this: (making sure to change the port for your ssh port)

[sshd]
# To use more aggressive sshd modes set filter parameter "mode" in jail.local:
# normal (default), ddos, extra or aggressive (combines all).
# See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details.
mode     = aggressive
enabled  = true
port     = SSHPORT
maxretry = 3
findtime = 300
bantime  = 72h
logpath  = %(sshd_log)s
backend  = %(sshd_backend)s

Start and enable fail2ban:

$ sudo systemctl start fail2ban

$ sudo systemctl enable fail2ban

Install Zsh and Oh My Zsh (optional, personal preference)

(This is just for my personal preference, its not needed…but I’m gonna document it for myself)

Check if you have git installed, if not download it.

$ sudo apt install git

Clone dotfiles

$ mkdir git

$ cd git

$ git clone https://github.com/unclassedpenguin/dotfiles.git

Use install script included in dotfiles.

$ ./install.sh

Then exit and log back in for changes to take effect.

I think thats it for setting up the vps… Now onto the minecraft server.


Setting up the Minecraft Server

Download Server

Create a minecraft folder

$ mkdir minecraft

$ cd minecraft

Now we need to download the minecraft server. This will change, depending on the version. Get the url from here. Right click on the download button for the ubuntu minecraft server and copy the url, and then use wget to download it.

$ wget https://minecraft.azureedge.net/bin-linux/bedrock-server-1.20.72.01.zip

Install unzip:

$ sudo apt install unzip

Unzip the folder:

$ unzip bedrock-server-yada-yada.zip

Setup to run

Create a start program:

$ touch start.sh

Edit the file and add these contents:

#!/bin/bash

LD_LIBRARY_PATH=. ./bedrock_server
sleep 10

Add execution permission to the file:

$ chmod +x start.sh

Edit server.properties, change at the least the name and the port.

We need to open the port in the firewall to be able to connect.

$ sudo ufw allow PORT

Make sure to change PORT to the port you had chosen in the server config file.

Test Server

At this point the server should be ready to run. You can test it by executing the start.sh
So, within the minecraft folder:

$ ./start.sh

Then, use minecraft on your phone to connect to the server.

After checking with your phone, use the command “stop” to shutdown the server.

I also like to change max players and enable the allow-list. Get your friends to connect before you turn on the allow-list, and then copy them into the allowlist.json. (Their name and xuid show up in the server feed when they connect to server)

[{"ignoresPlayerLimit":false,"name":"YourfriendsName","xuid":"8888888888888888"},{"ignoresPlayerLimit":false,"name": "AnotherFriend","xuid":"7777777777777777"}]

Setup automatic backups for the server

Now lets set it up so that the server will automatically backup every night. This will only make a local copy. You will need to manually download it every now and then to another system to have a secure backup incase your VPS has a problem.

First we need to create a couple of folders.

$ cd ~

$ mkdir -p backups/minecraft

Then we need to download a script:

$ cd backups

$ curl https://raw.githubusercontent.com/UnclassedPenguin/scripts/master/automcbu > automcbu

Make the script executable

$ chmod +x automcbu

Now we need to make sure the script will run everyday. We use cron to do that. To add a line to the crontab:

$ crontab -e

Add this to bottom of file that is opened:

SHELL=/bin/bash

# Backup minecraft server everyday at 4 AM
0 4 * * * bash /home/user/backups/automcbu

Make sure to edit user with your own username. This will run the backup every day at 4 am and store them in the backups/minecraft folder. The only things that are backed up are the worlds, the server config file and the allowlist.

For the backups to work, you have to use Screen.

Start a new screen session with the name “minecraft”:

$ screen -S minecraft

Now you can start the minecraft server:

$ cd minecraft

$ ./start.sh

To leave the screen session with it running in the background, use CTRL-a + d. This (d)isconnects you. To reconnect, if it is your only screen session, use screen -r.

Is that really it? Perhaps…

Nope! What about updates?


Upgrading the Server

At some point your app will update and when you go to log in you will get an error message about the server being out of date.

Heres how to fix it :)

SSH to server.

Make sure you have a current backup! Run automcbu manually if you need to.

Screen into the minecraft screen session:

$ screen -r

Shutdown the server by typing stop and enter.

Change directory to home folder:

$ cd

(If you’ve already done this before, you will have a minecraft-old, so delete it here.)

Delete old minecraft-old:

$ rm -r minecraft-old

Move current minecraft folder to minecraft-old:

$ mv minecraft minecraft-old

Create a new minecraft folder:

$ mkdir minecraft
$ cd minecraft

Download the new server:

Server Download

$ wget https://minecraft.azureedge.net/bin-linux/bedrock-server-0.00.00.00.zip

Unzip it:

$ unzip bedrock-server-0.00.00.00.zip

Change to home folder:

$ cd

Now we need to replace some files from the old server into the new server:

$ cp minecraft-old/allowlist.json minecraft/allowlist.json
$ cp minecraft-old/server.properties minecraft/server.properties
$ cp minecraft-old/start.sh minecraft/start.sh
$ cp -r minecraft-old/worlds minecraft/worlds

Then you should be good. Start your screen session again ($ screen -S minecraft) and start the server with $ ./start.sh. Check it from your phone, should be good to go!

Oh, op yourself again, if you need! in the server op playername.

Make sure to check the server the next few days to make sure backups are still functioning properly.

And maybe now thats actually it…