I am currently trying to set up a Minecraft for me and the boys to mess around on. something I want to do is find a way to make the server software automatically start when it boots in case something happens and makes it restart when I am away from home. As said in the title, I am running this on a no GUI version for performance reasons. Thanks in advance.
1 Answer
Since this is your first question on the site, I'm gonna try to over-explain everything in case you are new to this. In general, to run a process on boot on a modern Ubuntu system, you want to setup a "Systemd unit file" for a "service unit" and "enable" that unit so that it auto-starts on boot. Now you have terms to Google if any of this is unclear.
Depending on what service you are starting, the specifics can change a bit, but for all services that is the high-level process.
For minecraft, this seems like a sane guide. Per the guidelines of askubuntu, I'll try to summarize the main points of the guide here (and add a few things they have left out)
Create a user/group for running the minecraft server
We will call this the 'minecraft' user and the 'minecraft' usergroup for obvious reasons ;-) We create this user because it's more secure than re-using your own user/group. If there happens to be a bug in the minecraft server and one of your players takes advantage, we want the damage they can cause to be limited. Hypothetically, let's say the bug lets a malicious player run commands directly on your server. If your server was running as a separate "minecraft" user and group, they can only run commands as that user which is pretty restrictive. Worst case, they probably damage your minecraft install which is frustrating but not catastrophic. If you didn't make a new user/group and you ran minecraft using the root
user (the one that can do anything) then the same hypothetical attack would go from "frustrating" to "my whole system is compromised and all data was stolen off my server"
$ sudo adduser minecraft
Follow the prompts to setup a new user. If you're curious there are better commands than this one available (for example, you don't really need a shell or a home dir for this user), but this will work just fine
Create your service unit file
Using your editor of choice (you will need superuser permissions, so prefix the command with sudo
e.g. sudo nano
, create a file in /etc/systemd/system/minecraft.service
with these contents:
[Unit]
Description=Minecraft server
Wants=network.target
After=local-fs.target network.target
[Service]
User=minecraft
Group=minecraft
UMask=0027
EnvironmentFile=/etc/conf.d/minecraft
KillMode=none
SuccessExitStatus=0 1 255
NoNewPrivileges=true
PrivateDevices=true
PrivateTmp=true
ProtectHome=true
ProtectSystem=full
WorkingDirectory=~
ExecStart=/usr/bin/java -Xms${MINHEAP} -Xmx${MAXHEAP} -XX:ParallelGCThreads=${THREADS} -jar server.jar --nogui
# ExecStop=/usr/bin/mcrcon -H localhost -p ${RCON_PASSWD} stop
[Install]
WantedBy=multi-user.target
Note 1: I've commented out the mcrcon dependency. If you want a slightly better server, go install that and un-comment the ExecStop line.
Note 2: Feel free to change the ExecStart line as needed. For example, if you want to put the full path to your own server.jar
, you could do that by changing the ExecStart line
Create an environment file
FYI, we only need this step because the prior unit file references /etc/conf.d/minecraft
and then uses those variables in the ExecStart line. If we changed the unit file contents to not need an environment file, we would not have to make this one.
Create a file, again using sudo nano
, at /etc/conf.d/minecraft
and give it these contents:
# RCON_PASSWD needs to be identical rcon.password in server.properties
RCON_PASSWD=example
MINHEAP=2G
MAXHEAP=4G
THREADS=6
Enable your unit file
$ sudo chmod 644 /etc/systemd/system/minecraft.service
Start your service (this is a one-time "start"), and then check it's status. Make sure you can log in and everything looks ok. If you have issues, use the stop
command, fix the issue, then rerun the start
command and check the status again. Repeat until it looks kosher
$ sudo systemctl start minecraft
$ sudo systemctl status minecraft
Once everything looks good, enable the service to have it auto-run at bootup:
$ sudo systemctl enable minecraft
-
2@mook765 In this case it is minecraft.service. It's the name of the unit file.– ErikDec 26, 2019 at 4:25
-
minecraft.service: Failed to run 'start' task: No such file or directory
Aug 10, 2021 at 22:30 -
@AaronFranke sounds like you do not have any fail at /usr/bin/java. Maybe check that, if no file then you need to install java– HamyAug 10, 2021 at 23:12
-
I figured it out, I had two problems: Missing a shebang in the script, and the file contained
\r\n
instead of\n
. Aug 11, 2021 at 1:11