Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

How to create a Minecraft server on a VPS

profile picture
Author
Yusuf Yaser
Published
2022-03-04
Time to read
12 minutes reading time

Introduction

This tutorial will teach you how to create a Minecraft server on your VPS or dedicated server.

This tutorial was tested on Ubuntu 22.04 LTS.

Prerequisites

  • One local device » to connect to the game
  • One server with Ubuntu » to host the game
    • You need access to the root user or a user with sudo permissions.
    • Before you start a game on the server, you should make at least some basic configuration, including a firewall. For more information, see:
      Initial Server Setup with Ubuntu

Example terminology

  • 203.0.113.1 » Example public IP of the remote server
  • mcserver » Example user on the remote server

Step 1 - Setting up a user

It is not recommended to use the root user. Root users can unrestrictedly execute any command on the server. This can lead to accidental or unintentional changes. Creating a new user with sudo privileges can prevent such mistakes from happening.

Run this command to create a new user called "mcserver":

adduser mcserver

You will be asked to enter a password. Make sure to choose a secure one!

You can leave the rest empty.

Changing the user information for mcserver
Enter the new value, or press ENTER for the default
	Full Name []: 
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 

You can now add the new user to the sudo group:

usermod -aG sudo mcserver

Run this command to switch to the user "mcserver":

su - mcserver

Step 2 - Getting the server ready

The default port for Minecraft servers is 25565. To run Minecraft, you need Java.

For this reason, you need to:

Step 2.1 - Enabling port 25565

If you have a firewall on your server, make sure it allows port 25565 TCP and UDP so that your local device can connect to the game hosted on your server.

There are several different firewall tools. This tutorial covers:

UFW » Default firewall configuration tool for Ubuntu
  • View current firewall settings
    To check if the ufw firewall is active and if you already have any rules, you can use:

    sudo ufw status

    Example output:

    mcserver@ubuntu-server:~# ufw status
    Status: active
    
    To                         Action      From
    --                         ------      ----
    OpenSSH                    ALLOW       Anywhere
    OpenSSH (v6)               ALLOW       Anywhere (v6)
  • Allow port 25565 TCP and UDP
    If the firewall is active, run these commands to allow incoming traffic to port 25565 TCP and UDP:

    sudo ufw allow proto tcp to any port 25565
    sudo ufw allow proto udp to any port 25565
  • View new firewall settings
    The new rules should now be added. To check, use:

    sudo ufw status

    Example output:

    mcserver@ubuntu-server:~# sudo ufw status
    Status: active
    
    To                         Action      From
    --                         ------      ----
    OpenSSH                    ALLOW       Anywhere
    25565/tcp                  ALLOW       Anywhere
    25565/udp                  ALLOW       Anywhere
    OpenSSH (v6)               ALLOW       Anywhere (v6)
    25565/tcp (v6)             ALLOW       Anywhere (v6)
    25565/udp (v6)             ALLOW       Anywhere (v6)

Hetzner Cloud Firewall

This is only relevant if your remote server is a Hetzner Cloud server and you have a Hetzner Cloud Firewall applied to it. To edit your Firewall rules, open Cloud Console and select your Firewall. In the Firewall menu "Rules", add the following "inbound" rules:

IPs Protocol Port Port range
Any IPv4 Any IPv6 TCP 25565
Any IPv4 Any IPv6 UDP 25565

Note: If you're using a different firewall, make sure it allows incoming traffic to port 25565 TCP and UDP.

Step 2.2 - Installing Java

To start Minecraft, you need to have Java installed on your server.

  • Check if Java is already installed
    java --version
  • Install Java
    sudo apt update && sudo apt install openjdk-19-jdk-headless openjdk-19-jre-headless -y
    To check if the installation worked, you can use java --version again.

Step 3 - Installing Minecraft

Now that the port is open and Java is installed, you can install Minecraft itself. The server release of Minecraft is available at:


