山城淑敬.jp

Since:2002.05.02 連絡先: shukukei@mojizuri.jp

CuberiteでMinecraftマルチプレイサーバを立ててみた。

Minecraftマルチプレイ用のサーバには、公式から提供されているもの以外に有志により開発が行われているものがあり、それぞれが様々な特徴を持っています。
今回はその中からC++言語で書かれ、軽量・高速・拡張性を謳っている“Cuberite”をインストールしてみました。


検証に使用したサーバのスペックは以下の通りです。
OS:Ubuntu 20.04 LTS (x86_64)
CPU:2コア
RAM:1GB

まずはファイアウォールを有効にしますが、後々の作業でSSHを使いたいため有効にしておきます。

# ufw enable
# ufw limit OpenSSH

カーネルパラメータを調整します。

# vi /etc/sysctl.conf
 net.ipv4.conf.default.rp_filter = 1
 net.ipv4.conf.default.accept_redirects = 0
 net.ipv4.conf.default.accept_source_route = 0
 net.ipv4.conf.default.send_redirects = 0
 net.ipv4.conf.all.rp_filter = 1
 net.ipv4.conf.all.accept_redirects = 0
 net.ipv4.conf.all.accept_source_route = 0
 net.ipv4.conf.all.send_redirects = 0
 net.ipv4.tcp_window_scaling = 1
 net.ipv4.tcp_moderate_rcvbuf = 1
 net.ipv4.tcp_no_metrics_save = 1
 net.ipv4.tcp_max_syn_backlog = 65535
 net.ipv4.tcp_sack = 1
 net.ipv4.tcp_timestamps = 0
 net.ipv4.tcp_fin_timeout = 5
 net.ipv4.tcp_syn_retries = 2
 net.ipv4.tcp_syncookies = 1
 net.ipv4.tcp_synack_retries = 3
 net.ipv4.tcp_tw_reuse = 1
 net.ipv4.tcp_rfc1337 = 1
 net.ipv4.tcp_keepalive_time = 65
 net.ipv4.tcp_keepalive_intvl = 65
 net.ipv4.tcp_keepalive_probes = 5
 net.ipv4.tcp_max_tw_buckets = 65535
 net.ipv4.icmp_ignore_bogus_error_responses = 1
 net.ipv4.icmp_echo_ignore_all = 1
 net.ipv4.icmp_echo_ignore_broadcasts = 1
 net.ipv4.ip_local_port_range = 9024 65535
 net.ipv4.ip_local_reserved_ports = 25565
 net.ipv4.tcp_mem = 182757 243679 365514
 net.ipv4.neigh.default.gc_thresh3 = 8192
 net.ipv6.conf.default.accept_redirects = 0
 net.ipv6.conf.default.accept_source_route = 0
 net.ipv6.conf.all.accept_redirects = 0
 net.ipv6.conf.all.accept_source_route = 0
 net.ipv6.neigh.default.gc_thresh3 = 8192
 net.core.somaxconn = 65535
 net.core.netdev_max_backlog = 182757
 kernel.sysrq = 0
 kernel.panic = 120
 kernel.dmesg_restrict = 1
 vm.swappiness = 1

txqueuelenを調整します。

# apt install ethtool
# vi /etc/systemd/system/txqueuelen.service
 [Unit]
 Description=txqueuelen
 After=network.target
 
 [Service]
 Type=oneshot
 User=root
 WorkingDirectory=/root
 ExecStart=/bin/bash -c "ip link set ens3 txqueuelen 60000"
 TimeoutSec=15
 Restart=no
 
 [Install]
 WantedBy=multi-user.target

# systemctl daemon-reload
# systemctl enable txqueuelen.service

TCP Segmentation Offloadを無効にします。

# vi /etc/udev/rules.d/50-eth_tso.rules
 ACTION=="add", SUBSYSTEM=="net", KERNEL=="eth*", RUN+="/sbin/ethtool -K %k tso off"
 ACTION=="add", SUBSYSTEM=="net", KERNEL=="ens*", RUN+="/sbin/ethtool -K %k tso off"

時刻同期の設定を行います。

# vi /etc/systemd/timesyncd.conf
 NTP=time.cloudflare.com
 FallbackNTP=ntp.jst.mfeed.ad.jp ntp.nict.jp time.google.com time.facebook.com

