Rusty's Database

Rusty's Technical Adventures

Minecraft Server Installation – Linux – Ubuntu 22.04 LTS

Notes:

  • I referenced this guide from Linuxize and made my own modifications: How to Install Minecraft Server on Ubuntu 18.04 | Linuxize
  • This guide is for Minecraft Server 1.20.2, though it should work for any version. The only differences will be the version of Java required for your version of Minecraft Server and the Minecraft Server version.
  • This guide will also focus on using a Single Ubuntu Virtual Machine to run a Single Minecraft Server Instance. Though this guide can be used to configure multiple Minecraft instances on a single machine by changing a few variables. I’ll mention these, but won’t go into specifics.
  • I’ve run into goofy issues leaving the IP as the loopback (127.0.0.1) so I recommend changing any instances of the IP to the IP of the server.

Server Setup:

  1. Update the server.

    apt-get -y update && apt-get -y upgrade
    
  2. Install required packages.

    apt-get -y install git build-essential openjdk-19-jre-headless
    
    • If you’re installing an older version (or newer if you’re referencing this guide in the future) of Minecraft server, make sure to install the correct Java version, otherwise the server will not run. Depending on how old the Java version is, it may not be supported by Ubuntu 22.04. You could containerize the server with something like Docker or spin up an older version of Ubuntu. Just be careful of any potential security flaws.
  3. Create a Minecraft user:

    useradd -r -m -U -d /opt/minecraft -s /bin/bash minecraft
    
    • This will create a user that will specifically run this Minecraft instance, instead of the root user. It’s also very easy to modify each reference of the “minecraft” user to spin up multiple different Minecraft Servers on the same Ubuntu server without containers.
  4. Switch to the minecraft user to install/setup the Minecraft Server.

    su - minecraft
    
  5. Create some directories.

    mkdir -p ~/{backups,tools,server}
    
    • This creates three directories. One for backups, tools, and the server files. It makes it easy to manage everything.
  6. Download/install mcrcon.

    cd ~/tools && git clone https://github.com/Tiiffi/mcrcon.git
    
    • This is the remote console for the Minecraft Server.
  7. Compile mrcron.

    cd mcrcon
    gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c
    
  8. Download the Minecraft Server.

    wget https://piston-data.mojang.com/v1/objects/5b868151bd02b41319f54c8d4061b8cae84e665c/server.jar -P ~/server
    
    • The link above is for Minecraft 1.20.2, make sure to replace the link above with the correct version of Minecraft Server you want to run.
  9. Start the Minecraft Server.

    cd ~/server
    java -Xmx1024M -Xms512M -jar server.jar nogui
    
    • Change the -Xmx1024M to the amount of RAM you want to allocate to the server.
      • For Example if you want to allocate 16Gb of RAM, take 1024 * 16 to get 16384 and change the ‘1024’ to ‘16384’.
    • The server will fail to start but it will create some files we need to modify.
  10. Configure necessary settings.

    1. Accept the Eula by changing eula=false to eula=true.
nano eula.txt

​ 2. Accept the Eula by changing eula=false to eula=true.

