{"id":226,"date":"2021-03-26T17:42:21","date_gmt":"2021-03-27T00:42:21","guid":{"rendered":"https:\/\/www.lucaswilliams.net\/?p=226"},"modified":"2021-03-26T17:42:25","modified_gmt":"2021-03-27T00:42:25","slug":"minecraft-server-for-ubuntu-20-04-2","status":"publish","type":"post","link":"https:\/\/www.lucaswilliams.net\/index.php\/2021\/03\/26\/minecraft-server-for-ubuntu-20-04-2\/","title":{"rendered":"Minecraft Server for Ubuntu 20.04.2"},"content":{"rendered":"\n<p>Hello everyone. I hope you are all doing well. I am writing this blog entry because I created a Minecraft server for my kids some time ago, but I had a hardware failure in the system and never replaced it. At the time, it was no big deal since the boys decided that they were done with Minecraft. But lately, with this new version of Minecraft, they have gotten back into it, and they wanted to have a shared sandbox that they can play with their friends on. <\/p>\n\n\n\n<p>So, I rebuilt their Minecraft server, but this time, I did it from 16.04 to 20.04. It was pretty straight forward and not much has changed in the way of doing this, but this is here for those of you that want to deploy your own Minecraft server.<\/p>\n\n\n\n<p><strong>NOTE:<\/strong> This will only work for the Java version of Minecraft. If you are using the Windows 10 version or the one on Xbox or Switch, you will not be able to connect to this server.<\/p>\n\n\n\n<p>So, the first thing you need is a clean installation of Ubuntu 20.04.2 Server. The system specs should be at least 4GB of RAM and 2 CPU Cores and 80GB of Storage. After you install Ubuntu, do the normal first boot practices, update, upgrade, reboot if required, etc.<\/p>\n\n\n\n<p><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/p>\n\n\n\n<p>Once that is completed, you need to install a couple things on top.<\/p>\n\n\n\n<p>One thing I like is the MCRcon tool from Tiiffi. I use this to do backups and statistics of my server, and it is really easy to use, and it&#8217;s super small. So I install the Build-Essential package as well as git. Minecraft also leverages Java, so I install the Open Java Development Kit packages with headless mode:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt install git build-essential<\/code> openjdk-11-jre-headless<\/pre>\n\n\n\n<p>Once that is completed, I then create a minecraft user so that when I run this as a service, it is a lot more secure, and I have a location where to keep all the dedicated Minecraft files.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo useradd -m -r -U -d \/opt\/minecraft -s \/bin\/bash minecraft<\/code><\/pre>\n\n\n\n<p>This creates the Minecraft user with the home directory in \/opt\/minecraft. This also doesn&#8217;t create a password for this account so we don&#8217;t have to worry about someone gaining access to our system with this account. You can only access this account via <code>sudo su - minecraft<\/code> with your local admin account.<\/p>\n\n\n\n<p>Now, we need to switch to the minecraft user and run the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo su - minecraft\nmkdir -p {server,tools,backups}\ngit clone https:\/\/github.com\/Tiiffi\/mcrcon.git ~\/tools\/mcrcon\ncd ~\/tools\/mcrcon\nmake\n<\/pre>\n\n\n\n<p>This will create the required directories for Minecraft, and download and build the MCRcon tool. You can verify that the MCRcon tools built successfully by running the command:<\/p>\n\n\n\n<p><code>~\/tools\/mcrcon\/mcrcon -v<\/code><\/p>\n\n\n\n<p>You will get the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mcrcon 0.7.1 (built: Mar 26 2021 22:34:02) - https:\/\/github.com\/Tiiffi\/mcrcon\n Bug reports:\n     tiiffi+mcrcon at gmail\n     https:\/\/github.com\/Tiiffi\/mcrcon\/issues\/<\/pre>\n\n\n\n<p>Now, we get to installing the Minecraft Server Java file.<\/p>\n\n\n\n<p>First, we need to download the server.jar file from Minecraft. You can go <a href=\"https:\/\/minecraft.net\/en-us\/download\/server\/\" data-type=\"URL\" data-id=\"https:\/\/minecraft.net\/en-us\/download\/server\/\">here<\/a> to download the file, or what I did, is I go to the link, and from there, I right click the link and select &#8216;Copy Link Address&#8217; so I can paste it into my terminal on the server and use <code>wget<\/code> to install it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wget https:\/\/launcher.mojang.com\/v1\/objects\/1b557e7b033b583cd9f66746b7a9ab1ec1673ced\/server.jar -P ~\/server <\/pre>\n\n\n\n<p>Now, we need to run the Minecraft server. It will fail on the first run because we need to accept the EULA. We also need to modify the server.properties file since the first run creates these files:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd ~\/server\n<code>java -Xmx1024M -Xms1024M -jar server.jar nogui<\/code><\/pre>\n\n\n\n<p>After the program fails to start, we need to modify the eula.txt file and change the <code>eula=false<\/code> at the end of the file to <code>eula=true<\/code>. Save this file and exit.<br>Next, we need to enable RCON in Minecraft. Open the server.properties file and search for the following variables, and change them accordingly:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>rcon.port=25575<\/code>\n<code>rcon.password=PassW0rd<\/code>\n<code>enable-rcon=true<\/code><\/pre>\n\n\n\n<p>Also, while you are in this file, you can make any other changes that you want to the server, such as the server name, listening port for the server, the MOTD, etc. Also, choose a complex password so that not just anyone can remote control your server.<\/p>\n\n\n\n<p>Now, I like to have this run as a service using SystemD. To do this, create a service script. First you have to exit as the Minecraft user by typing <code>exit<\/code> and getting back to your local admin shell. Then run the following:<\/p>\n\n\n\n<p><code>sudo vi \/etc\/systemd\/system\/minecraft.service<\/code><\/p>\n\n\n\n<p>Paste the following in the file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=Minecraft Server\nAfter=network.target\n\n[Service]\nUser=minecraft\nNice=1\nKillMode=none\nSuccessExitStatus=0 1\nProtectHome=true\nProtectSystem=full\nPrivateDevices=true\nNoNewPrivileges=true\nWorkingDirectory=\/opt\/minecraft\/server\nExecStart=\/usr\/bin\/java -Xmx2G -Xms2G -jar server.jar nogui\nExecStop=\/opt\/minecraft\/tools\/mcrcon\/mcrcon -H 127.0.0.1 -P 25575 -p PassW0rd stop\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\n<p>Save the document. Next, run <\/p>\n\n\n\n<p><code>sudo systemctl daemon-reload<\/code><\/p>\n\n\n\n<p>This will refresh systemd with the new minecraft.service. <\/p>\n\n\n\n<p>Now, you can start the minecraft service:<\/p>\n\n\n\n<p><code>sudo systemctl start minecraft<\/code><\/p>\n\n\n\n<p>To get it to start on reboots, execute the following:<\/p>\n\n\n\n<p><code>sudo sytemctl enable minecraft<\/code><\/p>\n\n\n\n<p>The last thing we have to do is create the backup job for your server. This uses the MCRcon tool and crontab to clean up the server as well.<\/p>\n\n\n\n<p>Switch back to the Minecraft user and perform the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo su - minecraft\nvi ~\/tools\/backup.sh<\/pre>\n\n\n\n<p>Paste the following script into the new file you are creating:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!\/bin\/bash\n function rcon {\n   \/opt\/minecraft\/tools\/mcrcon\/mcrcon -H 127.0.0.1 -P 25575 -p PassW0rd \"$1\"\n }\n rcon \"save-off\"\n rcon \"save-all\"\n tar -cvpzf \/opt\/minecraft\/backups\/server-$(date +%F-%H-%M).tar.gz \/opt\/minecraft\/server\n rcon \"save-on\"\n # Delete older backups\n find \/opt\/minecraft\/backups\/ -type f -mtime +7 -name '*.gz' -delete<\/pre>\n\n\n\n<p>Now, create a crontab to run the backup:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">crontab -e\n0 0 * * * \/opt\/minecraft\/tools\/backup.sh<\/pre>\n\n\n\n<p>Now exit as the Minecraft user and return as the local admin. Lastly, because I leverage UFW for my firewall, I need to open the port to the world so that people can connect to it. I do that with the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow from 10.1.10.0\/24 to any 25575\nsudo ufw allow 25565\/tcp\n<\/pre>\n\n\n\n<p>This allows the Remote console to be accessed only by my internal network, and allows the Minecraft game to be accessed by the outside world. <\/p>\n\n\n\n<p>Now, you are ready to connect your Minecraft clients to your server and have some fun! <\/p>\n\n\n\n<p>Let me know if this guide worked for you or if you have any questions or comments, please leave them below. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone. I hope you are all doing well. I am writing this blog entry because I created a Minecraft server for my kids some time ago, but I had a hardware failure in the system and never replaced it. At the time, it was no big deal since the boys decided that they were [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,37,98,6],"tags":[90,103,34,101,100,102,99,67,69,5],"class_list":["post-226","post","type-post","status-publish","format-standard","hentry","category-games","category-howto","category-java","category-ubuntu","tag-20-04","tag-crontab","tag-games","tag-git","tag-java","tag-mccron","tag-minecraft","tag-server","tag-systemd","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts\/226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":1,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":227,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts\/226\/revisions\/227"}],"wp:attachment":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}