# systemctl restart systemd-timesyncd

Cuberiteをインストールします。
公式からバイナリファイルも提供されていますが、より良いパフォーマンスを得るためにソースコードからインストールすることにしました。

# apt install screen git make cmake gcc g++
# adduser minecraft --disabled-login
# su - minecraft
$ sh -c "$(wget -O - https://compile.cuberite.org)"
 
 Hello, this script will download and compile Cuberite.
 On subsequent runs, it will update Cuberite.
 The compilation and download will occur in the current directory.
 If you're updating, you should run: /compile.sh
 Compiling from source takes time, but it usually generates faster
 executables. If you prefer ready-to-use binaries or if you want
 more info, please visit:  https://cuberite.org/
 
         Choose compile mode:
         * (R)Release: Compiles normally.
                       Generates the fastest build.
 
         * (D)Debug:   Compiles in debug mode.
                       Makes your console and crashes more verbose.
                       A bit slower than Release mode. If you plan to help
                       development by reporting bugs, this is preferred.
 
 
 Choose compile mode: (r/d) (Default: "Release"): r
 
 Choose the number of compilation threads.
 You have 2 CPU threads.
 If you have enough RAM, it is wise to choose your CPU's thread count.
 Otherwise choose lower. Old Raspberry Pis should choose 1. If in doubt, choose 1.
 Please enter the number of compilation threads to use (Default: 1): 2
 
 #### Settings Summary ####
 Build Type:            Release
 Branch:                master (Currently the only choice)
 Compilation threads:   2
 CPU Threads:           2
 Previous compilation:  Not detected. We are assuming this is the first compile.sh run.
 Upstream Link:         https://github.com/cuberite/cuberite.git
 Upstream Repo:         origin
 
 After pressing ENTER, the script will connect to https://github.com/cuberite/cuberite.git
 to check for updates and/or fetch code. It will then compile your program.
 If you compiled before, make sure you're in the proper directory and that "Previous compilation" is detected.
 Press ENTER to continue...
 
 
 ~中略~
 
 
 -----------------
 Compilation done!
 
 Cuberite awaits you at:
 /home/minecraft/cuberite/build-cuberite/Server/Cuberite
 
 You can always update Cuberite by executing:
 /home/minecraft/cuberite/compile.sh
 
 Enjoy :)

さっそく実行してみます。

$ cd /home/minecraft/cuberite/build-cuberite/Server
$ ./Cuberite
 [11:44:36] --- Started Log ---
 [11:44:36] Cuberite local build build id: Unknown
 [11:44:36] from commit id: approx: 99d96337da2852b3163108334eba84e1ba806cb7 built at: approx: 2020-05-31 20:27:08 +0100
 [11:44:36] Creating new server instance...
 [11:44:36] Reading server config...
 [11:44:36] Regenerating settings.ini, all settings will be reset
 [11:44:36] Starting server...
 [11:44:36] Loading UpgradeBlockTypePalette...
 [11:44:36] Loading per-protocol palettes...
 [11:44:36] Loaded palettes for protocol versions: 1.13, 1.14.4
 [11:44:36] Compatible clients: 1.8.x, 1.9.x, 1.10.x, 1.11.x, 1.12.x
 [11:44:36] Compatible protocol versions 47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340
 [11:44:36] Regenerating webadmin.ini, all settings will be reset
 [11:44:36] WebServer: The server will run in unsecured HTTP mode.
 [11:44:36] Put a valid HTTPS certificate in file 'webadmin/httpscert.crt' and its corresponding private key to 'webadmin/httpskey.pem' (without any password) to enable HTTPS support
 [11:44:36] Creating default ranks...
 [11:44:36] Default ranks created.
 [11:44:36] Loaded 755 crafting recipes
 [11:44:36] Loaded 67 furnace recipes and 66 fuels
 [11:44:36] Loaded 49 brewing recipes
 [11:44:36] Cannot read world settings from "world/world.ini", defaults will be used.
 [11:44:36] Chosen a new random seed for world: 892289869
 [11:44:36] Cannot read world settings from "world_nether/world.ini", defaults will be used.
 [11:44:36] Chosen a new random seed for world: 1891556548
 [11:44:36] Cannot read world settings from "world_the_end/world.ini", defaults will be used.
 [11:44:36] Chosen a new random seed for world: 497712301
 [11:44:36] -- Loading Plugins --
 [11:44:36] Initialised Core
 [11:44:36] Initialized ChatLog v.3
 [11:44:36] -- Loaded 2 Plugins --
 [11:44:37] Preparing spawn (world): 13.00% (52/400; 51.23 chunks / sec)
 [11:44:38] Preparing spawn (world): 40.25% (161/400; 107.18 chunks / sec)
 [11:44:39] Preparing spawn (world): 69.00% (276/400; 113.86 chunks / sec)
 [11:44:40] Preparing spawn (world): 95.50% (382/400; 105.47 chunks / sec)
 [11:44:40] World "world": Generated spawnpoint position at {256.50, 66.00, 0.50}
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <-512 -512=""> took 0.039 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <-512 0=""> took 0.039 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <-512 512=""> took 0.062 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <0 -512=""> took 0.019 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <0 0=""> took 0.029 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <0 512=""> took 0.000 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <512 -512=""> took 0.049 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <512 0=""> took 0.000 sec
 [11:44:40] Stopwatch: CreateStructure for NetherFort at <512 512=""> took 0.033 sec
 [11:44:41] Preparing spawn (world_nether): 20.50% (82/400; 81.84 chunks / sec)
 [11:44:42] Preparing spawn (world_nether): 67.50% (270/400; 187.44 chunks / sec)
 [11:44:43] World "world_nether":Generated spawnpoint position at {24.50, 40.00, -23.50}
 [11:44:44] World "world_the_end": Did not find an acceptable spawnpoint. Generated a random spawnpoint position at {0.00, 168.00, 0.00}
 [11:44:44] WebAdmin is running on port(s) 8080
 [11:44:44] Startup complete, took 8033ms!
 help
 [11:54:45] Executing console command: "help"
 [11:54:45] ban                 - Bans a player by name.
 [11:54:45] banip               - Bans a player by IP.
 [11:54:45] banlist             - Shows a list of all players banned by name.
 [11:54:45] banlist ips         - Shows a list of all players banned by IP.
 [11:54:45] chunkstats          - Displays detailed chunk memory statistics
 [11:54:45] clear               - Clears the inventory of a player.
 [11:54:45] deop                - Resets a player's rank to default.
 [11:54:45] destroyentities     - Destroys all entities in all worlds
 [11:54:45] effect              - Adds an effect to a player.
 [11:54:45] enchant             - Adds an enchantment to a specified player's held item.
 [11:54:45] gamemode            - Changes a player's gamemode.
 [11:54:45] give                - Gives an item to a player.
 [11:54:45] gm                  - Changes a player's gamemode.
 [11:54:45] groups              - Shows a list of all the available groups.
 [11:54:45] help                - Shows the available commands
 [11:54:45] kick                - Kicks a player by name.
 [11:54:45] kill                - Kills a player.
 [11:54:45] list                - Shows a list of all connected players in a machine-readable format.
 [11:54:45] listgroups          - Shows a list of all the available groups.
 [11:54:45] listranks           - Shows a list of all the available ranks.
 [11:54:45] load                - Adds and enables the specified plugin
 [11:54:45] numchunks           - Shows number of chunks currently loaded.
 [11:54:45] op                  - Add a player to the Admin rank.
 [11:54:45] players             - Shows a list of all connected players.
 [11:54:45] plugins             - Shows a list of the plugins.
 [11:54:45] rank                - Shows or sets a player's rank.
 [11:54:45] ranks               - Shows a list of all the available ranks.
 [11:54:45] regen               - Regenerates a chunk.
 [11:54:45] reload              - Reloads all plugins
 [11:54:45] restart             - Restarts the server cleanly
 [11:54:45] save-all            - Saves all worlds.
 [11:54:45] say                 - Sends a chat message to all players.
 [11:54:45] seed                - Shows the seed of the given world name or default world, if not given.
 [11:54:45] spawn               - Returns a player to the spawn point.
 [11:54:45] spawnpoint          - Sets the spawn point for a player.
 [11:54:45] stop                - Stops the server cleanly
 [11:54:45] time                - Sets or displays the time.
 [11:54:45] time add            - Adds a given value to the current time.
 [11:54:45] time day            - Sets the time to day.
 [11:54:45] time night          - Sets the time to night.
 [11:54:45] time query daytime  - Displays the current time.
 [11:54:45] time query gametime - Displays the amount of time elapsed since start.
 [11:54:45] time set            - Sets the time to a given value.
 [11:54:45] toggledownfall      - Toggles the weather between clear skies and rain.
 [11:54:45] tp                  - Teleports a player.
 [11:54:45] tps                 - Returns the tps (ticks per second) from the server.
 [11:54:45] unban               - Unbans a player by name.
 [11:54:45] unbanip             - Unbans a player by IP.
 [11:54:45] unload              - Disables the specified plugin
 [11:54:45] unloadchunks        - Unloads all unused chunks.
 [11:54:45] unrank              - Resets a player's rank to default.
 [11:54:45] unsafegive          - Gives an item to a player, even if the item is blacklisted.
 [11:54:45] weather             - Changes the world's weather.
 [11:54:45] whitelist           - Manages the whitelist.
 [11:54:45] whitelist add       - Adds a player to the whitelist.
 [11:54:45] whitelist list      - Shows a list of all players on the whitelist.
 [11:54:45] whitelist off       - Turns off whitelist processing.
 [11:54:45] whitelist on        - Turns on whitelist processing.
 [11:54:45] whitelist remove    - Removes a player from the whitelist.

