MENU

拠点間 VPN を構築してみた

目次

はじめに

こんにちは.ネットワークエンジニアの「はむ」です.

今回は,EC2 とオンプレサーバ間で VPN を接続する,拠点間 VPN (site-to-site VPN) の設定方法を紹介したいと思います.

拠点間 VPN とは?

VPN にはリモートアクセス VPN と拠点間 VPN の 2 種類があります.リモートアクセス VPN とは,スマートフォンやタブレットなどの移動端末から VPN サーバに接続して,端末からインターネットにアクセスする際に

拠点間 VPN の設定

OpenVPN サーバ (EC2) 側にて実施

まず,いちいち sudo コマンドを打つのが面倒なので,root ユーザで実施.

$ sudo su -

apt を最新状態にして,openvpn と easy-rsa をインストール.

# apt update && apt upgrade -y && apt install -y openvpn easy-rsa

次に,CA 証明書と秘密鍵を生成.

# mv /usr/share/easy-rsa /etc/openvpn/
# cd /etc/openvpn/easy-rsa/
# ./easyrsa init-pki

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


# ./easyrsa build-ca
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)

Enter New CA Key Passphrase:        # CA 用パスフレーズを入力
Re-Enter New CA Key Passphrase:     # 再度 CA 用パスフレーズを入力
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]: ec2-vpn-gw # EC2 側の VPN サーバのホスト名を入力

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

サーバ証明書を作成.

# ./easyrsa build-server-full ec2-vpn-gw nopass
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
...+++++++
...
...
+++++++
-----
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-28034.Cjr5Hn/tmp.htyIsu
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: # CA 用パスフレーズを入力
801B62F5797F0000:error:0700006C:configuration file routines:NCONF_get_string:no value:../crypto/conf/conf_lib.c:315:group=<NULL> name=unique_subject
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'ec2-vpn-gw'
Certificate is to be certified until Sep 17 13:10:53 2026 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

DH パラメータを作成.

# ./easyrsa gen-dh
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
Generating DH parameters, 2048 bit long safe prime
......................
+........
.........
..++*++*++*++*++*++*++

DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem

証明書廃止のための証明書失効リストを作成.

# ./easyrsa build-client-full dummy nopass
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
..........+..+.........+...
.+....+..+..
.......+....
+++++++++++++++++++++++++++
-----
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-28151.9GhUge/tmp.LLOmkK
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: # CA 用パスフレーズを入力
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'dummy'
Certificate is to be certified until Sep 17 13:14:27 2026 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

ダミーのクライアント証明書を廃止.

# ./easyrsa revoke dummy
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)


Please confirm you wish to revoke the certificate with the following subject:

subject=
    commonName                = dummy


Type the word 'yes' to continue, or any other input to abort.
  Continue with revocation: yes # このまま進めるなら yes と入力
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-28207.qAB58S/tmp.5I2X23
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: # CA 用パスフレーズを入力
Revoking Certificate 94A161280AB6A87BE105D72163C073CA.
Data Base Updated

IMPORTANT!!!

Revocation was successful. You must run gen-crl and upload a CRL to your
infrastructure in order to prevent the revoked cert from being accepted.

証明書失効リストを作成.

# ./easyrsa gen-crl
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-28245.FU8ffQ/tmp.hxrCpU
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: # CA 用パスフレーズを入力

An updated CRL has been created.
CRL file: /etc/openvpn/easy-rsa/pki/crl.pem

TLS 認証鍵を作成.

# openvpn --genkey --secret /etc/openvpn/server/ta.key
2024-06-14 13:16:18 WARNING: Using --genkey --secret filename is DEPRECATED.  Use --genkey secret filename instead.

出力された各種鍵や証明書をコピーして移動.

# cp pki/ca.crt pki/issued/ec2-vpn-gw.crt pki/private/ec2-vpn-gw.key pki/dh.pem pki/crl.pem /etc/openvpn/server/.

OpenVPN サーバの設定.雛形をコピーして編集

# cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf /etc/openvpn/server/.
# vi /etc/openvpn/server/server.conf

主な設定内容

