LoginSignup
22
18

More than 3 years have passed since last update.

Amazon Linux2 でOpenVPNを構築する

Last updated at Posted at 2018-10-24

Amazon Linux2 でOpenVPN を構築する

※追記:パケットフォワーディングおよびファイアウォールの設定

 概要

Amazon Linux2 をパブリックサブネットに構築後からの作業。
vpnux Client で 接続する。

作業の流れ

  • インストール
    1. EPEL リポジトリを有効化する
    2. OpenVPNのインストール
    3. easy-rsaのインストール
  • 認証局と鍵の作成
    1. 認証局の初期化
    2. 認証局の作成
    3. DHパラメータの生成
    4. TLS認証鍵の作成
    5. サーバー証明書と秘密鍵の作成
    6. クライアント証明書と秘密鍵の作成
  • OpenVPN設定
    1. OpenVPN設定ファイル作成
    2. OpenVPN自動起動設定
    3. 確認
  • パケットフォワーディング設定
    1. NAT設定
    2. フォワーディング設定
    3. iptablesの設定を再起動しても維持する

インストール

EPEL リポジトリを有効化する

RHEL 7 および Amazon Linux 2 で EPEL rpmパッケージをインストールして有効にする

sudo yum update -y 
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

OpenVPNのインストール

sudo yum install openvpn -y 

easy-rsaのインストール

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

/usr/share/easy-rsaに導入される。

 認証局と鍵の作成

認証局の初期化

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

認証局を作成前にのみ実行する
メッセージからpkiディレクトリが作成されていることがわかる。

認証局の作成

./easyrsa build-ca
Generating a 2048 bit RSA private key
...........................+++
...................................+++
writing new private key to '/usr/share/easy-rsa/3/pki/private/ca.key.wTA3RRKWak'
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:
/usr/share/easy-rsa/3/pki/ca.crt

パスフレーズを入力する。例:hogehoge
Common Name はデフォルトでよければ、入力せずに[Enter]。例:Easy-RSA CA

DHパラメータの生成


./easyrsa gen-dh
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
......................................+.........................................................................................................................
 ~~ 略 ~~
.......................................................................+......................................................................++*++*

DH parameters of size 2048 created at /usr/share/easy-rsa/3/pki/dh.pem

TLS認証鍵の作成

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

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

./easyrsa build-server-full server nopass
Generating a 2048 bit RSA private key
...................................+++
................+++
writing new private key to '/usr/share/easy-rsa/3/pki/private/server.key.Y66YWmuObr'
-----
Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /usr/share/easy-rsa/3/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:'server'
Certificate is to be certified until Oct 16 06:04:10 2028 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

nopassオプションでパスフレーズが不要になる。
認証局作成時に設定したパスフレーズを入力。例:hogehoge

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

 ./easyrsa build-client-full client
Generating a 2048 bit RSA private key
...................................................................................+++
...........+++
writing new private key to '/usr/share/easy-rsa/3/pki/private/client.key.aHtmDZttPl'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /usr/share/easy-rsa/3/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:'client'
Certificate is to be certified until Oct 16 06:06:45 2028 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated

最初のパスフレーズが、クライアント証明書のパスワード、例:hogehoge
後のパスフレーズが認証局作成時のパスワードを入力する。例:hogehoge

作成されたファイルは、pkiディレクトリ内の各サブディレクトリに保管されている。
private:秘密鍵
reqs:証明書要求
issued:証明書

OpenVPNの設定

OpenVPN設定ファイルの作成

/etc/openvpn/server.conf を作成する。

デフォルトでは存在しないので、

cp /usr/share/doc/openvpn-2.4.10/sample/sample-config-files/server.conf /etc/openvpn/

テンプレートをコピーする。
内容を修正する。

vi /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                #適宜変更する

# OpenVPNからクライアントへ払い出すIPアドレスのレンジ
server 10.8.0.0 255.255.255.0   #任意

ifconfig-pool-persist ipp.txt
# クライアントがVPNを経由して、通信させたいネットワーク経路を記載(複数可)
push "route 10.0.0.0 255.255.255.0" #パブリックサブネットを指定

keepalive 10 120  #任意

#tls-auth ta.key 0 # TLS-Auth不要の場合はコメントアウト(今回はコメントアウト)
cipher AES-256-CBC
comp-lzo   #任意

persist-key
persist-tun

status openvpn-status.log
verb 3
# TCP通信の場合はコメントアウト(今回はUDPなのでコメントアウトしない)
explicit-exit-notify 1

push "route"のところはVPNのサブネットのIPアドレスになります。
IPアドレスにあわせて変更してください。

OpenVPNの起動

systemctl start openvpn@server 

OpenVPN自動起動設定

systemctl enable openvpn@server

確認

systemctl list-unit-files -t service | grep openvpn
openvpn-client@.service                       disabled
openvpn-server@.service                       disabled
openvpn@.service                              enabled

パケットフォワーディング設定

NAT設定

iptables -t nat -POSTROUTING -j MASQUERADE

設定を保存する。

iptables-save > /etc/sysconfig/iptables

フォワーディング設定

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

の追加。または、

echo 1 > /proc/sys/net/ipv4/ip_forward 

iptablesの設定を再起動しても維持する

以下を追加する。

/etc/rc.local
iptables-restore < /etc/sysconfig/iptables

再起動して、確認をする。
以上

参考

Amazon Linux でOpenVPN + easy-rsa 3を構築する(詳細解説)

AWSにOpenVPNでVPNを構築してみた

AWSにOpenVPN[AmazonLinux]を構築(ユーザー & パスワード認証編)

【丁寧解説】Linuxファイアウォール iptablesの使い方

net.ipv4.ip_forward設定を利用してforward機能を実現

iptables の規則を保存する

iptablesの設定を再起動しても維持する

CentOS7でrc.localが実行されない問題

Linuxのスタートアップ時にシェルスクリプトやコマンドを実行させる方法

22
18
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
22
18