How to host a Minecraft server on a virtual machine

How to host a Minecraft server on a virtual machine

Overview

To run a dedicated Minecraft server, you need a dedicated server machine, plenty of RAM, and plenty of bandwidth. Why not let Google handle these requirements for you? In this lab, you'll learn how to install, configure, and run a standard Minecraft server on Compute Engine.

Note: This lab is not approved by or associated with Mojang or Minecraft.

Your Minecraft server software will run on a Compute Engine instance, which is a virtual machine that runs on Google's infrastructure. This lab uses the default machine type for Compute Engine instances, n1-standard-1. The n1-standard-1 machine type includes a 10 GB boot disk, 1 virtual CPU (vCPU), and 3.75 GB of RAM. This machine type runs Debian Linux by default.

To make sure there's plenty of room for your Minecraft server's world data, you'll also attach a high performance 50 GB persistent solid-state drive (SSD) to your instance. With the addition of this persistent SSD, your instance will satisfy the system requirements for a dedicated Minecraft server and support up to 50 players comfortably.

What you'll do

  • Create a Compute Engine virtual machine instance

  • Install and configure the Minecraft server

  • Set up automatic backups of your Minecraft world data

Setup

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left.

Create and configure your Compute Engine instance

In the Cloud Platform Console, go to the VM instances page by clicking the Navigation menu > Compute Engine > VM instances.

How to host a Minecraft server on a virtual machine

You'll arrive at a dialog prompting you to create a new Compute Engine instance. Click Create Instance:

How to host a Minecraft server on a virtual machine

On the Create an instance dialog, configure the following for your instance. Ignore the other values for now.

Field

Value

Name

mc-server

Zone

us-central1-f

Boot disk > Change > Boot disk type

SSD Persistent Disk (Click Select to commit the change)

That's it for basic configuration! That said, don't create your new instance just yet—to meet the requirements for a dedicated Minecraft server, you'll stay in the Create an instance dialog and configure access and some advanced settings.

Enable Cloud Storage access for your instance

In this step you'll configure access to Cloud Storage for your instance. This is required to back up your world data to Cloud Storage, which you'll learn about later in this lab.

To enable access, configure Identity and API access in the Create an instance dialog. Ignore the other values.

Field

Value

Identity and API access > Service account

Compute Engine default service account

Identity and API access > Access scopes

Set access for each API

Storage

Read Write

Add a persistent disk to your instance

Next, you'll attach a persistent disk to your instance. Unlike boot disks, persistent disks are not tied to the life of your Compute Engine instance. For example, if your hosting needs change over time, you can move the disk to a more suitable machine type later.

The specific type of persistent disk you will use in this lab is a persistent SSD. This type of persistent disk supports very fast I/O operations, which can help reduce server lag.

Towards the bottom of the Create an instance dialog you'll see a link for Networking, disks, security, management, sole tenancy. Click the link.

How to host a Minecraft server on a virtual machine

In the Disks section, click Add new disk. You will next configure your persistent disk.

How to host a Minecraft server on a virtual machine

In the New disk dialog, set the values of the fields listed below. Ignore the other fields.

Field

Value

Name

minecraft-disk

Disk source type

Blank disk

Disk type

SSD Persistent Disk

Size (GB)

50

Click Save. When the instance is created, the disk will be attached automatically.

Tag your instance

Now click the Networking tab, add the tag minecraft-server to the Network Tags field. Later in the lab, you'll use this tag to create a firewall rule to allow external Minecraft clients to access your server.

Set up a static IP for your instance

To forward incoming requests to your instance dependably, your instance needs a static IP.

Still in the Networking tab, Under Network interface field, select default network. From External IP dropdown, select Create IP address.

Name your IP address mcs-ip and click Reserve to create the address.

How to host a Minecraft server on a virtual machine

Click Done to close this dialog.

Create your instance

Your instance is configured! Click the Create button to create your new instance. This action will take you back to the VM instances page.

Note: It can take up to 20 seconds for your instance to be created.

How to host a Minecraft server on a virtual machine

Click Check my progress to verify the objective. Create and configure your Compute Engine instance.

Mount and format your persistent disk

At this point, your disk is attached to your instance, but it hasn't yet been mounted to the instance. That's okay! If you mounted the disk now, you wouldn't be able to do much with it. As with any disk, your persistent disk first needs to be formatted with a filesystem that your operating system — in this case, Debian Linux — can understand.

Begin by establishing an SSH connection with your instance. On the VM instances page, click the SSH button to open a browser-based SSH terminal:

How to host a Minecraft server on a virtual machine

Run the following to create a new directory named minecraft in your instance's home directory:

sudo mkdir -p /home/minecraft

You'll use this directory as a mount point for your persistent disk.

First format your disk:

sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-minecraft-disk

Then mount your disk:

sudo mount -o discard,defaults /dev/disk/by-id/google-minecraft-disk /home/minecraft Mount and format your persistent disk.