port 1194 # デフォルトは 1194 だが好きなポートへの変更も可能
proto udp # 特別な事情がなければ通常は udp を選択
dev tun # トンネルモードが tun,ブリッジモードが tap.基本的にはルーティングできるトンネルモードを選択.
ca ca.crt
cert ec2-vpn-gw.crt
key ec2-vpn-gw.key  # This file should be kept secret
dh dh.pem
server 10.0.0.0 255.255.255.0 # VPN のセグメントとして割り当てたいサブネットを指定
push "route 172.26.5.156 255.255.240.0" # VPN サーバのプライベートサブネットを指定
tls-auth ta.key 0 # This file is secret
cipher AES-256-CBC
data-ciphers AES-256-GCM:AES-128-GCM:AES-256-CBC
user nobody
group nobody
status /var/log/openvpn/openvpn-status.log
log         /var/log/openvpn/openvpn.log
log-append  /var/log/openvpn/openvpn.log
verb 3
management localhost 7505
crl-verify crl.pem

ファイルを保存したら,nobody グループが存在するか getnet で確認.存在しなければ追加.

# getent group nobody
# groupadd nobody

全ての設定が済んだら,OpenVPN サーバを起動.

# systemctl start openvpn-server@server.service
# systemctl enable openvpn-server@server.service
Created symlink /etc/systemd/system/multi-user.target.wants/openvpn-server@server.service → /lib/systemd/system/openvpn-server@.service.

enabled かつ acrive (running) になっていれば OK.

# systemctl status openvpn-server@server.service
● openvpn-server@server.service - OpenVPN service for server
     Loaded: loaded (/lib/systemd/system/openvpn-server@.service; <strong>enabled</strong>; vendor preset: enabled)
     Active: <strong>active (running)</strong> since Fri 2024-06-14 13:45:42 UTC; 1 week 3 days ago
       Docs: man:openvpn(8)
             https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
             https://community.openvpn.net/openvpn/wiki/HOWTO
   Main PID: 30687 (openvpn)
     Status: "Initialization Sequence Completed"
      Tasks: 1 (limit: 516)
     Memory: 4.3M
        CPU: 22.607s
     CGroup: /system.slice/system-openvpn\x2dserver.slice/openvpn-server@server.service
             └─30687 /usr/sbin/openvpn --status /run/openvpn-server/status-server.log --status-version 2 --suppress-timestamps --config server.conf

Jun 14 13:45:42 ec2-vpn-gw systemd[1]: Starting OpenVPN service for server...
Jun 14 13:45:42 ec2-vpn-gw systemd[1]: Started OpenVPN service for server.

tun0 が立ち上がっていれば OpenVPN サーバの設定は完了.

# ip a
...
129: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 500
    link/none 
    inet 10.0.0.1 peer 10.0.0.2/32 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::c592:bfaf:bee8:f318/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

クライアント証明書を作成.

# ./easyrsa build-client-full onpre-vpn-gw nopass
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
.+...............+..
...+..+......+...+.+...+..+......
..+..+.......+........+....+...+.
.+.....+.........+.+......+...+..
+.......+.....+.....
-----
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-30902.u8Eocz/tmp.ecbZSA
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: # CA 用パスフレーズを入力
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'onpre-vpn-gw'
Certificate is to be certified until Sep 17 13:53:47 2026 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

クライアントに送信するために,ディレクトリにまとめて権限修正.

# mkdir share
# cp pki/ca.crt pki/private/onpre-vpn-gw.key pki/issued/onpre-vpn-gw.crt /etc/openvpn/server/ta.key share
# ls -l share/
total 20
-rw------- 1 root root 1208 Jun 14 13:59 ca.crt
-rw------- 1 root root  636 Jun 14 13:59 ta.key
-rw------- 1 root root 4516 Jun 14 13:59 onpre-vpn-gw.crt
-rw------- 1 root root 1704 Jun 14 13:59 onpre-vpn-gw.key
# chmod 777 share/*
# exit
$ sudo mv /etc/openvpn/easy-rsa/share ~/

ローカル PC にて実施

ローカル PC にて,下記コマンドで OpenVPN サーバからオンプレサーバへ証明書類を移行.移行後は流出防止のために削除.

$ rsync -r ec2-vpn-gw:~/share .
$ rsync -r share onpre-vpn-gw:~/
$ rm -r share

OpenVPN クライアント (オンプレサーバ) 側にて実施

