Setting up a Minecraft Server using systemd under Ubuntu 15.10

My daughter loves Minecraft and she loves playing it with someone (usually me) even more. I decided I wanted to set up a server that would allow her to connect and play in her world whenever she wanted, and also allow for multiplayer fun. We couldn't do that on her laptop, because it just isn't powerful enough to handle serving up Minecraft, as well as playing it. Thus, after much research, I figured out how to get a Minecraft server (or several) up and running using systemd on Ubuntu 15.10, which is what my desktop runs.

NOTE: This how-to only covers how to get a basic server up and running as a service under systemd. Many related topics (such as configuring your firewall, port forwarding, Minecraft server setup, server security, etc.) are outside of the scope of this discussion. This how-to assumes that your server will only be available to your LAN. YMMV.

Here are the steps in a nutshell:

Create a minecraft user, with a home directory at /opt/minecraft.

sudo groupadd -r minecraft
sudo useradd -r -g minecraft -d "/opt/minecraft" -s "/bin/bash" minecraft
sudo mkdir /opt/minecraft
sudo chown minecraft.minecraft /opt/minecraft

Next, switch to the minecraft user to set up the server(s).

sudo su minecraft
cd ~
pwd

The last command is just to make sure that we're where we think we are (it should be /opt/minecraft).

Create at least one directory to store a Minecraft server instance. You can actually create more than one, and I will include details about that later. For now, just create one. We'll start with a creative mode server. We'll also download the Minecraft server jar.

mkdir creative
cd creative
wget https://s3.amazonaws.com/Minecraft.Download/versions/1.9/minecraft_server.1.9.jar

You may need to update that last line to grab the latest server version.

Now, run the server for the first time. This will generate a few files and exit because we haven't accepted the EULA.

java -Xms1G -Xmx1G -jar minecraft_server.1.9.jar nogui

Edit the eula.txt file to accept the EULA. Change eula=false to eula=true. You can use whatever text editor you like. Save and close the file, then relaunch the server (using the same command as above).

Now is a good time to make sure the server is running correctly. Jump into Minecraft and try to connect. Walk around a bit and make sure everything is working as it should be. (How to do this is beyond the scope of this how-to).

Go ahead and stop the server (just type stop and press <ENTER> in the terminal that you launched the server in). Next we're going to create a symbolic link to make updating this easier down the line. It also has the advantage of allowing us to run multiple versions of Minecraft server without creating new unit files. All of this will make sense in a minute.

ln -s minecraft_server.1.9.jar minecraft_server.jar

You can test the server again, if you like, just to make sure you got it right. Just run the same command as before, but use minecraft_server.jar instead of minecraft_server.1.9.jar.

Before we set up the systemd configuration, we have one other thing to do. We need to set up rcon to allow for remote administration. Open the server.properties file and change rcon.enable=false to rcon.enable=true. Save and close the file, then fire up the server again.

If you did this right, everything should still work, but you'll get an error in the console about not having an rcon password. Stop the server and open the server.properties file again. You'll notice a few lines have been added. Make note of the port number from the line rcon.port=25575 (25575 is the default port number). Also, add a password to the rcon.password= line. Make it something good, because anyone who has this will have access to your Minecraft server!

This is a good time to get your hands on a client that will let you connect to your server. Since I have an Android phone, I use (and really like) MConsole. There are a lot of other options, though, so you don't have to take my advice. Once you have the client in hand, go ahead and test it out by entering your server's IP, rcon port and rcon password. If it works, you're golden!

Finally, we'll set up the systemd unit file. Create a new text file under the /opt/minecraft parent directory called minecraft@.service. The @ sign is very important. Paste the following into this file:

[Unit]
Description=Minecraft Server %i

[Service]
WorkingDirectory=/opt/minecraft/%i
User=minecraft
Group=minecraft
Restart=always

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

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."15'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"15'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"15'
ExecStop=/bin/sleep 2

[Install]
WantedBy=multi-user.target

What everything does here is beyond the scope of this how-to, but you may need to adjust some of this for your particular system. Using Oracle Java 8 on Ubuntu 15.10, though, this worked for me. You'll notice the reference to minecraft_server.jar. Using the symbolic link keeps us from having to update the unit file when Mojang releases a new version. Instead, we can just stop the server(s), replace the file and symbolic link, and restart the server(s). It also facilitates running multiple servers with a single unit file. As long as every server has a similar symbolic link, there's no need to reinvent the wheel for each instance.

To start the server service, we just need to create a symbolic link and start it. First, though, we'll switch back to our own user.

exit
sudo ln -s /opt/minecraft/minecraft@.service /etc/systemd/system/minecraft@.service
sudo systemctl start minecraft@creative.service

Notice that the directory name we chose for our server in the beginning, creative, appears here after the @ sign. This is important and is what allows us to use a single unit file and have multiple servers running simultaneously.

You can check on the status of your server by using the following command:

systemctl status minecraft@creative.service

Assuming everything is up and running, you can now connect to your server via rcon or jump in and start playing. Enjoy!

By the way, in order to have multiple servers going at once, all you have to do is set up your second (third, fourth, etc.) with a different port. Make sure your rcon port is also different. All of your servers should then be visible.

One final thing: to have your server start automatically when your computer starts up, copy the minecraft@.service to /lib/systemd/system/ and then enable the server as follows:

sudo cp /opt/minecraft/minecraft@.service /lib/systemd/system/.
sudo systemctl enable minecraft@creative.service

Your server should start up automatically upon reboot.