Ubuntu 20.04 Minecraft Server Setup - LinuxConfig.org

The beauty of having your own Minecraft server is that you're totally in charge of your gaming experience. You get to choose what settings are used on the server, you can use (or abuse) your admin powers for an advantage, and bestow those powers on to your fellow gaming buddies. You may want to create a private server for just you and your friends, or make it public for everyone to access.


This is a companion discussion topic for the original entry at https://linuxconfig.org/ubuntu-20-04-minecraft-server-setup

WoW
That was an outstanding piece of work.

How would I add a user to the OP list after I install the server?

I got it running. I am able to connect to it? Now I want to add my minecraft account as OP user.

How would I do that?

I am getting an error when starting Minecraft on Ubuntu 20.04 LTS. I copied the text of the minecraft@.service file exactly. I get the following error. minecraft@survival.servce: start request repeated too quickly. Any help would be appreciated.

Hey, I noticed an error in your systemd script. Specifically in how you invoke the stuff command. This causes none of the commands sent to the screen session to execute. Rather, the following happens:

  1. You run service minecraft@yourdirectory stop
  2. Your script sends the first command to the screen session via

stuff "say SERVER SHUTTING DOWN IN 5 SECONDS. SAVING ALL MAPS..."5

With this command, stuff sends the string "say SERVER SHUTTING DOWN IN 5 SECONDS. SAVING ALL MAPS..." to the server console, alongside a specific keypress, represented by the '5'. Normally, this should be enter, to execute the command in the server console. However, in screen the 5 stands for Numpad 5, as seen in the manpage of screen (Chapter 11.2 - Input Translation). That means that your command (and all subsequent commands) paste the command string into the console, followed by a five. So your script output on the console would look like this:

say SERVER SHUTTING DOWN IN 5 SECONDS. SAVING ALL MAPS...5save-all5stop5

  1. After all of that happens, the process just gets killed and the server does not properly shut down.

To fix this, you need to replace the Numpad 5 part in the stuff commands with the Enter key, which is represented by the string \015.

To make it easy, here is my fixed script (modified slightly with a proper countdown, you can customize as you want of course):

[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/opt/minecraft/%i

User=minecraft
Group=minecraft

Restart=always

ExecStart=/usr/bin/screen -DmS mc-%i /usr/bin/java -Xmx2G -jar minecraft_server.jar nogui

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER WILL SHUT DOWN IN 30 SECONDS. MAKE SURE YOU ARE IN A SAFE SPOT! SAVING ALL MAPS..."\015'
ExecStop=/bin/sleep 20
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 10 SECONDS. YOU BETTER HURRY UP NOW! SAVING ALL MAPS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say 5..."\015'
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say 4..."\015'
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say 3..."\015'
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say 2..."\015'
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say 1..."\015'
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\015'


[Install]
WantedBy=multi-user.target

I tested it and the commands now get passed properly. You can check for yourself and also see the erroneous behaviour of your script by pulling the screen session to the foreground on a second terminal or ssh session with

su - minecraft
screen -r

and watching the console output. Dont forget to put the session back into the background afterwards with

 Ctrl-a d

(Press Control and a at the same time, release both and then press d).

I hope that some people who setup their server with this guide see this, because not shutting the server down properly might lead to unexpected behaviour, glitches and data loss. Apart from this error, great and easy to follow guide!

1 Like

You have to hook into the screen session via

su - minecraft
screen -r

Now you are able to freely enter server commands. In the case of making a user an Operator, that would be

op yourUserName

Don’t forget to put the session back into the background afterwards with

Ctrl-a d

(Press Control and a, release both and then press d) and exit the minecraft user with

exit

Thank you! I did not know about this method.

I was able to work around it by modifying the script to not auto-restart. Then I could kill the process. Enter the console, OP my user, then exit the console and then restart the script after enabling the restart switch again.

I like your method better though!

thank you again for the response.

No problem, happy to help!

I would advise you to read my other comment as well and modify your systemd script accordingly. As it is in the guide right now it is faulty and causes your server to never properly shutdown. Just a little heads up.

For the systemD startup script on step 4 it says to save the file and exit, how do i do that?

Hi Musky_Tom,

Welcome to our forums.

You can exit edit mode in vi by hitting ESC, and after that you can save and exit by typing :wq, and finally pressing ENTER.

For those interested I had an issue that basically with the status said Failed with result ‘exit code’

To investigate further I did journalctl -u minecraft@survival which said “You are not the owner of /run/screen/S-minecraft.”

I did chown -R minecraft /run/screen/S-minecraft to make it work

Sorry, I am have the same issue.
I have tried you command at opt/minecraft/survival and also at etc/systems/system/ but get the no file error.

I had a question about restarting the server. Is there a command i can use to stop, start, or restart the server?

Hi El_Dingo,

Welcome to our forums.

As the tutorial sets up a systemd service, you can stop and restart the service with it.

To stop:

$ sudo systemctl stop minecraft@survival

To restart:

$ sudo systemctl restart minecraft@survival

When trying to start the server i het the message failed to start minecraft@survival.service: Unit minecraft@service jas a bad unit file setting.
Does annyone knotwilg wereld i dan vind the wrong wilde or even hoe to fix this?

Hi Justen_Cremer,

Welcome to our forums.

Can you post your unit file’s contents to us?

Hey, same problem here. I did chown for that folder and still am getting “Failed with result ‘exit code’.” Any ideas?

Also, a question here: when I do change to the user minecraft and do screen -r, I can’t see the console with the Minecraft log. Any idea why this may be?


btw wow that was fast

You seem to miss the configuration’s section headers, like [Unit], [Service] and [Install]. Without them systemd can’t tell what to do with the settings.