移行した証明書類を移行後,OpenVPN のディレクトリに展開.

$ sudo mv share/* /etc/openvpn/client/

OpenVPN サーバの設定.雛形をコピーして編集.

$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/client/.
$ sudo chmod 600 ca.crt ta.key onpre-vpn-gw.crt onpre-vpn-gw.key
$ sudo vi /etc/openvpn/client/client.conf 

主な設定内容.

client
dev tun
proto udp
remote 57.180.178.31 1194
user nobody
group nobody
ca ca.crt
cert onpre-vpn-gw.crt
key onpre-vpn-gw.key
tls-auth ta.key 1
cipher AES-256-CBC
data-ciphers AES-256-GCM:AES-128-GCM:AES-256-CBC
verb 3
log         /var/log/openvpn/openvpn.log
log-append  /var/log/openvpn/openvpn.log

設定終了後は,OpenVPN クライアントを起動.

$ sudo systemctl start openvpn-client@client.service
$ sudo systemctl enable openvpn-client@client.service

enabled かつ acrive (running) になっていれば OK.

$ sudo systemctl status openvpn-client@client.service
● openvpn-client@client.service - OpenVPN tunnel for client
     Loaded: loaded (/lib/systemd/system/openvpn-client@.service; <strong>enabled</strong>; vendor preset: enabled)
     Active: <strong>active (running)</strong> since Fri 2024-06-14 14:33:33 UTC; 1 week 3 days ago
       Docs: man:openvpn(8)
             https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
             https://community.openvpn.net/openvpn/wiki/HOWTO
   Main PID: 212058 (openvpn)
     Status: "Initialization Sequence Completed"
      Tasks: 1 (limit: 2219)
     Memory: 3.2M
        CPU: 12.764s
     CGroup: /system.slice/system-openvpn\x2dclient.slice/openvpn-client@client.service
             └─212058 /usr/sbin/openvpn --suppress-timestamps --nobind --config client.conf

Notice: journal has been rotated since unit was started, output may be incomplete.

OpenVPN クライアントの起動後は,オンプレミスサーバが存在するネットワークにある,プライベート IP アドレスとグローバル IP アドレスを変換しているルータに,OpenVPN で使用するポート (今回は 1194) の開放設定を入れる. その際,グローバル IP の 1194 ポート宛ての通信をオンプレミスサーバの 1194 ポートに向くように設定する.

tun0 が立ち上がっていれば OpenVPN クライアントの設定は完了.

$ ip a
...
6: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 500
    link/none 
    inet 10.0.0.6 peer 10.0.0.5/32 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::c2db:3963:dd3b:84ef/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever
...

OpenVPN サーバ側で正常性確認

OpenVPN サーバから ping を打って疎通していることを確認.

$ ping -c 1 -I tun0 10.0.0.6
PING 10.0.0.6 (10.0.0.6) from 10.0.0.1 tun0: 56(84) bytes of data.
64 bytes from 10.0.0.6: icmp_seq=1 ttl=64 time=5.01 ms

--- 10.0.0.6 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 5.013/5.013/5.013/0.000 ms

オンプレ側で別サブネットへルーティングする場合

設定は全て EC2 側の OpenVPN サーバにて実施.

まずは,IPv4 forward を有効にする. IPv4 forward を有効にするためには,/etc/sysctl.conf ファイルの net.ipv4.ip_forward=1 のコメントアウトを外して,sudo sysctl -p を実行する.

次に,サーバ側ファイルでは client-config-dir ccd のコメントアウトを外し,route 10.0.1.0 255.255.255.0 を追記. 10.0.1.0 255.255.255.0 はルーティングしたいオンプレ側のサブネットに各自変更する.

最後に,ccd ディレクトリを server ディレクトリ配下に作成し,ccd ディレクトリ配下のファイルに設定を追加. ここで,ccd ディレクトリに作成するファイル名はクライアントと同じ名前 (onpre-vpn-gw) にすること.

iroute 10.0.1.0 255.255.255.0

最後に

今回は OpenVPN を使用した拠点間 VPN (site-to-site VPN) の接続方法を紹介しました.

次回の記事は,Wordpress のインストール方法について解説しようと思います.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

コメント

コメントする

目次