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..."