nano server.properties
  • We’ll only change a few in this guide but change these settings to your preferences.

    rcon.password=securepassword
    enable-rcon=true
    server-ip=ipofserver
    
    • Create a secure password for rcon.password. Keep note of this for now as we’ll need it later.
    • We’ll enable rcon by changing enable-rcon=false to enable-rcon=true
    • Change server-ip= to server-ip=ipofserver (for example if the IP of the Ubuntu server is 192.168.100.100, set server-ip=192.168.100.100)
    • Also, set the rcon.port= and server-port=. They need to be different from each other and from any other Minecraft Server or Service running on the Ubuntu Server. If these ports conflict with ones that are currently in use, the Minecraft Server will fail to start.
      • If you’re running just a single server, set the server-port= to 25565, which is the default port Minecraft uses, and the rcon.port= to 25575, which is the default for rcon.
  1. Make a service.

    1. Type exit to return to your root user.
    nano /etc/systemd/system/minecraft.service
    
    1. Modify the following config to suit your server, and paste it into the service file.
    [Unit]
    Description=Minecraft Server
    After=network.target
    
    [Service]
    User=minecraft
    Nice=1
    KillMode=none
    SuccessExitStatus=0 1
    ProtectHome=true
    ProtectSystem=full
    PrivateDevices=true
    NoNewPrivileges=true
    WorkingDirectory=/opt/minecraft/server
    ExecStart=/usr/bin/java -Xmx1024M -Xms512M -jar server.jar nogui
    ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p securepassword stop
    
    [Install]
    WantedBy=multi-user.target
    
    • Modify the “securepassword” in ExecStop= to the rcon password set in the server.properties file.
    • Make sure to modify -Xmx1024M & the IP (127.0.0.1).
      • If running multiple servers, you’ll also need to change the user, working directory, and the rcon port.
  2. Reload the system daemon, start the Minecraft service, & enable the service on start up.

    systemctl daemon-reload
    systemctl start minecraft
    systemctl enable minecraft
    
    • Check the service by typing systemctl status minecraft.service

    • If the service is running properly, you’ll see “Active: active (running)” in the output.

      ● minecraft.service - Minecraft Server
           Loaded: loaded (/etc/systemd/system/minecraft.service; enabled; vendor preset: enabled)
           Active: active (running) since Sun 2023-08-06 19:18:32 UTC; 7s ago
         Main PID: 3601 (java)
            Tasks: 26 (limit: 9388)
           Memory: 547.4M
              CPU: 18.142s
           CGroup: /system.slice/minecraft.service
                   └─3601 /usr/bin/java -Xmx1024M -Xms512M -jar server.jar nogui
      

Server Backups:

  1. Change to the minecraft user.

    su - minecraft
    
  2. Create a backup script.

    nano /opt/minecraft/tools/backup.sh
    
    • Modify the following output as necessary and save the file.

      #!/bin/bash
      
      function rcon {
        /opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p securepassword "$1"
      }
      
      rcon "save-off"
      rcon "save-all"
      tar -cvpzf /opt/minecraft/backups/server-$(date +%F-%H-%M).tar.gz /opt/minecraft/server
      rcon "save-on"
      
      ## Delete older backups
      find /opt/minecraft/backups/ -type f -mtime +7 -name '*.gz' -delete
      
      • If you’re not making any custom changes, the only fields that need to be changed are the IP, to the IP of the server, and “strong-password” to the rcon server set in “server.properties”.
      • If running multiple servers, you’ll have to change the directories in the script as well.
  3. Make the script executable.

    chmod +x /opt/minecraft/tools/backup.sh
    
  4. Create a cron job to automatically run the script at a specified time and interval.

    crontab -e
    
    • press “1”
    • Paste the following text.
    0 2 * * * /opt/minecraft/tools/backup.sh
    
    • This will run the backup script at 2AM every day. Modify this as needed.
      • Search for “cron job scheduling”, there are plenty of resources that will explain how this works.

How to access the Minecraft Console:

  1. The console can be access via root or the minecraft user.

    /opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p securepassword -t
    
    • Change the IP (127.0.0.1) and “securepassword” to your rcon password.

Additional Commands:

systemctl stop minecraft.service
systemctl start minecraft.service
systemctl restart minecraft.service
systemctl enable minecraft.service
systemctl disable minecraft.service
systemctl status minecraft.service
  • Stop, stops the service, shutting down the server.
  • sStart, starts the service, starting up the server.
  • Restart, restarts the service, restarting the server.
  • Enable, enables the service to start on startup, allowing the Minecraft server to start automatically after a server reboot.
  • Disable, disables the service on startup, meaning the server will not automatically start after a server reboot.
  • Status, shows the status of the service, showing if the service is active or not.
tail -f server/logs/latest.log
  • Running this command from the root directory of the minecraft user will show a running log of the server.
    • Modify this command depending on what directory you’re in.
    • ‘f’ can be replaced with a number to view that number of previous lines in the log.
      • tail -1000 server/logs/latest.log will show the last 1000 lines of the log.

Comments

Leave a Reply