Sunday, April 05, 2020

Minecraft Setup On Linux Using systemctl

With schools and the economy shut down, all the cool kids are talking about their Minecraft "server" and the "IP. Help your children be cool and get them a Minecraft server!

In this posting we will:
- set up unix group and user
- configure ssh
- install Minecraft software
- start and configure Minecraft to run as a unix service (daemon)

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic

### Setup a user and group. In this example, the new users is "testuser" and the new group is "minec".
$ sudo addgroup minec
$ cat /etc/group | grep minec

# Create new user for the software install.
$ sudo adduser minec --ingroup minec
# At this point, you may want to log in as the user and run command "groups" to confirm the group is set up appropriately.

# Allow logon via ssh. Put user in /etc/ssh/sshd_config on the "AllowUsers" line
# Use "vi" or "vim" or similar editor to edit file /etc/ssh/sshd_config and add "testuser" to the "AllowUsers" line.
# After editing, it will look like this:
$ grep -i allowusers /etc/ssh/sshd_config
AllowUsers minec

# Have ssh  reread the configuration file. First get the process ID, then send HUP signal to that PID.
$ ps -ef | grep 'bin/sshd'$ sudo kill -hup <sshd_pid>
# Alternatively use service manager to restart ssh
$ sudo systemctl restart ssh

### If java is not installed, install java now.
$ which java
$ sudo apt update
Hit:1 focal InRelease
Get:2 focal-updates InRelease [107 kB]
Get:3 focal-backports InRelease [98.3 kB]
Get:4 focal-security InRelease [107 kB]
Fetched 312 kB in 1s (252 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
$ sudo apt install openjdk-11-jdk

$ which java

### Install Minecraft.
# Good instructions are at
# After downloading the software, the directory structure will look like this.
$ pwd
$ ls -l
total 35368
-rw-r-----  1 minec minec        2 Apr  4 12:56 banned-ips.json
-rw-r-----  1 minec minec        2 Apr  4 12:56 banned-players.json
-rw-r-----  1 minec minec      180 Mar 29 19:21 eula.txt
drwxr-x---  2 minec minec     4096 Apr  5 09:55 logs
-rw-r-----  1 minec minec      140 Apr  4 12:56 ops.json
-rw-r-----  1 minec minec 36175593 Mar 29 19:01 server.jar
-rw-r-----  1 minec minec      940 Apr  4 12:56
-rwxr-x---  1 minec minec       67 Apr  4 10:01
-rw-r-----  1 minec minec     1645 Apr  5 16:13 usercache.json
-rw-r-----  1 minec minec        2 Mar 29 19:21 whitelist.json
drwxr-x--- 11 minec minec     4096 Apr  5 17:46 world

# In preparation for starting the server unattended, the startup commands are in the executable shell file "".
This script first backs up the Minecraft configuration.
If you want to run it from the console and watch the log in the GUI, run the java command without "-nogui".
umask 026
cd /home/minec/Minecraft_Server
/bin/tar -cvf /tmp/worldDirs.$(date +%Y%m%d.%H%M%S).tar /home/minec/Minecraft_Server/world
echo "Finished tar of worldDirs."
/usr/bin/java -jar /home/minec/Minecraft_Server/server.jar -nogui &
/bin/echo $! > /home/minec/Minecraft_Server/

# Set up the service. Create file /etc/systemd/system/minecraft.service with these contents.
$ cd /etc/systemd/system
$ sudo vi minecraft.service
Description=Minecraft server
#ExecStop=/bin/kill -s TERM $MAINPID

# Reload systemctl configurations.
$ sudo systemctl daemon-reload
# Enable the service.
$ sudo systemctl enable minecraft.service
Created symlink /etc/systemd/system/ → /etc/systemd/system/minecraft.service.

# If the prior line output is not "Created symlink ..." then something is not correct.

# Start the minecraft service.
$ sudo systemctl start minecraft.service
$ sudo systemctl status minecraft.service

# Restart the machine to test if the Minecraft server starts ok.
$ sudo reboot

# After the machine restarts, look in /var/log/syslog for messages, check the status of the service, and look for the process.
$ sudo tail -44 /var/log/syslog
$ ps -ef | grep mine

$ systemctl status minecraft.service
● minecraft.service - Minecraft server
   Loaded: loaded (/etc/systemd/system/minecraft.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-04-10 21:26:31 MDT; 3min 26s ago
  Process: 7622 ExecStart=/home/minec/Minecraft_Server/ (code=exited, status=0/SUCCESS)
 Main PID: 7632 (java)
    Tasks: 36 (limit: 4915)
   CGroup: /system.slice/minecraft.service
           └─7632 /usr/bin/java -jar /home/minec/Minecraft_Server/server.jar -nogui

# You can stop the service. To prohibit the service from restarting upon machine reboot, also disable the service.
$ sudo systemctl stop minecraft.service
$ sudo systemctl disable minecraft.service
Removed /etc/systemd/system/

No comments:

Post a Comment