Install and run the Minecraft server on your instance

Your persistent disk is officially mounted. Now it's time to do what you came here to do in the first place: install and run your Minecraft server.

Set up the Java Runtime Environment (JRE)

The Minecraft server runs on top of the Java Virtual Machine (JVM), so it requires the Java Runtime Environment (JRE) to run. Because the server doesn't need a graphical user interface, this lab uses the headless version of the JRE. This approach reduces the JRE's resource usage on your machine, helping ensure that the Minecraft server has plenty of room to expand its own resource usage if needed.

You'll need to update the Debian repositories on your Debian installation before you can download and install the headless version of the JRE. To do so, run the following command in your SSH terminal:

sudo apt-get update

After your repositories are updated, you can install the headless JRE:

sudo apt-get install -y default-jre-headless

Install the Minecraft server

Now that you've set up the JRE, it's time to download and install the Minecraft server.

Begin by navigating to your minecraft directory:

cd /home/minecraft

Because the minecraft directory contains your mounted persistent disk, you're going to need a special level of access known as root user access to run commands on it. Run the following to become the root user:

sudo su

Next, download the current Minecraft server's Java archive file (JAR) to your instance:

curl https://s3.amazonaws.com/Minecraft.Download/versions/1.11/minecraft_server.1.11.jar > minecraft_server.1.11.jar Note: This lab uses minecraft_server.1.11. However, a newer version might have been released since this lab was published. You can find a link to the latest JAR file by visiting the Minecraft download page.

Start the server for the first time:

java -Xms1G -Xmx7G -jar minecraft_server.1.11.jar nogui

The first run is a little anticlimactic: the server simply starts, executes some quick operations, and stops. If you run the following command, however, you'll notice that some new files have been created in the minecraft directory:

ls -l

Among these new files, you'll find a file called eula.txt, which was downloaded when you started the server. This file contains a single boolean variable, eula. To use the Minecraft server, you must accept the terms of the Minecraft End User License Agreement (EULA). To accept the terms of the EULA, edit the following file and set the value of eula from false to true.

Open this file for editing:

nano eula.txt

Save the file (ctrl + o and Enter) and exit (ctrl + x).

Install and configure Minecraft server.

Configure the Minecraft server

Before you run your Minecraft server, you might want to edit the server's default properties.

Enter the following command to edit the file server.properties:

nano server.properties

For a complete description of each property type and its possible values, see the server.properties page on the Minecraft Wiki.

Run the Minecraft server

If you start the Minecraft server again at this point, it will be tied to the life of your SSH session — that is, if you close your SSH terminal, the server will be terminated as well. To get around this issue, you can use screen, an application that allows you to create a virtual terminal that can be "detached", becoming a background process, or "reattached", becoming a foreground process. When a virtual terminal is detached to the background, it will run whether you are logged in or not.

In your SSH terminal, run the following to install screen:

apt-get install -y screen

Next, start your Minecraft server in a screen virtual terminal. Use the -S flag to name your terminal mcs:

screen -S mcs java -Xmx1024M -Xms1024M -jar /home/minecraft/minecraft_server.1.11.jar nogui

Detach the screen terminal by pressing Ctrl + a, then typing d. The terminal will continue to run in the background.

To reattach the terminal, run screen -r <terminal_name> which follows:

screen -r mcs

Finally, detach your screen terminal again if needed.

Type exit once to leave root user mode, then type exit again to close your SSH connection.

You now have a running Minecraft server. However, it's not quite ready to be shared just yet. Before you can share your server, you need to set up a firewall rule that will allow people to access it.

Allow clients to access the Minecraft server

To be able to forward incoming requests to your instance dependably, you'll create a firewall rule. To do so:

  1. In the Cloud Platform Console, click the Navigation menu > VPC network.

  2. In the VPC Networks section, click default.

  3. Then click the Firewall rules tab.

  4. Click the Add firewall rule button the following dialog opens:

    How to host a Minecraft server on a virtual machine

  5. Configure the fields below. Ignore the other fields.

Field

Value

Name

minecraft-rule

Targets

Specified target tags

Target tags

minecraft-server

Source filter

IPv4 ranges

Source IPv4 ranges

0.0.0.0/0

Protocols and ports

Specified protocols and ports: tcp:25565

Note: By default, the Minecraft server uses 25565 as its default listening port. You can change this port number by editing server.properties, the configuration file for your Minecraft server, and restarting your server. Make sure to change the number in the firewall rule as well.

Click Create to create your new firewall rule. Users can now access your server from their local Minecraft clients.

Allow clients to access the Minecraft server.

Schedule regular backups

Whether you're running a local Minecraft client or running a Minecraft server, it's a good idea to backup your Minecraft world data on a regular basis. This section demonstrates how to set up regular backups of your world data using Cloud Storage.

Create a backup script

