iOS12から接続可能なOpenVPNサーバの構築手順

お久しぶりです。

結局諦めきれずにVPNサーバの構築を続けていたらまただいぶ長いこと更新が止まってしまったのですが、やっと納得いくものができました。

今回紹介するのはL3レベルのVPNなので以前紹介したものとはちょっと異なると思うのですが、自宅経由で通信することでフリーWifiを使用しても通信内容を傍受されたりしないぐらいのことはできますね。

また、うちの場合、通信経路にFirewallがあるのでそこで通信制限かけたりしてて意図しない通信しないようにできたりはするので、セキュリティレベルは結構上がるんじゃないかと。

また、VPNが落ち着いたんで次はプロキシサーバを作ろうと思っててそれができればセキュアかつ通信容量も削減出来て快適なスマホ生活が送れそうです 笑

まぁ、長い前置きはこのくらいにして設定方法は以下の通りです。

●構築環境

1.サーバ側
・CentOS 7.6.1810
・OpenVPN 2.4.7
・easy-rsa 3.0.3
2.クライアント側
・iOS12.4
・OpenVPN 3.0.2

●構築手順

1.epelリポジトリインストール

コマンド:yum install epel-release

2.openvpnと関連パッケージインストール

コマンド:yum install openvpn easy-rsa

3.認証局の作成

3-1.設置ディレクトリの作成

コマンド:mkdir /etc/openvpn/easy-rsa

3-2.ファイルをコピー

コマンド:cp /usr/share/easy-rsa/3.0.3/* /etc/openvpn/easy-rsa/ -R

3-3.ディレクトリを移動

コマンド:cd /etc/openvpn/easy-rsa

3-4.easy-rsa環境変数設定ファイル作成

3-4-1.設定例をコピーしてファイルを作成する

コマンド:cp /usr/share/doc/easy-rsa-3.0.3/vars.example ./vars

3-4-2.以下の部分を編集する

set_var EASYRSA_KEY_SIZE 2048 #コメント解除
set_var EASYRSA_DIGEST “sha256″ #コメント解除

3-5.初期化

コマンド:./easyrsa init-pki

結果

Note: using Easy-RSA configuration from: ./vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki

3-6.認証局の作成

コマンド:./easyrsa build-ca

結果

Note: using Easy-RSA configuration from: ./vars
Generating a 2048 bit RSA private key
…………………………………………………………….++
…………………………………………………………………
writing new private key to ‘/etc/openvpn/easy-rsa/pki/private/ca.key.l7PWiWQ1KU’
Enter PEM pass phrase:<任意のパスフレーズを入力>
Verifying – Enter PEM pass phrase:<任意のパスフレーズを入力>

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/easy-rsa/pki/ca.crt

3-7.DHパラメータの作成

コマンド: ./easyrsa gen-dh

結果

Generating DH parameters, 4096 bit long safe prime, generator 2
This is going to take a long time
………………………………………………………..+…………………………++++
DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem

3-8.サーバ用の証明書と秘密鍵の作成

コマンド:./easyrsa build-server-full server_r nopass

結果

Generating a 2048 bit RSA private key
……………………………..+++
……………………………………………..+++
writing new private key to ‘/etc/openvpn/easy-rsa/pki/private/server_r.key.aNjZ0KK0TS’

Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:<手順3-5で入力したパスフレーズを入力>
Check that the request matches the signature
Signature ok
The Subject’s Distinguished Name is as follows
commonName :ASN.1 12:’server_r’
Certificate is to be certified until Jul 7 13:48:31 2029 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

3-9.クライアント用の証明書と秘密鍵の作成

コマンド:./easyrsa build-client-full client1 nopass

結果

Generating a 2048 bit RSA private key
……..+++
………..+++
writing new private key to ‘/etc/openvpn/easy-rsa/pki/private/client1.key.2vXB8D3SAc’

Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:<手順3-5で入力したパスフレーズを入力>
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 Jul 7 13:53:39 2029 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

3-10.TLS認証キーの作成

コマンド:openvpn –genkey –secret /etc/openvpn/ta.key

4.設定ファイルの作成

ファイル:/etc/openvpn/server.conf

以下の内容で作成

port 1194
proto tcp ← 接続後にスピードテストしたところtcpの方が早かった
dev tun
ca /etc/openvpn/easy-rsa/pki/ca.crt
cert /etc/openvpn/easy-rsa/pki/issued/server_r.crt
key /etc/openvpn/easy-rsa/pki/private/server_r.key
dh /etc/openvpn/easy-rsa/pki/dh.pem
server 10.8.0.0 255.255.255.0
push “route 10.2.22.0 255.255.255.0” ← VPNサーバのサブネットへの通信ルートを設定
push “redirect-gateway def1”
push “dhcp-option DNS 8.8.8.8”
client-to-client
keepalive 10 120
tls-auth /etc/openvpn/ta.key 0
cipher AES-256-CBC
replay-window 128
user nobody
group nobody
persist-key
persist-tun
status /var/log/openvpn/openvpn-status.log
log /var/log/openvpn/openvpn.log
verb 3
auth SHA256 ← デフォルトだとSHA1でセキュリティ的に甘いので設定

5.ログ関係の設定

5-1.保存用ディレクトリ作成

コマンド:mkdir /var/log/openvpn

5-2.ログローテート設定

ファイル作成:/etc/logrotate.d/openvpn

/var/log/openvpn/*.log {
missingok
notifempty
daily
compress
rotate 365
postrotate
/bin/kill -HUP ‘cat /var/run/syslogd.pid 2> /dev/null’ 2> /dev/null || true
endscript
}

6.サービス起動と自動起動設定

6-1.サービス起動

コマンド:systemctl start openvpn@server

6-2.サービス自動起動設定

コマンド:systemctl enable openvpn@server

7.Firewallの設定

7-1.設定

コマンド:firewall-cmd –add-port=1194/udp –permanent
コマンド:firewall-cmd –add-masquerade –permanent

7-2.適用

コマンド:firewall-cmd –reload

8.ルーティングの有効化

「/etc/sysctl.conf」に「net.ipv4.ip_forward = 1」を追記し適用のために再起動

以上です。

これでサーバの構築はできたので、次回、iphone側の設定の紹介をします。

ひとまずここまでで
では