実際の運用ではSystemdを使いたいため、設定を行います。

# vi /etc/systemd/system/cuberite.service
 [Unit]
 Description=Cuberite server
 After=network.target
 
 [Service]
 Type=simple
 User=minecraft
 WorkingDirectory=/home/minecraft/cuberite/build-cuberite/Server
 ExecStart=/usr/bin/sh -c '/usr/bin/screen -DmS CuberiteScreen /home/minecraft/cuberite/build-cuberite/Server/Cuberite'
 ExecStop=/usr/bin/screen -S CuberiteScreen -X eval 'stuff "save-all"\\015'
 ExecStop=/usr/bin/screen -S CuberiteScreen -X eval 'stuff "stop"\\015'
 TimeoutStartSec=15
 TimeoutStopSec=60
 Restart=on-failure
 
 [Install]
 WantedBy=multi-user.target

# systemctl daemon-reload

主な操作方法は次の通りです。
・起動させるとき
  # systemctl start cuberite.service
・終了させるとき
  # systemctl stop cuberite.service
・サーバ起動時に自動的に起動させるとき
  # systemctl enable cuberite.service
・現在の状態を確認するとき
  # systemctl status cuberite.service
・ログを確認するとき
  # journalctl -u cuberite.service

さて、ファイアウォールの設定を行ってクライアントから接続してみます。

# ufw allow in proto tcp to any port 25565

f:id:shukukei:20200907223011p:plain


ちゃんと繋がりました。

f:id:shukukei:20200907223047p:plain


次はWebAdmin機能を試してみます。
初期状態では管理者ユーザが作成されていないので、ユーザを作成します。
変更内容を適用するためにはCuberiteを再起動する必要があるようです。

$ vi webadmin.ini
 ; This file controls the webadmin feature of Cuberite
 ; It specifies whether webadmin is enabled, and what logins are allowed.
 ; Username format: [User:*username*]
 ; Password format: Password=*password*; for example:
 ; [User:admin]
 ; Password=admin
 ; Please restart Cuberite to apply changes made in this file!
 
 [User:master]
 Password=passwd
 
 [WebAdmin]
 Ports=8080
 Enabled=1
・ログイン前

f:id:shukukei:20200907223129p:plain

・ログイン後

f:id:shukukei:20200907223203p:plain

・Chat

f:id:shukukei:20200907223240p:plain

・Manage Server

f:id:shukukei:20200907223319p:plain

・Permissions

f:id:shukukei:20200907223356p:plain

・Player Ranks

f:id:shukukei:20200907223441p:plain

・Players

f:id:shukukei:20200907223512p:plain

・Plugins

f:id:shukukei:20200907223552p:plain

・Ranks

f:id:shukukei:20200907223623p:plain

・Server Settings

f:id:shukukei:20200907223701p:plain

・Time & Weather

f:id:shukukei:20200907223738p:plain

・Whitelist

f:id:shukukei:20200907223817p:plain


設定についてはユーザーズマニュアルを参照して行います。
https://book.cuberite.org/

・サーバ負荷(接続数:1人)

f:id:shukukei:20200907223845p:plain