『linux』 minecraft server 快速部署脚本

按照纯净版服务器的部署方法写了一个minecraft+mcrcon在linux上的快速部署脚本
理论上支持deepin系和centos系的系统,目前只完成了ubuntu的测试。

使用需要在minecraft官网获取下载server.jar的链接

mcrcon的使用方式是/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p 密码(有引导设置)

#!/bin/bash
RED="\033[31m"      # Error message

colorEcho() {
    echo -e "${1}${@:2}${PLAIN}"
}

serverpath="/opt/minecraft/server/"
mcrconpath="/opt/minecraft/tools/mcrcon/"
servicepath="/etc/systemd/system/"

checkSystem() {
    uid=$(id -u)
    if [[ $uid -ne 0 ]]; then
        colorEcho $RED " 请以root身份执行该脚本"
        exit 1
    fi

    res=$(command -v yum)
    if [[ "$res" = "" ]]; then
        res=$(command -v apt)
        if [[ "$res" = "" ]]; then
            colorEcho $RED " 不受支持的Linux系统"
            exit 1
        fi
        PMT="apt"
        CMD_INSTALL="apt install -y "
        CMD_UPGRADE="apt update ; apt upgrade -y ; apt autoremove -y"
    else
        PMT="yum"
        CMD_INSTALL="yum install -y "
        CMD_UPGRADE="yum update -y"
    fi
    res=$(command -v systemctl)
    if [[ "$res" = "" ]]; then
        colorEcho $RED " 系统版本过低,请升级到最新版本"
        exit 1
    fi
}

prepare(){
    $CMD_UPGRADE
    $CMD_INSTALL curl wget openjdk-17-jdk git nano vim build-essential
    useradd -r -m -U -d /opt/minecraft -s /bin/bash minecraft
    su - minecraft -c "mkdir -p ~/{tools,server}"
}

mcrcon_b(){
    su - minecraft -c "cd ~/tools && git clone https://github.com/Tiiffi/mcrcon.git"
    su - minecraft -c "cd ~/tools/mcrcon && gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c"
    alias mcrcon="/opt/minecraft/tools/mcrcon/mcrcon"
}

server_jar(){
    read -p "请将server.jar下载链接复制到此处:" server
    su - minecraft -c "wget $server -P $serverpath"
    read -p "请输入希望给minecraft分配的最大内存(MB):" Max
    cd $serverpath
    su - minecraft -c "cd ${serverpath} && java -Xms512M -Xmx${Max}M -jar server.jar nogui"
    sed -i "s|false|true|g" ${serverpath}eula.txt
    read -p "请输入mcrcon管理密码:" mcrcon_passwd
    sed -i "s|strong-password|$mcrcon_passwd|g" ${serverpath}server.properties
    sed -i "s|enable-rcon=false|enable-rcon=true|g" ${serverpath}server.properties
}
service(){
    echo '[Unit]
Description=Minecraft Server
After=network.target
[Service]
User=minecraft
Nice=1
KillMode=none
SuccessExitStatus=0 1
ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
WorkingDirectory=/opt/minecraft/server
ExecStart=/usr/bin/java -XmxTTTM -Xms512M -jar server.jar nogui
ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password stop
[Install]
WantedBy=multi-user.target' >> ${servicepath}minecraft.service
    sed -i "s|TTT|$Max|g" ${servicepath}minecraft.service
    sed -i "s|strong-password|$mcrcon_passwd|g" ${servicepath}minecraft.service
    read -p "是否允许开机自启动[y/n]:" res
    if [[ "$res" = "y" ]]; then
	systemctl enable minecraft.service
    fi	
}

main(){
    checkSystem
    prepare
    mcrcon_b
    server_jar
    service
}

main

分享一个我搭建linux mc server的方法

server

How to Set Up a Dedicated Minecraft Server on Linux

里面大概的思路步骤跟这个一样,但是细节不同。首先是安装java有更简单的方法,我直接java --version检测没有java之后就给了我命令直接apt install了。另外mc server的最新版本可以从mc server官网这里找到链接。

配置文件参数说明

这里面至少要改“online-mode”=false 这样可以跳过检查是否是正版mc,直接白嫖

client

hmcl

这个是mc客户端启动器,用户使用离线模式启动游戏,再进入联机模式,输入服务器ip即可

一个问题是感觉Linux server上面修改一些游戏选项不是很方便,如想切换生存/创造模式,有待研究

后续脚本中想尝试对server.properties的状态获取和直接修改