Skip to content

Instantly share code, notes, and snippets.

@psigen
Last active March 28, 2020 22:29
Show Gist options
  • Save psigen/584dd28382261c50b17aaa17d594f1fd to your computer and use it in GitHub Desktop.
Save psigen/584dd28382261c50b17aaa17d594f1fd to your computer and use it in GitHub Desktop.
Minecraft Server Setup (Linux)

Minecraft Server Setup

This is a pair of bash scripts that can be installed on an Ubuntu server to bootstrap a working minecraft server. They are designed to be run with root permissions.

  • server.sh - bootstrap the creation of a service that runs the minecraft server
  • console.sh - connect to the console of a running server using screen
#!/bin/bash
#
# Attach to a running console on the minecraft server.
# Use Ctrl+a, d to exit from the console.
#
MINECRAFT_USER=minecraft
MINECRAFT_SCREEN=minecraft
sudo -u "${MINECRAFT_USER}" screen -rx "${MINECRAFT_SCREEN}"
#!/bin/bash
#
# Based on: https://minecraft.gamepedia.com/Tutorials/Ubuntu_startup_script
# For server versions, see: https://mcversions.net/
#
set -ex
MINECRAFT_DIR=/srv/minecraft-server
MINECRAFT_USER=minecraft
MINECRAFT_SERVICE=minecraft-server.service
MINECRAFT_URL=https://launcher.mojang.com/v1/objects/3dc3d84a581f14691199cf6831b71ed1296a9fdf/server.jar
MINECRAFT_SHA256=5ecdedab3a6e129321a444490d0a467c25ea702a24a99cebe3b6aed41f8f5729
# Create a service user to run minecraft.
adduser --system --home "${MINECRAFT_DIR}" "${MINECRAFT_USER}"
addgroup --system "${MINECRAFT_USER}"
adduser "${MINECRAFT_USER}" "${MINECRAFT_USER}"
mkdir -p "${MINECRAFT_DIR}"
chown -R "${MINECRAFT_USER}:${MINECRAFT_USER}" "${MINECRAFT_DIR}"
# Install required dependencies
apt-get update -qq
apt-get install -qq openjdk-8-jdk-headless
# Download the minecraft server installation and accept the EULA.
wget -O "${MINECRAFT_DIR}/server.jar" "${MINECRAFT_URL}"
(echo "${MINECRAFT_SHA256} ${MINECRAFT_DIR}/server.jar" | sha256sum --check) || (echo "SHA MISMATCH - Download was corrupt!" && false)
cat <<EOF > "${MINECRAFT_DIR}/eula.txt"
eula=true
EOF
# Set up the initial server properties.
cat <<EOF > "${MINECRAFT_DIR}/server.properties"
#Minecraft server properties
#Sat Mar 28 06:50:21 UTC 2020
spawn-protection=16
max-tick-time=60000
query.port=25565
generator-settings=
force-gamemode=false
allow-nether=true
enforce-whitelist=true
gamemode=survival
broadcast-console-to-ops=true
enable-query=false
player-idle-timeout=0
difficulty=easy
spawn-monsters=true
broadcast-rcon-to-ops=true
op-permission-level=4
pvp=true
snooper-enabled=true
level-type=default
hardcore=false
enable-command-block=false
max-players=30
network-compression-threshold=256
resource-pack-sha1=
max-world-size=29999984
function-permission-level=2
rcon.port=25575
server-port=25565
server-ip=
spawn-npcs=true
allow-flight=false
level-name=world
view-distance=10
resource-pack=
spawn-animals=true
white-list=true
rcon.password=
generate-structures=true
max-build-height=256
online-mode=true
level-seed=
use-native-transport=true
prevent-proxy-connections=false
enable-rcon=false
motd=Welcome to the BG Minecraft Server
EOF
# Create a systemctl service to run the server.
# This uses a screen launcher to allow terminal access to the server.
cat <<EOF > "/lib/systemd/system/${MINECRAFT_SERVICE}"
[Unit]
Description=start and stop the minecraft server
[Service]
WorkingDirectory=${MINECRAFT_DIR}
User=${MINECRAFT_USER}
Group=${MINECRAFT_USER}
Type=forking
Restart=on-failure
RestartSec=20 5
ExecStart=/usr/bin/screen -dmS minecraft /usr/bin/java -Xms1536M -Xmx1536M -jar server.jar nogui
[Install]
WantedBy=multi-user.target
EOF
# Start up the minecraft service.
systemctl daemon-reload
systemctl enable "${MINECRAFT_SERVICE}"
systemctl start "${MINECRAFT_SERVICE}"
sleep 1
systemctl status "${MINECRAFT_SERVICE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment