🍓

【OCI】Ubuntu で Minecraft 統合版(Bedrock) Server を動かす

2023/05/23に公開

OCI 上の Ubuntu で Minecraft 統合版(Bedrock) Server を作る

OCI の無料枠はすごいですねー。
無料枠で、Ubuntu Server を立てて運用できます。
子供と遊ぶための Minecraft 統合版(Bedrock) Server を立てましたので、そのメモを残しておきます。

簡単なまとめ

  • OCI の ubuntu は iptables が既定で有効化されているため、ゲストOSでのポート開放が必要な点に、注意 ※Azure と違う部分で、ハマった。
  • Minecraft は 統合版(Bedrock)

Step1. OCI で Ubuntu Server をデプロイする

  1. OCI の Portal へログインして、左上 ハンバーガーメニュー から、 [ コンピュート ] - [ インスタンス ] と進み、 [ インスタンスの作成 ] をクリックします。
    Portal 中ほどにある [ リソースの起動 ] - [ VMインスタンスの作成 ] のクイックリンクから、インスタンスの作成画面に進んでも構いません。


OCI の Portal 画面

  1. [ コンピュート・インスタンスの作成 ] 画面で、以下のような感じで ubuntu インスタンスを作成します。
  • 名前 : minecraft や vm001 など、任意の名前を入力
  • コンパートメントに作成 : 既定のまま
  • 配置 : 既定のまま
  • イメージ : Ubuntu 20.04 LTS
  • インスタンスタイプ : VM.Standard.E2.1.Micro
OSイメージ選択画面


OSイメージ選択画面

  • ネットワーキング : 新規仮想クラウド・ネットワークの作成
    • 新規仮想クラウド・ネットワーク名 : minecraft-vnet など、任意の名前を入力
    • サブネットの作成 : minecraft-vnet-subnet など、任意の名前を入力
    • CIDR ブロック : 任意
    • パブリックIPv4アドレス : 既定のまま ( "割当て" を選択した状態にすること)
ネットワーク選択画面


ネットワーク選択画面

  • SSH キーの追加 : 秘密キーを保存しておく


仮想マシンの作成画面

  1. [作成] をクリックし、1 - 2 分待つと、デプロイが完了しました。

    作成された仮想マシン

Step2. OCI のネットワークセキュリティリストを設定する(Minecraft 用のポートを開放)


OSイメージ選択画面


OSイメージ選択画面

Step3. Ubuntu Server に ssh で接続し、 Minecraft 統合版(Bedrock) Server を構成する

OCI の Ubuntu では、既定のユーザとして ubuntu が設定されています。
Step1. で ダウンロードしておいた key を用いて、ssh で接続します。

Ubuntu Server へ接続できたら、以下のコマンドを順に実行していき、Minecraft 統合版(Bedrock) Server を構成します。

bash
# アップデートする
sudo apt update

# unzip をインストールする
sudo apt install unzip

# 特権昇格して iptables を編集する
sudo vim /etc/iptables/rules.v4

# Minecraft 用の UDP 19132 の受信許可を、ssh 許可ルールの次行に追加する
# -A INPUT -p udp --dport 19132 -j ACCEPT # Minecraft Bedrock Server
#

# iptables を再起動する
sudo iptables-restore < /etc/iptables/rules.v4

# Minecraft 用ユーザー(ここでは bedrockadmin)を作成する
# Password は、適当に設定し、 anser は、全て Enter で OK
sudo adduser bedrockadmin

# Minecraft 用フォルダを作成し、所有者を bedrockadmin に変更する
sudo mkdir /opt/bedrock
sudo chown bedrockadmin:bedrockadmin /opt/bedrock/

# Minecraft 用ユーザーに切り替える
su - bedrockadmin
cd /opt/bedrock

# UBUNTU (LINUX) 用 MINECRAFT 専用サーバーのソフトウェア をダウンロードする。
# なお、最新版は公式サイト https://www.minecraft.net/ja-jp/download/server/bedrock で確認すること。
# 2023/05/21 時点では 1.19.83.01 が最新版でした。
curl -L -O https://minecraft.azureedge.net/bin-linux/bedrock-server-1.19.83.01.zip

# ダウンロードしたら、unzip コマンドで解凍する。
unzip bedrock-server-1.19.83.01.zip -d bedrock-server

# 解凍したフォルダへ移動し、サーバーを起動する。
cd bedrock-server
LD_LIBRARY_PATH=. ./bedrock_server

# 統合版のクライアントから接続して動作を確認します。
# なお、サーバーを終了させるには、コマンドラインで「stop」と入力します。
LD_LIBRARY_PATH=. ./bedrock_server 実行結果 sample
[2023-05-22 15:00:44:669 INFO] Starting Server
[2023-05-22 15:00:44:669 INFO] Version 1.19.83.01
[2023-05-22 15:00:44:669 INFO] Session ID 2eb41965-0805-4947-8a66-afe9ecb6f1bb
[2023-05-22 15:00:44:669 INFO] Build ID 1.19.83
[2023-05-22 15:00:44:669 INFO] Branch r/19_u8
[2023-05-22 15:00:44:669 INFO] Commit ID c184ec32505984a38099320b43fd54425c2e4f5d
[2023-05-22 15:00:44:669 INFO] Configuration Publish
[2023-05-22 15:00:44:669 INFO] Level Name: Bedrock level
[2023-05-22 15:00:44:670 INFO] Game mode: 0 Survival
[2023-05-22 15:00:44:670 INFO] Difficulty: 1 EASY
[2023-05-22 15:00:44:770 INFO] opening worlds/Bedrock level/db
[2023-05-22 15:00:46:598 INFO] IPv4 supported, port: 19132: Used for gameplay and LAN discovery
[2023-05-22 15:00:46:598 INFO] IPv6 supported, port: 19133: Used for gameplay
[2023-05-22 15:00:46:341 INFO] Server started.
/etc/iptables/rules.v4 のサンプル
/etc/iptables/rules.v4
# CLOUD_IMG: This file was created/modified by the Cloud Image build process
# iptables configuration for Oracle Cloud Infrastructure

