My (modped) Minecraft Server Operation [alapok]

The idea came up recently to start a server again. Since the modpack you are looking at does not have an official server-side package, you also had to prepare it, i will explain in a later article.

Also, since some MC server hostings have a lot of constraints, I have come to the conclusion that, as in the past, it will run on its own infrastructure.

Prerequisites

  • (in our case) CentOS 7 server (whether physical or virtual) with public (to be fixed, not condition) IP address
  • a user sudo rights
  • a user that will run the server in its name (never use the rott user specifically, not recommended for security reasons)
  • mcrcon that lets us live the Minecraft server console
  • Git
  • "Development Tools" Package
  • Java
  • as well as the modpack itself

Install required packages

Java

To run Minecraft, you need Java 8 or later. Also, since we don't need a graphical interface on the server, we're installing the headless version from openjdk. It's a good solution because there's less dependency in this version, so you're going to waste fewer resources for us. Installing this is very easy:

$ sudo yum install java-1.8.0-openjdk-headless

mcrcon "dependencies"

$ sudo yum install git
$ sudo yum install "Development Tools"

Checks

Make sure that the necessary packages are in fact installed:

$ java -version
openjdk version "1.8.0_221"
...
$ git --version
git version 1.8.3.1
$ gcc -v
...
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

Environment design

Create a Minecraft user

The service will run in this name, so we create a system user and a group called Minecraft. And your home library will be /opt/minecraft.

$ sudo useradd -r -m -U -d /opt/minecraft /s /bin/bash minecraft

This user does not need to enter a password, as we will not log in with it via SSH, so we will not give it the right to do so. If you want to switch to this user, you will need either the root user (avoid this) or a user with sudo rights.

Let us switch now, because we will take the next steps in his name.

$ sudo su - minecraft

Building a library structure

In the user's home directory, bring ladder 3 directories in order to place the necessary files and packages transparently and structuredly. We'll create a few more in the future, but that's enough for the basics.

$ mkdir -p ~/{backups,tools,server}
  • Backups from the server will be added to the backup directory. This will be synchronized with a remote backup server in the future.
  • The tools are added to our all kinds of scripts (currently only mcrcon and backup.sh).
  • The server directory will be hosted by the Minecraft server itself.

download and translate mcrcon

RCON is a protocol that allows you to connect to our Minecraft server and run commands on it. mcrcon is an RCON client written in C.

Enter the tools directory and clone the Tiiffi/mcrcon GitHub repository:

$ cd ~/tools & git clone https://github.com/Tiiffi/mcrcon.git

Then, log in to the resulting directory:

$ cd ~/tools/mcrcon

and translate using GCC:

$ gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c

Minecraft server download and configuration

Download or create your own

I'll make my own modpack in a later article. If a server-side version of the modpack you choose is available, fill it in and unpack it.

However, if you want to turn vanilla into a server, simply use wget to download it from mojang servers:

$ wget https://launcher.mojang.com/v1/objects/a412fd69db1f81db3f511c1463fd304675244077/server.jar -P ~/server

Configuration

Create a file named eula.txt under the server directory

$ nano ~/server/eula.txt

and write the next line

eula=true

Then save and close. Then create a server.properties file under the server directory:

$ nano ~/server/server.properties

and make up with the following parameters:

#Minecraft server properties
generator-settings=
op-permission-level=4
allow-nether=true
level-name=world
enable-query=false
allow-flight=false
announce-player-achievements=true
rcon.password=password
server port=25565
level-type=DEFAULT
max_minion_spawn=-1
enable-rcon=true
force-gamemode=false
level-seed=
server-ip=
max-build-height=256
spawn-npcs=true
white-list=false
spawn-animals=true
hardcore=false
snooper-enabled=true
online-mode=true
resource-pack=
pvp=true
1 difficulty
enable-command-block=false
gamemode=0
player-idle-timeout=0
max-players=20
rcon.port=25575
spawn-monsters=true
generate-structures=true
view-distance=10
motd=My Minecraft Server

Don't forget to replace the password value with a strong password.

You can read more about these parameters in minecraft wiki.

Systemd Unit File

From here on out, you'll have to switch back to your sudo user at the beginning of this article by typing exit.

Then create a file called minecraft.service in /etc/systemd/system/:

$ sudo nano /etc/systemd/system/minecraft.service

Paste the following lines:

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

[Service]
User=minecraft
Nice=1
KillMode=none
SuccessExitStatus=0 1
ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
WorkingDirectory=/opt/minecraft/server
ExecStart=/usr/bin/java -Xmx4096M -Xms2048M -jar server.jar nogui
ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p sign stop

[Install]
WantedBy=multi-user.target

Rewrite the rcon port and password according to your environment. Also, to the extent of our environment and according to our modpack, rewrite Xmx and Xms. Xmx specifies how much memory you can allocate in total, and Xms specifies how much the novice allotment should be.

Save and close the file and notify systemd that a new unit file has been created:

$ sudo systemctl daemon-reload

You can then start your server:

$sudo systemctl start minecraft

At the first start, you create some configuration files as well as a library that contains the Minecraft world.

Make sure the server has started successfully:

$ sudo systemctl status minecraft
● minecraft.service - Minecraft Server
   Loaded: loaded (/etc/systemd/system/minecraft.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-06-24 15:46:16 CEST; 4s ago
 Main PID: 28883 (java)
   CGroup: /system.slice/minecraft.service
           └ 28883 /usr/bin/java -Xmx4096M -Xms2048M -jar server.jar nogui

We allow minecraft service to start automatically when you boot:

$ sudo systemctl enable minecraft

Firewall setting

If the server is protected by a firewall, then in order to connect to our Minecraft server from the outside, you will have to open port 25565 (of course, if you have entered a different port in the server.properties file, then):

$ sudo firewall-cmd --permanent --zone=public --add-port=25565/tcp
$ sudo firewall-cmd --reload

Backup

In this section, we create a script automated by cronjob, which will be used to save the server. To do this, we will need our minecraft user again:

$ sudo su - minecraft

Create a backup.sh file in /opt/minecraft/tools/:

$ nano/opt/minecraft/tools/backup.sh

And copy the following lines into it:

#!/bin/bash

function rcon {
  /opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password "$1"
}

rcon "save-off"
rcon "save-all"
tar -cvpzf /opt/minecraft/backups/server-$(date +%F-%H-%M).tar.gz /opt/minecraft/server
rcon "save-on"

## Delete older backups
find /opt/minecraft/backups/ -type f -mtime +7 -name '*.gz' -delete

chmod lets you run your script:

$ chmod +x /opt/minecraft/tools/backup.sh

Open the crontab file and create a cronjob that runs once a day at a fixed time (this can be adjusted at will, on a busy server you can run several times a day):

$ crontab

In our example, run at, say, 23:00 every day:

0 23 * * */opt/minecraft/tools/backup.sh

Access minecraft console

Access to minecraft's console is done using mcrcon.

The syntax is as follows, you must specify the host, port, password, and use the -t switch to switch the mcrcon to terminal mode:

$ /opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p
Logged in. Type 'quit' or 'exit' to quit.
>

If you're going to use the console a lot, you might want to create an alias for the line above. Open the .bashrc file in our home directory:

$ nano ~/.bashrc

Then add the following line:

alias mcrcon='/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p signss

Save and close the file, and then make the alias you create available in this session:

$ source ~/.bashrc

After that, it is enough to reach the console, so much to add to mcrcon:

$ mcrcon
Logged in. Type 'quit' or 'exit' to quit.
>

Közösségi megosztás:

Be the first to comment

Leave a Reply