To install Minecraft, do the following:

  • Create a new directory for the Minecraft files and open it
    mkdir MinecraftServer && cd MinecraftServer

  • Go to minecraft.net or papermc.io
    Choose a version and copy the link to the JAR file. You can use wget and the copied link to save the file on your server. Pick one of the options below, do not run both commands:

    • minecraft.net
      wget https://piston-data.mojang.com/v1/objects/8dd1a28015f51b1803213892b50b7b4fc76e594d/server.jar
    • papermc.io
      wget -O server.jar https://api.papermc.io/v2/projects/paper/versions/1.20.4/builds/436/downloads/paper-1.20.4-436.jar

    This will download v1.20.4. If you want to install a different version, you can replace the link in the example command with the link you copied.

    Use ls -al to check if the file is now available on your server. You should only have one JAR file on your server. If you downloaded both JAR files, you can delete one of them.


  • Accept the EULA
    Starting your Minecraft server for the first time will fail. This is because you need to accept the Mojang EULA in the eula.txt file. Start the server:

    java -jar server.jar

    Make sure to execute this command within the directory in which you saved the JAR file.

    Use ls -al to check if eula.txt is now in your directory. Edit the file:

    nano eula.txt

    Change eula=false to eula=true to accept their EULA and use ctrl+X, followed by Y, followed by Enter to save the file.

Step 4 - Starting your server

Now that everything is ready, you can start the game. If you start the Minecraft server in a normal terminal session, the game will end as soon as you end the connection to your remote server. One way to keep the game running is to use the screen tool. Screen allows you to start separate virtual terminal sessions in one remote terminal session.

When you end your remote terminal session, the virtual terminal session (screen session) will stay open. When you start a new remote terminal session, you will see that the screen session is still there.

Install screen

If you don't have screen installed yet, you can install it now with:

sudo apt update && sudo apt install screen -y

To start a Minecraft game in a screen session, do the following:

  • Start the session

    screen -S MinecraftServer

    You can replace MinecraftServer with any other name for your screen.

  • Go to the Minecraft directory
    Open the directory in which you saved the server JAR file of Minecraft. If you used the example path from this tutorial, you can open it with:

    cd ~/MinecraftServer
  • Start Minecraft
    You can run this command replacing <MEMORY> with the maximum RAM you want to give to your Minecraft server in MB. You need to leave extra memory on your server, so do not use the entire RAM. You can use free -h to check the total memory.

    • Without flags
      java -Xms512M -Xmx<MEMORY>M -jar server.jar
    • With recommended JVM startup flags
      java -Xms512M -Xmx<MEMORY>M -XX:+AlwaysPreTouch -XX:+DisableExplicitGC -XX:+ParallelRefProcEnabled -XX:+PerfDisableSharedMem -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1HeapRegionSize=8M -XX:G1HeapWastePercent=5 -XX:G1MaxNewSizePercent=40 -XX:G1MixedGCCountTarget=4 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1NewSizePercent=30 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:G1ReservePercent=20 -XX:InitiatingHeapOccupancyPercent=15 -XX:MaxGCPauseMillis=200 -XX:MaxTenuringThreshold=1 -XX:SurvivorRatio=32 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -jar server.jar

    • -Xms » minimum RAM the Minecraft server gets (512M)
    • -Xmx » maximum RAM the Minecraft server gets (<MEMORY>M)

    You should now be in the Minecraft console where you can enter Minecraft commands.

    You can use /list to test if it is working:

    [09:08:10] [Server thread/INFO]: Done (64.773s)! For help, type "help"
    /list
    [09:09:03] [Server thread/INFO]: There are 0 of a max of 20 players online:

Step 5 - Managing the screen session

You can use the key combination ctrl+A followed by D to exit the screen session. This will not end the screen session and the game will continue to run.

To go back to the Minecraft screen session or to make any other changes, you can use these commands:

  • List all screens

    screen -list

    The screen you just created should also be listed here.

  • Go back to your running screen session

    screen -dr MinecraftServer

    If you used a different name, replace MinecraftServer with your screen name.

  • Delete the screen session

    screen -X -S MinecraftServer quit

    Note: This will end the Minecraft game and the screen session will no longer be available.