# See the Oracle-Provided Images section in the Oracle Cloud Infrastructure
# documentation for security impact of modifying or removing these rule

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [463:49013]
:InstanceServices - [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p udp --sport 123 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p udp --dport 19132 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
-A OUTPUT -d 169.254.0.0/16 -j InstanceServices
-A InstanceServices -d 169.254.0.2/32 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.2.0/24 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.4.0/24 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.5.0/24 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.2/32 -p tcp -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp -m udp --dport 53 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p tcp -m tcp --dport 53 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.3/32 -p tcp -m owner --uid-owner 0 -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.4/32 -p tcp -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p tcp -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp -m udp --dport 67 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp -m udp --dport 69 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp --dport 123 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.0/16 -p tcp -m tcp -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j REJECT --reject-with tcp-reset
-A InstanceServices -d 169.254.0.0/16 -p udp -m udp -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j REJECT --reject-with icmp-port-unreachable
COMMIT

Step3. 統合版のクライアントから接続して動作を確認する

統合版のクライアントから、Minecraft Server に接続して動作を確認します。
Ubuntu Server のパブリック IP アドレスを確認し、統合版のクライアントから接続します。

[サーバー] タブの、[サーバーを追加] をクリックします。

[サーバーアドレス] に、Ubuntu Server のパブリック IP アドレスを入力します。
サーバー名はわかりやすい名前を適当に付けておきます。ポートは 統合版既定の 19132 のまま、[保存] をクリックします。

[サーバー] タブに、追加したサーバーが表示されます。
追加後、数秒程度の間は、サーバーを探しているような表示がなされますが、しばらくすると、以下キャプチャのようにバージョン情報等が表示されるはずです。
選択し、[サーバーに参加] をクリックで、接続できるます。

はろーわーるど!

なお、サーバー側はというと、ユーザーが接続してきたタイミングで、Ubuntu Server の コンソール に ユーザー名と xuid が表示されるはずです。
Step4 では、この ユーザー名と xuid を用いて、接続を可能なユーザーを制御します。

Step4. Minecraft Server へ接続可能なユーザーを限定する

現状では、誰でも Minecraft Server に接続できてしまいます。
allow-list を設定し、Minecraft Server へ接続可能なユーザーを限定します。
server.properties で allow-list を有効 ( allow-list=true )にし、allowlist.json に接続を許可するユーザーを記述します。

/opt/bedrock/bedrock-server/server.properties
#... 中略

allow-list=true
# If true then all connected players must be listed in the separate allowlist.json file.
# Allowed values: "true" or "false"

#... 中略
/opt/bedrock/bedrock-server/allowlist.json
[
    {"ignoresPlayerLimit":false,"name":"<user01>","xuid":"xxxxxxxxxxxxxxxx"},
    {"ignoresPlayerLimit":false,"name":"<user02>","xuid":"yyyyyyyyyyyyyyyy"}
]

今回私の環境では、接続するのは 私自身 と こども の2名なので、上記のように記述しています。

Step5. Minecraft Server が自動起動するように構成する

次のような、Minecraft Server を Screen で起動させるスクリプトを作成します。
このスクリプトを crontab に登録(crontab -e)し、サーバー起動時に自動起動するように設定します。

/opt/bedrock/bedrock-server/bedrock_script.sh
#!/bin/bash
# bedrock_serverインストールディレクトリ
BEDROCK_PATH='/opt/bedrock/bedrock-server'

# session名
SESSION_NAME='bds'

cd ${BEDROCK_PATH}
LD_LIBRARY_PATH=. screen -dmS ${SESSION_NAME} ./bedrock_server
crontab -e
@reboot sh /opt/bedrock/bedrock-server/bedrock_script.sh

Server を再起動させ、クライアントから接続できることを確認します。
なお、su - bedrockadmin でユーザーを切り替えた後、 screen -r で Minecraft Server のコンソールに接続できます。

Step6. OCI のネットワークセキュリティリストを設定する(ssh 用のポートを閉じる)

既定では、0.0.0.0/0 宛に ssh が許可されているため、セキュリティリスクがあります。
Step5 までの設定が完了したら、Step2 同様の手順で、OCI のポータル画面より ssh用の TCP 22 番ポート を閉じておきます。

これで終わりです!
お疲れ様でした!

参考リンク

こちらが非常に参考になりました。

https://vpslife.server-memo.net/ubuntu_bedrock_minecraft_install/#toc10

https://pcvogel.sarakura.net/2022/05/14/35345

https://qiita.com/hnishi/items/3190f2901f88e2594a5f

Discussion