レオハ○レスでもMinecraftServer
OverView
1. はじめに
今回のレシピを紹介します。
Used | Name |
---|---|
OS |
Ubuntu 16.04 |
lubuntu-desktop |
|
VPS Service |
ABLENET |
OpenVPN 2.4.7 |
CentOSのほうが日本語の情報や記事が多かったのですが、触ったことのあるUbuntuを優先しました。
Ubuntuは18.04_LTSが最新で出ていますが、ABLENETで18.04をインストールする際不具合連発だったので避けました。
それと、今回はUbuntuですが、GUIにはlubuntu-desktopを使用しています。だって軽いんだもの。
Warning
|
これから先は、Ubuntuのインストール及び、 lubuntu-desktopをインストールし終わっている前提で進めます。 |
2. OpenVPNのインストール
まずはOpenVPNをインストールします。
バージョンを気にしなければapt install openvpnでOKですが、今回は最新版を使用するので
https://community.openvpn.net/openvpn/wiki/OpenvpnSoftwareReposに従って設定を行います。
$ wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg|apt-key add -
$ echo "deb http://build.openvpn.net/debian/openvpn/stable xenial main" > /etc/apt/sources.list.d/openvpn-aptrepo.list
$ apt update
$ apt install openvpn
コマンドを実行すると、OpenVPNがインストールされると思います。
インストールされるバージョンには最新版を選択するよう指定しているので、もしかしたらバージョンが2.4.7より先かもしれないですが、確認してみましょう。
$ openvpn --version
OpenVPN 2.4.7 x86_64 ...(以下略)
と表示されればOKです。
もちろん、2.4.7以降でも問題ありません。最新のバージョンが落とされていれば結構です。
これでOpenVPNのインストールは完了です。
3. EasyRSAのインストール
OpenVPNで使用する公開鍵や認証局等に必要なのが、このEasyRSAです。
昔はOpenVPNに同梱されていたのですが、いつの間にか別々でインストールする必要が出てきたみたいです。
EasyRSAをaptでインストールするともれなく2.X系が手に入るのですが、今回は3.X系をインストールします。
-
https://github.com/OpenVPN/easy-rsa/releasesから最新のEasyRSAのtgzを落とします。
-
落としたtgzは解凍
-
出来たフォルダの名前をeasyrsaにリネーム。
-
/etc/openvpn/下に移動します。
すると、以下のディレクトリ構成になっているはず。
/ └ etc └ openvpn └ easyrsa
これでEasyRSAのインストールは完了です。
4. EasyRSAの設定
4.1. 認証局
$ cd /etc/openvpn/easyrsa
4.1.1. 初期化
$ ./easyrsa init-pki
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easyrsa/pki
4.1.2. 作成
pkiを初期化したので、CAを作成します。
$ ./easyrsa build-ca
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2g 1 Mar 2016
Enter New CA Key Passphrase:
Re-Enter New CA Key Passphrase:
Generating RSA private key, 2048 bit long modulus
..............+++
...............+++
e is 65537 (0x10001)
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/openvpn/easyrsa/pki/ca.crt
root@h150-66-20-140:/etc/openvpn/easyrsa#
ここで、
を作成します。
$ ./easyrsa build-ca
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2g 1 Mar 2016
Enter New CA Key Passphrase:
Re-Enter New CA Key Passphrase:
Easy-RSA error:
Passphrases do not match.
CA作成に成功すれば、
が作成されます。
4.3. サーバー用秘密鍵・証明書の作成
今回はserver1という名前のサーバーファイルを作成します。
引数にnopassを指定しないと、サービス起動時にパスワードを要求されたりするので、
厳格でない限り、nopassで問題ないでしょう。 作成には、CA作成時に入力したパスフレーズが要求されるので、注意です。
$ ./easyrsa build-server-full server1 nopass
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2g 1 Mar 2016
Generating a 2048 bit RSA private key
.....................................................+++
.................................................+++
writing new private key to '/etc/openvpn/easyrsa/pki/private/server1.key'
-----
Using configuration from /etc/openvpn/easyrsa/pki/safessl-easyrsa.cnf
Enter pass phrase for /etc/openvpn/easyrsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'server1'
Certificate is to be certified until Feb 7 23:58:52 2022 GMT (1080 days)
Write out database with 1 new entries
Data Base Updated
成功すれば、
が作成されます。以下のディレクトリ構造になると思います。
4.4. クライアント用秘密鍵・証明書の作成
今度はクライアントファイルを作成します。
作成には、CA作成時に入力したパスフレーズが要求されるので、注意です。
サーバーのときと違って、引数にnopassをつけなくても問題ありません。
むしろ、nopassは外すことを推奨します。第三者が勝手に接続しないためです。
PEM passwordは新たなパスワードを入力してください。
その後に入力するのがCA作成時に設定したパスフレーズです。
Note
|
PEM passwordは後々クライアント接続するときに使用するので、メモしておいてください。 |
$ ./easyrsa build-client-full client1
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2g 1 Mar 2016
Generating a 2048 bit RSA private key
..............+++
...........................................................+++
writing new private key to '/etc/openvpn/easyrsa/pki/private/client1.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
Using configuration from /etc/openvpn/easyrsa/pki/safessl-easyrsa.cnf
Enter pass phrase for /etc/openvpn/easyrsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'client1'
Certificate is to be certified until Feb 8 00:41:12 2022 GMT (1080 days)
Write out database with 1 new entries
Data Base Updated
成功すれば、
が作成されます。以下のディレクトリ構造になると思います。
5. OpenVPNの設定
5.1. HMAC鍵の作成
$ openvpn --genkey --secret /etc/openvpn/ta.key
5.2. サーバー設定ファイルの作成
server.confを作成していきます。 サンプルファイルがあるので、作業ディレクトリにcfgフォルダを作ってそこにコピー、解凍します。
$ mkdir /etc/openvpn/cfg
$ cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/cfg/server.conf.gz
$ gunzip /etc/openvpn/cfg/server.conf.gz
$ mv /etc/openvpn/cfg/server.conf /etc/openvpn/cfg/server1.conf
解凍作業まで完了したら、server1.confを開きます。
以下はserver1.confから設定部分のみを切り出したものです。
;local a.b.c.d port 1194 ;proto tcp proto udp ;dev tap dev tun ;dev-node MyTap ca ca.crt cert server.crt key server.key dh dh2048.pem ;topology subnet server 10.8.0.0 255.255.255.0 ifconfig-pool-persist ipp.txt ;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100 ;server-bridge ;push "route 192.168.10.0 255.255.255.0" ;push "route 192.168.20.0 255.255.255.0" ;client-config-dir ccd ;route 192.168.40.128 255.255.255.248 ;client-config-dir ccd ;route 10.9.0.0 255.255.255.252 ;learn-address ./script ;push "redirect-gateway def1 bypass-dhcp" ;push "dhcp-option DNS 208.67.222.222" ;push "dhcp-option DNS 208.67.220.220" ;client-to-client ;duplicate-cn keepalive 10 120 tls-auth ta.key 0 cipher AES-256-CBC ;compress lz4-v2 ;push "compress lz4-v2" ;comp-lzo ;max-clients 100 ;user nobody ;group nobody persist-key persist-tun status openvpn-status.log ;log openvpn.log ;log-append openvpn.log verb 3 ;mute 20 explicit-exit-notify 1
不要なものを削ぎ落とし、手を加えたのが下記です。
今回はtapではなくtun、つまりルーティングモードでVPNを運用します。
port 1937 proto udp dev tun ca ca.crt cert server1.crt key server1.key dh dh.pem topology subnet server 192.168.10.0 255.255.255.0 ifconfig-pool-persist ipp.txt ;client-config-dir ccd push "route 192.168.10.0 255.255.255.0" #192.168.10.XXXへのアクセスはVPN経由にする push "dhcp-option DNS 192.168.10.1" client-to-client keepalive 10 120 tls-auth ta.key 0 cipher AES-256-GCM compress lz4-v2 push "compress lz4-v2" max-clients 10 user nobody group nogroup #sampleだとnobodyになっているのですがエラー出るのでnogroupに変えてください(泣 persist-key persist-tun status openvpn-status.log ;log openvpn.log ;log-append openvpn.log verb 4 ;mute 20 explicit-exit-notify 1
$ cp /etc/openvpn/cfg/server1.conf /etc/openvpn/server1.conf
$ cp /etc/openvpn/easyrsa/pki/ca.crt /etc/openvpn/ca.crt
$ cp /etc/openvpn/easyrsa/pki/issued/server1.crt /etc/openvpn/server1.crt
$ cp /etc/openvpn/easyrsa/pki/private/server1.key /etc/openvpn/server1.key
$ cp /etc/openvpn/easyrsa/pki/dh.pem /etc/openvpn/dh.pem
以下のディレクトリ構成になるはず。
/ └ etc └ openvpn ├ easyrsa │ └ pki │ ├ ca.crt │ ├ issued │ │ ├ client1.crt │ │ └ server1.crt │ ├ private │ │ ├ ca.key │ │ ├ client1.key │ │ └ server1.key │ ├ req │ │ ├ client1.req │ │ └ server1.req │ └ dh.pem ├ ta.key ├ server1.conf ├ ca.crt ├ server1.crt ├ server1.key ├ dh.pem └ cfg └ server1.conf
5.3. クライアント設定ファイルの作成
今度はクライアントに配布するclientX.ovpnを作成していきます。
$ cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/cfg/client1.ovpn
コピーしたclient1.ovpnを開きます。
以下はserver1.confから設定部分のみを切り出したものです。
client ;dev tap dev tun ;dev-node MyTap ;proto tcp proto udp remote my-server-1 1194 ;remote my-server-2 1194 ;remote-random resolv-retry infinite nobind ;user nobody ;group nobody persist-key persist-tun ;http-proxy-retry # retry on connection failures ;http-proxy [proxy server] [proxy port #] ;mute-replay-warnings ca ca.crt cert client.crt key client.key remote-cert-tls server tls-auth ta.key 1 cipher AES-256-CBC #comp-lzo verb 4 ;mute 20
不要なものを削ぎ落とし、手を加えたのが下記です。
以下のディレクトリ構成になるはず。
/ └ etc └ openvpn ├ easyrsa │ └ pki │ ├ ca.crt │ ├ issued │ │ ├ client1.crt │ │ └ server1.crt │ ├ private │ │ ├ ca.key │ │ ├ client1.key │ │ └ server1.key │ ├ req │ │ ├ client1.req │ │ └ server1.req │ └ dh.pem ├ ta.key ├ server1.conf ├ ca.crt ├ server1.crt ├ server1.key ├ dh.pem └ cfg ├ server1.conf └ client1.ovpn
設定を完了したら、クライアントにファイルを配布する準備をします。
後で個別にやってもいいのですが、この後はサーバーの設定が続くので、先に済ませるのが吉です。
mkdir /etc/openvpn/client
cp /etc/openvpn/cfg/client1.ovpn /etc/openvpn/client/client1.ovpn
cp /etc/openvpn/easyrsa/pki/issued/client1.crt /etc/openvpn/client/client1.crt
cp /etc/openvpn/easyrsa/pki/private/client1.key /etc/openvpn/client/client1.key
cp /etc/openvpn/easyrsa/pki/ca.crt /etc/openvpn/client/ca.crt
cp /etc/openvpn/ta.key /etc/openvpn/client/ta.key
これで /etc/openvpn/client 内にクライアントに必要なファイルが揃いました。
後は、clientフォルダを圧縮して配布すればOKです。
.ovpnファイルに鍵をまとめることもできますが、その方法は紹介しません。
というよりわからないので紹介できませんごめんなさい。
6. ipフォワードの許可
/etc/sysctl.confをいじります。
#net.ipv4.ip_forward=1
を
net.ipv4.ip_forward=1
に変更します。コメント(#)を外すだけで完了です。
7. iptablesの設定
今回はMinecraftの土台としてVPSを設定しているので、グローバルipに来たトラフィックをVPN内に流す必要があります。
Minecraftで使用されるデフォルトポートは25565なので以下の通り設定します。
$ iptables -t nat -A PREROUTING -d 123.456.789.123/32 -p tcp -m tcp --dport 25565 -j DNAT --to-destination 192.168.10.2:25565
$ iptables -t nat -A POSTROUTING -d 192.168.10.2/32 -p tcp -m tcp --dport 25565 -j SNAT --to-source 192.168.10.1
8. OpenVPNのデーモン化
9. OpenVPN起動!
以下を実行する
$ systemctl start openvpn@server1
これで晴れてOpenVPNサーバー構築が完了する。おめでとう。