Step 5 - Using a different port

By default, the Minecraft game is always started on port 25565. To start a game on a different port, follow the steps below.

If you're still in your MinecraftServer screen session, use stop to end the current Minecraft server. If you want to start a second game in another screen session, you can use the key combination ctrl+A followed by D to exit the MinecraftServer screen session and create a new screen session. Each server JAR file is used for one Minecraft game. To start a second game in another screen session, you need to download the Minecraft server JAR file a second time. You should save the new file in a separate directory. You should also make sure that your server has enough RAM to host two games.

  • Open the server.properties file
    You can edit the port in the server.properties file. The file is in the same directory as your Minecraft server JAR file.

    Edit the server.properties file:

    nano server.properties
  • Change the port
    Go to the line server-port=25565 and change the port:

    server-port=25567

    If you want to use another port, remove 25567 and enter your port.

    Use ctrl+X, followed by Y, followed by Enter to save your changes and to exit the file.

  • Start a new game

    • Without flags
      java -Xms512M -Xmx<MEMORY>M -jar server.jar
    • With recommended JVM startup flags
      java -Xms512M -Xmx<MEMORY>M -XX:+AlwaysPreTouch -XX:+DisableExplicitGC -XX:+ParallelRefProcEnabled -XX:+PerfDisableSharedMem -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1HeapRegionSize=8M -XX:G1HeapWastePercent=5 -XX:G1MaxNewSizePercent=40 -XX:G1MixedGCCountTarget=4 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1NewSizePercent=30 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:G1ReservePercent=20 -XX:InitiatingHeapOccupancyPercent=15 -XX:MaxGCPauseMillis=200 -XX:MaxTenuringThreshold=1 -XX:SurvivorRatio=32 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -jar server.jar

    Replace <MEMORY> with the maximum RAM you want to give to your Minecraft server.

  • Update your firewall
    If you have a firewall, make sure it allows the port you just set for the game, in this example port 25567. For ufw and Hetzner Cloud Firewall, you can follow the steps described in "Step 2.1".

Step 6 - Auto-restarting your server

You can create a script to auto-restart the server if it crashes.

If you left the Minecraft screen session, use screen -dr MinecraftServer to go back.

You can use the Minecraft command stop to end your Minecraft server.

Enter the command below to save a new shell script in the same path as your Minecraft server JAR file, in this example the ~/MinecraftServer directory.

Replace <MEMORY> with the maximum RAM you want to give to your Minecraft server.

cat <<'EOF' >> ~/MinecraftServer/run.sh
#!/bin/bash

while true; do
	java -Xms512M -Xmx<MEMORY>M -jar server.jar
    for i in 3 2 1; do
    	echo "The server will restart in $i second(s), CTRL-C to cancel."
        sleep 1
    done
done

EOF

To make the script executable, run this command:

chmod +x ~/MinecraftServer/run.sh

Finally, start the Minecraft server:

./run.sh

If the Minecraft server crashes, it will automatically restart.

Step 7 - Joining your server

It is simple to join your Minecraft server!

On your local device, open the Minecraft Launcher and select Multiplayer » Add Server. You can use your server's IP address to join your server.

If you used the default port, you do not need to specify the port. If you used a different port, you should add the port at the end of the IP address separated by a colon: <server-ip>:<port>, e.g. 203.0.113.1:25567

If you are hosting the Minecraft server locally, you can use: localhost:<port>

Conclusion

You successfully created a Minecraft server and you know how to access its console. Next, you can edit the server.properties file to set up some custom settings. The file is in the same repository as your Minecraft server JAR file. To optimize performance, you can add the recommended JVM startup flags when you start your Minecraft server.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue

Discover

#Hetzner4gamers

Take your gaming experience to the next level! Discover which Hetzner server is best for your gaming world.

Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more