0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenVPN 纏め + Amazon Linux2

Last updated at Posted at 2020-12-23

概要

OpenVPNは、IP/TCPなどのL2、L3プロトコルを、SSL/TLSでカプセル化し、OpenVPNサーバーとOpenVPNクライアントとの間を安全に接続します

OpenVPNに必要なファイル

  • CA(認証局):CA証明書、CA秘密鍵
  • OpenVPNサーバー:CA証明書、サーバー証明書、サーバー秘密鍵、DHパラメーター
  • OpenVPNクライアント:CA証明書、クライアント証明書、クライアント秘密鍵

接続形態

ルーティング接続 ブリッジ接続
仮想トンネルネットワーク経由で異なるネットワークに接続する方法です。
ルーティング処理を介して接続するので、 OpenVPNサーバーとOpenVPNクライアントは別々のネットワークが利用できます。
LAN同士を相互に接続する用途に向いており、大規模なアクセス制御に向いている。
仮想インタフェースで接続する方法です。
接続ネットワークと同じネットワークセグメントのIPアドレスをOpenVPNクライアントの仮想インタフェースに割り当てることで、OpenVPNクライアントは、接続先ネットワークに参加できます。
ブロードキャストが届くので、SambaやWindowsサーバーなどが利用できます。小規模なネットワークや個人で利用するのに手軽である。

OpenVPNサーバー側

easy-rsa パッケージを使って、認証局などを作成

# /usr/share/easy-rsa/3/easy-rsa init-pki
# /usr/share/easy-rsa/3/easy-rsa build-ca

サーバー証明書を作成

# /usr/share/easy-rsa/3/easy-rsa build-ca build-server-full vpnsrv nopass

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

# /usr/share/easy-rsa/3/easy-rsa build-ca build-client-full vpncli nopass

DH パラメータを作成

# /usr/share/easy-rsa/3/easy-rsa gen-dh

作成した証明書ファイルを、etc/openvpn/server にコピー

# cp -r /usr/share/easy-rsa/3/pki etc/openvpn/server

TLS 鍵を作成

# openvpn --genkey --secret /etc/openvpn/server/pki/ta.key

/etc/openvpn/server/server.conf の設定
/usr/share/doc/openvpn/2.4.9/sample/sample-config-files からサンプルをコピーして利用するといい。

/etc/openvpn/server/server.conf

#ポート番号
port 1194

#プロトコル
proto udp

#ブリッジ接続
dev tap

#CA秘密鍵
ca ca.crt

#サーバー証明書
cert issued/vpnsrv.crt

#サーバー秘密鍵
key private/vpnsrv.key

#DHパラメータ
dh dh.pem

#VPNで利用するネットワーク
server 192.168.250.0 255.255.255.0
push "route 192.168.250.0 255.255.255.0"

#TLS認証鍵
tls-auth ta.key

#接続中のクライアントのリストを出力するファイル
status /var/log/openvpn-status.log

#ログを出力するファイル(指定しなければsyslogに出力)
log /var/log/openvpn.log

OpenVPNを起動

# systemctl start openvpn-server@server
# systemctl -w net.ipv4.ip_forward=1

OpenVPNクライアント側

OpenVPNクライアント側へ以下のファイルを転送

転送必要なファイル 説明
クライアント証明書 /etc/openvpn/server/pki/issued/vpncli1.crt
クライアント秘密鍵 /etc/openvpn/server/pki/private/vpncli1.key
CA秘密鍵 /etc/openvpn/server/pki/ca.crt
TLS認証鍵 /etc/openvpn/server/pki/ta.key
/etc/openvpn/client.conf

#クライアントであることの指定。
client

#ブリッジ接続
dev tap

#プロトコル
proto udp

#接続先サーバー、ポート
remoto server.naata.com 1194

#認証局証明書のファイル
ca     ca.crt
#クライアント証明書のファイル
cert   issued/vpncli1.crt
#クライアント秘密鍵のファイル
key    private/vpncli1.key

#tls認証鍵
tls-auth ta.key

OpenVPN を起動

# /sbin/openvpn /etc/openvpn/client.conf

OpenVPN を構築する(Amazon Linux2)

1.インストール

EPEL リポジトリを有効化

OpenVPN のインストール

sudo yum install openvpn -y 

easy-rsa のインストール

sudo yum install easy-rsa --enablerepo=epel -y

2.認証局と鍵の作成

認証局の初期化

sudo -s
cd /usr/share/easy-rsa/3
./easyrsa init-pki

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

認証局の作成

./easyrsa build-ca

DHパラメータの作成

./easyrsa gen-dh

TLS認証鍵の作成

openvpn --genkey --secret /etc/openvpn/ta.key

サーバー証明書と秘密鍵の作成

./easyrsa build-server-full server nopass

クライアント証明書と秘密鍵の作成

 ./easyrsa build-client-full client

3./etc/openvpn/server.confの設定

/etc/openvpn/server.confの編集

cp /usr/share/doc/openvpn-2.4.9/sample/sample-config-files/server.conf /etc/openvpn/
/etc/openvpn/server.conf
port 1194
proto udp
dev tun
ca /usr/share/easy-rsa/3/pki/ca.crt 
cert /usr/share/easy-rsa/3/pki/issued/server.crt
key /usr/share/easy-rsa/3/pki/private/server.key
dh /usr/share/easy-rsa/3/pki/dh.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 10.0.0.0 255.255.255.0"
keepalive 10 120
#tls-auth ta.key 0
cipher AES-256-CBC
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
explicit-exit-notify 1

4.openVPNの開始と確認

フォワーディングの設定

/etc/sysctl.conf
net.ipv4.ip_forward = 1

network の再起動

systemctl restart network 

openvpnの再起動

systemctl start openvpn@server
systemctl enable openvpn@server

openvpnのステータス確認

systemctl list-unit-files -t service | grep openvpn

5.AWS側の設定

  • OpenVPNをインストールしたインスタンスのセキュリティグループで 1194 番ポートを許可する

  • Privateネットワーク

  • ルートテーブル:VPNクライアント側のセグメントから、OpenVPNをインストールしたサーバへのルーティングを設定する。

  • セキュリティグループ:VPNクライアント側のセグメントからはいってくるパケットの許可

  • OpenVPNをインストールしたサーバで送信元/送信先の変更チェックを無効に設定する

※ 送信元/送信先の変更チェック無効化とは
EC2インスタンスはデフォルトで送信元/送信先チェックを有効化しています。つまり、対象のEC2インスタンス宛てでないパケットはデフォルトで弾く仕様ということです。今回はインターネット宛てのパケットをEC2で経由させる必要があるためこの機能を無効化し、パケットを弾かせないように変更する必要があります。

参考:https://it.hirokun.net/entry/ec2-openvpn-easyrsa3#OpenVPN-6

6.Tunnelblick(MAC)で検証

Tunnelbrick で、接続する。

client
dev tun
proto udp
remote OpenVPNサーバIPアドレス 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
cipher AES-256-CBC
keepalive 10 60
verb 3
mssfix 1280
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?