Raspberry Pi as Minecraft Server

A Raspberry Pi, especially the newer models, possesses all the necessary hardware power for running a dedicated Minecraft gaming server. While it is easy enough to host the game off your desktop computer while simultaneously playing, having a dedicated server allows you to host an uninterrupted Minecraft session that can stay up and running for longer periods of time. Rather than buying a robust rig to host your server, or paying a third party company to do the hosting for you, utilizing the Raspberry Pi becomes a cheap alternative. On top of that, you still have the ability to customize the server configuration to produce your ideal gaming environment.

In this tutorial, we will go over the step by step instructions to use a Raspberry Pi as a Minecraft server. With the proper configuration, you can start and stop your server with a simple script, and ensure that other systems on your local network, or those over the internet, can connect to your Raspberry Pi for a seamless gaming experience. Follow along with us below for the full setup.

In this tutorial you will learn:

  • How to install and configure Minecraft server on Raspberry Pi
  • How to use a systemd script for Minecraft server management
  • How to access Minecraft server console and configure settings
Raspberry Pi as Minecraft Server
Raspberry Pi as Minecraft Server
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install Prerequisites




There are a few packages we’ll need in order to run the Minecraft server, so let’s start by installing them. Open a command line terminal and type the following two commands:

$ sudo apt update
$ sudo apt install wget screen openjdk-17-jdk nmap

Explanation of each package we are installing:

  • wget will be used to download Minecraft server fies
  • screen is for running the Minecraft server in the background
  • openjdk-17-jdk is a Java package that Minecraft needs in order to run
  • nmap will be used later on for basic troubleshooting purposes

Create a Minecraft user

It is best practice to let the Minecraft server run under its own dedicated account, rather than using root or your normal user account. Create a new account on the Raspberry Pi with the following command:

$ sudo useradd -m -r -d /opt/minecraft minecraft

Then, set a password for the user:

$ sudo passwd minecraft

Install Minecraft server

NOTE
For best performance, it is best to run the Minecraft server by itself on the Raspberry Pi. In other words, ideally, the Raspberry Pi should not be hosting any other services or be used for everyday desktop tasks when the Minecraft dedicated service is running on the device.

While it is possible to run multiple instances of Minecraft on a single device in order to host multiple servers, keep in mind that Raspberry Pi is a lightweight system and it may not have sufficient resources for multiple servers. Therefore we will only be configuring a single server in this guide.

  1. We are going to start by creating a new directory where our Minecraft server files will reside. Feel free to change the name of yours, but we will be creating the following directory:
    $ sudo mkdir /opt/minecraft/myserver
    
  2. Next, we need to download the Minecraft server Java file with wget. Since Minecraft receives regular updates, you will need to make sure you are downloading the latest version by going to the official Minecraft download page and copying the link to the .jar file.

    Copy the link location from the Minecraft download page
    Copy the link location from the Minecraft download page



  3. Use the following command to download the file, replacing the link in this example with the current one available:
    $ sudo wget -O /opt/minecraft/myserver/minecraft_server.jar https://piston-data.mojang.com/v1/objects/8dd1a28015f51b1803213892b50b7b4fc76e594d/server.jar
    
    Use wget to download the server java file
    Use wget to download the server java file
  4. You need to accept the terms and conditions before being able to install the Minecraft server. Use this command to do so:
    $ sudo bash -c "echo eula=true > /opt/minecraft/myserver/eula.txt" 
    
  5. Lastly, we need to give our minecraft user account ownership on the Minecraft server directory:
    $ sudo chown -R minecraft /opt/minecraft/myserver/
    

Create Minecraft systemd startup script

We are now ready to begin hosting the Minecraft server. We could do this manually be executing a command, but we can make the process much more efficient by creating a systemd service that manages starting and stopping the server. It is very easy to do, as you will see below.

  1. Start by creating the following file with nano or your preferred text editor:
    $ sudo nano /etc/systemd/system/minecraft@.service
    
  2. Paste the following content in the new file:
    [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 SHUTTING DOWN IN 5 SECONDS. SAVING ALL MAPS..."\015'
    ExecStop=/bin/sleep 5
    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
    WARNING
    Note that line 13 instructs Minecraft on how much system memory it can use. The file above will allocate 2 GB of memory. Keep in mind the limits of the Raspberry Pi if you decide to make any changes to this setting.
  3. Now, you can save your changes to the file and exit.

    systemd startup script for Minecraft on Raspberry Pi
    systemd startup script for Minecraft on Raspberry Pi



How to start, stop, and manage Minecraft server

Starting the Minecraft server is very easy. Use the following systemctl command to put it up:

$ sudo systemctl start minecraft@myserver

You can confirm the current status of the server to make sure it’s up and running with the following command. It should return output that looks like the screenshot below.

$ sudo systemctl status minecraft@myserver
Checking the status of the Raspberry Pi Minecraft server
Checking the status of the Raspberry Pi Minecraft server

Type the following command if you want the Minecraft server to start automatically every time your Raspberry Pi system reboots:

$ sudo systemctl enable minecraft@myserver

To make sure your Minecraft server is listening for incoming connections, use the nmap command to check the default Minecraft port, which is 25565:

$ nmap -p 25565 localhost
nmap shows that Minecraft is listening on port 25565
nmap shows that Minecraft is listening on port 25565

As long as nmap shows that Minecraft is listening on the correct port, your server is good to go. If your port is closed, execute the following iptables command to allow port 25565 through your system firewall.

$ sudo iptables -I INPUT -p tcp --dport 25565 -j ACCEPT

Additional Configuration

Your MineCraft server should already be up and running, but here are some additional steps you can take in order to customize server settings:

Server settings are stored in the server.properties file. If you followed along with the same directory structure we used in the steps above, then this file can be opened at the following path:

$ su minecraft
$ nano /opt/minecraft/myserver/server.properties
Editing the server properties files for Minecraft settings
Editing the server properties files for Minecraft settings

In order to access the console at any time, we can reattach the screen session by executing:

$ su minecraft
$ screen -r

Accessing the Minecraft server console, where commands can be executed
Accessing the Minecraft server console, where commands can be executed



Closing Thoughts

In this tutorial, we saw how to use a Raspberry Pi as a Minecraft server. We also learned how to create a systemd script which can easily start, stop, and manage the status of our Minecraft server. By following the steps in our turorial, you can have a Minecraft server up and running on your Raspberry Pi in just a few minutes, with a convenient script configured to make controlling the status of your server a breeze.



Comments and Discussions
Linux Forum