notes/IT/Setting up a Minecraft server.md

104 lines
4.8 KiB
Markdown
Raw Normal View History

2023-07-23 14:20:03 +00:00
# Preamble
*This guide assumes some basic knowledge of Linux and package managers.*
2023-07-23 14:23:03 +00:00
At minimum, a minecraft server requires Java, the [minecraft server jar](https://www.minecraft.net/en-us/download/server) or an alternative server (spigot, lithium), and the ability to port forward and allow programs through your firewall.
2023-07-23 14:20:03 +00:00
2023-07-23 14:19:03 +00:00
# Installing Java
2023-07-23 14:30:03 +00:00
As of 2023-07, Minecraft requires Java 17 to run version 1.20.1. You may need to look for a later version. Don't install the default version of Java in the repos of your chosen distro, it's very often out of date, and probably won't work with minecraft. You're going to look for any package that contains the JRE (Java Runtime Environment) or JDK (Java Development Kit). Both contain the Java Virtual Machine (JVM) and the ability to run Java programs. I like AdoptOpenJDK, but any distribution of Java that fits the version requirements will work sufficiently.
2023-07-23 14:31:03 +00:00
- Prior to installation, be sure to remove a java installation if it exists on your system. You can see if java is present on your distro with `java --version`.
2023-07-23 14:33:03 +00:00
The format of the install command will change depending on the Java version, distro repositories and package manager, but this is what you'd run to install Java 17 on Debian 12:
2023-07-23 14:37:03 +00:00
```sh
2023-07-23 14:33:03 +00:00
sudo apt install openjdk-17-jre-headless
```
2023-07-23 14:35:03 +00:00
# Setting up a location for the server
2023-07-23 14:37:03 +00:00
I like to have the minecraft server jar in `/opt/minecraft` (`/opt` is for non-user specific programs that don't go in any of the `/bin` folders), accessible by a `minecraft` group.
```sh
# Make a new folder named `minecraft` in `/opt`
sudo mkdir /opt/minecraft
2023-07-23 14:38:03 +00:00
# Create a new group named `minecraft`
sudo groupadd minecraft
2023-07-23 14:41:03 +00:00
# Add your user to it (You may need to reload your user session)
2023-07-23 14:38:03 +00:00
sudo usermod -aG minecraft your-username-here
2023-07-23 14:39:03 +00:00
# Make it so that the minecraft group owns that folder
sudo chgrp minecraft /opt/minecraft
2023-07-23 14:40:03 +00:00
# Make it so that:
# The user that owns the folder has read, write, and execute perms
# The group that owns the folder has read, write, and execute perms
# Anyone not in the above categories can read, but not write or execute
2023-07-23 14:41:03 +00:00
sudo chmod 775 /opt/minecraft
2023-07-23 14:37:03 +00:00
```
2023-07-23 14:17:03 +00:00
2023-07-23 14:45:03 +00:00
Downloading the server jar into `/opt/minecraft`:
```
cd /opt/minecraft
# Go to https://www.minecraft.net/en-us/download/server,
# and copy the link for "Download server.[version].jar:
2023-07-23 14:46:03 +00:00
wget https://piston-data.mojang.com/v1/objects/84194a2f286ef7c14ed7ce0090dba59902951553/server.jar
2023-07-23 14:49:03 +00:00
# If you get permisison denied errors, the filesystem permissions for /opt/minecraft are
# not set correctly, check those with `ls -lah` and learn stuff
2023-07-23 14:46:03 +00:00
```
2023-07-23 14:45:03 +00:00
2023-07-23 14:46:03 +00:00
# Starting the server
```
2023-07-23 14:47:03 +00:00
# -jar means that you want to run a jarfile (something with the .jar) extension
# -Xmx is the maximum amount of memory your server will be allowed to use
2023-07-23 14:48:03 +00:00
# -Xms is the amount of memory your server will be allocated when it starts
# these two take values in format of NUMBERUNIT, so 3G would be 3 gigabytes,
# 512M would be 512 megabytes
2023-07-23 14:50:03 +00:00
# server.jar is the name of the server you just downloaded
# nogui is a commandline argument passed to the server that says
# to start without a gui
2023-07-23 15:21:03 +00:00
java -jar -XmX4G -Xms512M server.jar nogui
2023-07-23 14:45:03 +00:00
```
2023-07-23 14:51:03 +00:00
The first time you will get an error about an EULA, you need to look for `EULA.txt` in the same directory as the server jar, and edit the file with a text editor (`vim`, `nano`) to agree. Run it again, and you should see it generating the world
2023-07-23 14:52:03 +00:00
# Allowing minecraft through the firewall
This may or may not be needed, depending on your distro, and the command syntax will vary slightly depending on the distro.
2023-07-23 14:53:03 +00:00
iptables:
```
iptables -A INPUT -p tcp --dport 25565 -j ACCEPT
```
2023-07-23 15:12:03 +00:00
# Deploying Minecraft as a systemd service
2023-07-23 14:11:03 +00:00
This enables minecraft to run in the background, restart if it crashes, etc etc.
2023-07-23 14:12:03 +00:00
In `/etc/systemd/system`, place this file and tailor it to your liking.
```
[Unit]
2023-07-23 14:54:03 +00:00
Description=Minecraft Server
2023-07-23 14:13:03 +00:00
# I don't know what these mean I'm getting them from here:
# https://gist.github.com/dotStart/ea0455714a0942474635
2023-07-23 14:12:03 +00:00
Wants=network-online.target
2023-07-23 14:13:03 +00:00
After=network-online.target
[Service]
# If wanted, you can set a particular group or user to run the service under
2023-07-23 14:14:03 +00:00
# User=foo
# Group=bar
2023-07-23 15:12:03 +00:00
# Where you want the command to be run
2023-07-23 15:13:03 +00:00
WorkingDirectory=/opt/minecraft
2023-07-23 14:14:03 +00:00
# This command is run when the service is started
2023-07-23 15:13:03 +00:00
ExecStart=/usr/bin/java -Xmx8G -Xms3g -jar /path/to/server.jar nogui
2023-07-23 15:10:03 +00:00
```
2023-07-23 15:15:03 +00:00
Then run `sudo sytemctl enable minecraft.service` to have the server start up when the server is rebooted.
## Utility systemctl commands
```
# start the server
sudo systemctl start minecraft.service
2023-07-23 15:16:03 +00:00
# restart the server
sudo systemctl restart minecraft.service
# check the top few logs and whether or not the service is running
sudo systemctl status minecraft.service
2023-07-23 15:17:03 +00:00
# watch the logs (f for follow output, u for unit)
2023-07-23 15:16:03 +00:00
sudo journalctl -f -u minecraft.service
2023-07-23 15:15:03 +00:00
```
2023-07-23 15:10:03 +00:00
// TODO: https://unix.stackexchange.com/questions/612137/how-to-write-to-stdin-of-a-service