diff --git a/README.md b/README.md index 00f92a0..f16762b 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,11 @@ Everything lives on a ZFS dataset mounted at `/minecraft`. --accept-eula ``` +Add `-p 25599` to run on a non-default port. It's written into +`server.properties`, opened in firewalld, and shown in the player guides — which +print a bare hostname on 25565 and `host:port` on anything else, since Minecraft +assumes the default and a needless `:25565` confuses people. + The NeoForge version is resolved automatically — the newest stable build in the series matching your Minecraft version, from the NeoForged maven. Pin one with `-N 21.1.248` if you'd rather not track latest. diff --git a/deploy.sh b/deploy.sh index a2167e4..3d3f04e 100755 --- a/deploy.sh +++ b/deploy.sh @@ -14,7 +14,7 @@ set -euo pipefail NAME=""; AUTHOR=""; BASEURL=""; NFVER="latest"; MCVER="1.21.1" -SHARE="/minecraft"; XMX="10G"; XMS="10G"; ACCEPT_EULA="" +SHARE="/minecraft"; XMX="10G"; XMS="10G"; ACCEPT_EULA=""; PORT="25565" HERE="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" die() { echo "error: $*" >&2; exit 1; } @@ -29,6 +29,7 @@ while [ $# -gt 0 ]; do -N|--neoforge) NFVER="${2:-}"; shift 2 ;; -m|--mc) MCVER="${2:-}"; shift 2 ;; -s|--share) SHARE="${2:-}"; shift 2 ;; + -p|--port) PORT="${2:-}"; shift 2 ;; -X|--xmx) XMX="${2:-}"; shift 2 ;; -x|--xms) XMS="${2:-}"; shift 2 ;; --accept-eula) ACCEPT_EULA="--accept-eula"; shift ;; @@ -51,7 +52,7 @@ sudo -v || die "sudo is required" step "1/2 Minecraft systemd service" sudo "$HERE/mc-service-setup.sh" \ - -s "$SHARE" -A "$USER" -X "$XMX" -x "$XMS" -m "$MCVER" \ + -s "$SHARE" -A "$USER" -X "$XMX" -x "$XMS" -m "$MCVER" -p "$PORT" \ ${NFVER:+-N "$NFVER"} ${ACCEPT_EULA} step "2/2 packwiz pack and player guides" @@ -59,10 +60,10 @@ step "2/2 packwiz pack and player guides" # the pack setup under the new group explicitly rather than telling you to log # out and back in. if id -nG "$USER" | tr ' ' '\n' | grep -qx minecraft; then - "$HERE/packwiz-setup.sh" -n "$NAME" -a "$AUTHOR" -m "$MCVER" -u "$BASEURL" -s "$SHARE" + "$HERE/packwiz-setup.sh" -n "$NAME" -a "$AUTHOR" -m "$MCVER" -u "$BASEURL" -s "$SHARE" -p "$PORT" else sg minecraft -c "$(printf '%q ' "$HERE/packwiz-setup.sh" -n "$NAME" -a "$AUTHOR" \ - -m "$MCVER" -u "$BASEURL" -s "$SHARE")" + -m "$MCVER" -u "$BASEURL" -s "$SHARE" -p "$PORT")" fi step "Done" diff --git a/mc-service-setup.sh b/mc-service-setup.sh index d7e8cc2..955153e 100755 --- a/mc-service-setup.sh +++ b/mc-service-setup.sh @@ -18,6 +18,7 @@ set -euo pipefail SHARE="/minecraft" MCUSER="minecraft" +PORT="25565" # Minecraft server port XMS="10G" XMX="10G" NFVER="" # version, or "latest" to resolve one; empty = don't install @@ -36,6 +37,7 @@ while [ $# -gt 0 ]; do case "$1" in -s|--share) SHARE="${2:-}"; shift 2 ;; -U|--user) MCUSER="${2:-}"; shift 2 ;; + -p|--port) PORT="${2:-}"; shift 2 ;; -x|--xms) XMS="${2:-}"; shift 2 ;; -X|--xmx) XMX="${2:-}"; shift 2 ;; -N|--neoforge) NFVER="${2:-}"; shift 2 ;; @@ -48,6 +50,8 @@ while [ $# -gt 0 ]; do done [ "$(id -u)" -eq 0 ] || die "run this with sudo — it creates a user and writes unit files" +[[ "$PORT" =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ] \ + || die "-p wants a port between 1 and 65535, got: $PORT" # ----------------------------------------------------------------- share ---- @@ -170,6 +174,31 @@ if [ ! -f "$SHARE/eula.txt" ] || ! grep -q '^eula=true' "$SHARE/eula.txt"; then fi fi +# ------------------------------------------------------------ properties ---- + +# Update in place so a server.properties the server already generated keeps all +# its other settings; create the key if it is not there yet. +set_prop() { + local key="$1" val="$2" f="$SHARE/server.properties" + if [ -f "$f" ] && grep -q "^${key}=" "$f"; then + sed -i "s|^${key}=.*|${key}=${val}|" "$f" + else + printf '%s=%s\n' "$key" "$val" >> "$f" + fi + chown "$MCUSER":"$MCUSER" "$f" +} + +info "setting server-port=$PORT" +set_prop server-port "$PORT" + +if systemctl is-active --quiet firewalld; then + info "opening $PORT/tcp" + firewall-cmd --quiet --permanent --add-port="$PORT/tcp" + firewall-cmd --quiet --reload +else + warn "firewalld is not running — make sure $PORT/tcp is reachable" +fi + # -------------------------------------------------------------- jvm args ---- # Written fresh so -X/-x actually take effect on re-runs. G1 tuning is the @@ -258,7 +287,7 @@ echo info "installed. Start it with:" cat < SERVER_ADDR="" # defaults to the hostname of $SITE_URL +MCPORT="25565" # Minecraft port, shown in the guides GUIDES=1 # write the player setup guides die() { echo "error: $*" >&2; exit 1; } @@ -51,6 +52,7 @@ while [ $# -gt 0 ]; do -U|--site-url) SITE_URL="${2:-}"; shift 2 ;; -P|--pack-url) PACK_URL="${2:-}"; shift 2 ;; -A|--server) SERVER_ADDR="${2:-}"; shift 2 ;; + -p|--port) MCPORT="${2:-}"; shift 2 ;; --no-guides) GUIDES=0; shift ;; -h|--help) usage 0 ;; *) echo "unknown option: $1" >&2; usage 1 ;; @@ -109,6 +111,14 @@ if [ -n "$SITE_URL" ]; then fi PACK_URL="${PACK_URL%/}" +# Minecraft assumes 25565, so only show a port when it is not that — a +# needless ":25565" in the address is a classic source of player confusion. +if [ "$MCPORT" = "25565" ]; then + SERVER_DISPLAY="$SERVER_ADDR" +else + SERVER_DISPLAY="$SERVER_ADDR:$MCPORT" +fi + [ -n "$WEBROOT" ] || WEBROOT="$SHARE" if (( GUIDES )); then @@ -371,7 +381,7 @@ font-size:.84rem;color:var(--muted)}
  • MC $MCVER
  • Loader NeoForge
  • Java 21
  • -
  • Server $SERVER_ADDR
  • +
  • Server $SERVER_DISPLAY
  • @@ -441,7 +451,7 @@ font-size:.84rem;color:var(--muted)}

    Hit Launch. The first run downloads the full mod set, so give it a minute. At the title screen go to MultiplayerAdd Server:

    -
    $SERVER_ADDR
    +
    $SERVER_DISPLAY

    That's it. From here on just launch normally — updates arrive on their own.