Minecraft Multiplayer Server

by jsmith
776 deployments · 223 still active · last rev. 4 years ago

Set up a Minecraft multiplayer server on Ubuntu. Automatically installs Java, creates a minecraft user, pulls in the latest minecraft_server.jar, and writes a script to automatically start the server. Read the comments in the script for more information.

Compatible with: Ubuntu 10.04 LTS 32bit
# Minecraft Stackscript
# Prepares the system, installs Java, installs Minecraft, and starts the server.
# By Jed Smith <jed@jedsmith.org>
# <udf name="mcopname" label="Admin Nickname" example="Nickname to give initial operator status">
# <udf name="mclevelname" label="Level Name" default="world" example="Name for the level">
# <udf name="mconline" label="Online Mode" default="yes" oneOf="yes,no" example="Whether the server will verify usernames with minecraft.net">
# <udf name="mcmonsters" label="Spawn Monsters" default="yes" oneOf="yes,no">
# <udf name="mcmaxplayers" label="Maximum Players" default="20">
# Once this script is completed, you can start the Minecraft server by typing the following:
#    # su - minecraft -s /opt/minecraft/run.sh
# This StackScript installs screen as well, so you can assign a password to the minecraft user,
# log in as the minecraft user, and run minecraft in a screen (if you so desire).
# Don't run minecraft as root!

# which distro are we on?
distro=`grep DISTRIB_CODENAME /etc/lsb-release | cut -d'=' -f 2`

# add source for java
cat >>/etc/apt/sources.list <<EOF

# for java, added by minecraft stackscript
deb http://archive.canonical.com/ubuntu $distro partner
deb-src http://archive.canonical.com/ubuntu $distro partner

# update and install packages
apt-get -y update
apt-get -y upgrade

# accept the sun-dlj-v1-1 license
for j in jdk jre; do
	echo "sun-java6-$j shared/accepted-sun-dlj-v1-1 select true" | /usr/bin/debconf-set-selections

# install java
apt-get -y install sun-java6-jre screen

# add the minecraft user
adduser --system --group --disabled-login --disabled-password --home /opt/minecraft minecraft
cd /opt/minecraft

# find where to get minecraft (please don't change this, Notch)
version=`wget -q -O- http://www.minecraft.net/download.jsp | grep "download/minecraft_server.jar" | cut -d'"' -f 2`
wget -O minecraft_server.jar "http://www.minecraft.net/$version"

# convert configuration variables
[ "$MCONLINE" == "yes" ] && MCONLINE=true
[ "$MCONLINE" == "true" ] || MCONLINE=false
[ "$MCMONSTERS" == "yes" ] && MCMONSTERS=true
[ "$MCMONSTERS" == "true" ] || MCMONSTERS=false

# write the configuration
cat >server.properties <<EOF
# Minecraft server properties
# Created by Minecraft StackScript `date`

# initial op user
echo $MCOPNAME >ops.txt
touch banned-players.txt
touch banned-ips.txt

# write the run script
cat >run.sh <<EOF
java -Xmx1024M -Xms1024M -jar /opt/minecraft/minecraft_server.jar nogui

# clean up
chown minecraft:minecraft *
chmod a+x run.sh

# do a motd too
cat >/etc/motd.tail <<EOF

This server has been deployed from Jed Smith's Minecraft StackScript.  To start
Minecraft, run the following as root:

  # su - minecraft -s /opt/minecraft/run.sh

screen is also installed if you prefer to run Minecraft in a screen.  It is a
very bad idea to run the Minecraft server as root; although there are no
currently-known vulnerabilities, you're better off insulating your server.

All files related to the server are in /opt/minecraft.