Begin by establishing an SSH connection with your instance from the VM instances page in your Cloud Platform Console (Navigation menu > Compute Engine > VM instances, click SSH). After the terminal opens, become the root user:

sudo su

Create a new Cloud Storage bucket to store your backups.

gsutil mb gs://<project_id>-minecraft-backup

Replace the <project_id> with Project ID in the connection details of the lab.

How to host a Minecraft server on a virtual machine

Note: Your bucket name must be unique across Cloud Storage.

Next, create a new shell script file, backup.sh, in your minecraft folder, and open it for editing:

nano /home/minecraft/backup.sh

Paste the following script into the file. Replace with your Cloud Storage bucket:

#!/bin/bash screen -r mcs -X stuff '/save-all\n/save-off\n' /usr/bin/gsutil cp -R ${BASH_SOURCE%/*}/world gs://<bucket>/$(date "+%Y%m%d-%H%M%S")-world screen -r mcs -X stuff '/save-on\n'

Save and exit.

This script begins by saving the current state of your world data and pausing your server's auto-save functionality. Next, the script backs up your server's world data directory (world), placing its contents in a time stamped directory (<timestamp>-world) in your Cloud Storage bucket. After the script finishes backing up the data, it resumes auto-saving on the Minecraft server.

Run the following to make your script executable:

chmod 755 /home/minecraft/backup.sh

Test the script:

/home/minecraft/backup.sh

After the script finishes, visit the Storage browser in the Cloud Platform Console and click on your bucket. You should see a timestamped backup of your world directory.

Schedule a cron job

Unless you prefer to manually initialize each backup, you'll probably want to have your script run automatically at predictable intervals. To accomplish this task, you need to schedule a new cron job.

To schedule a cron job, begin by opening the cron table for editing:

crontab -e

Select an editor of your choice.

Scroll to the bottom of the file and paste the following line, which specifies that backup.sh will run every 4 hours:

0 */4 * * * /home/minecraft/backup.sh

Save and exit.

That's it! Your Compute Engine instance will now automatically back up your world data to a Cloud Storage bucket every 4 hours.

Schedule regular backups.

Note: If you back up your world data every 4 hours, that means you're backing it up 6 times a day, 72 times a week, and roughly 300 times a month. Make sure to remove old backups regularly:

Shut down your Minecraft server

In the Qwiklabs environment, all of your resources will be deleted when you end the lab. But in a production environment,if you don't need to run your Minecraft server continuously, you should shut it down to avoid incurring unnecessary expenses.

Go to your SSH terminal and stop the Minecraft server by passing a /stop command to the screen terminal in which it is running:

sudo screen -r -X stuff '/stop\n'

Now that you've stopped your Minecraft server, you can safely shut down your mc-server instance.

On the VM instances page, click on the mc-server instance, then click the Stop button at the top of the page. Click Stop again in the Stop VM instance pop up to confirm. Go to the VM instances page to watch the process. You will be logged out of your SSH session.

To start up your instance again, click on mc-server name, and then click Start at the top of the page, and Start again on the Start VM instance pop up to confirm. To start the Minecraft server again, establish an SSH connection with the instance, remount your persistent disk, and start your Minecraft server in a new screen terminal, as described in the Run the Minecraft server section.

Automate startup and shutdown procedures

If you plan to shut down your server regularly, consider adding startup and shutdown scripts to your instance to automate common startup and shutdown procedures. You can automate the startup procedure as follows:

  1. Go to the VM instances page in the Cloud Platform Console.

  2. Click your instance name.

  3. Click Edit.

  4. In the Startup script section, add the following script:

#!/bin/bash mount /dev/disk/by-id/google-minecraft-disk /home/minecraft (crontab -l ; echo "0 */4 * * * /home/minecraft/backup.sh")| crontab - cd /home/minecraft screen -d -m -S mcs java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.11.jar nogui

When you restart your instance, this script will automatically mount your Minecraft disk to the appropriate directory, reinstall your cron job, start your Minecraft server in a screen session, and detach the session.

To automate your shutdown procedure, add a new key called shutdown-script in the Metadata section and copy the following into its Value field:

#!/bin/bash sudo screen -r -X stuff '/stop\n'

Click Save.

When you stop your instance, this script will shut down your Minecraft server before the instance shuts down.

Automate startup and shutdown procedures.

Congratulations!

You did it! You set up your Minecraft server on Compute Engine.

Next Steps / Learn More

  • Visit the Minecraft Wiki: It provides tons of useful resources for Minecraft players and Minecraft server administrators alike.
  • Try an alternative Minecraft server: The vanilla Minecraft server you installed in this lab is just one of the many Minecraft servers available. Visit Custom servers in the Minecraft Wiki to see a list of alternative servers that have been optimized for game types, machine requirements, easy modifications, and more.

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated June 2, 2022
Lab Last Tested June 2, 2022

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.