RTX1210導入をきっかけに自宅通信回線をIPoE化したりLANケーブルを自作したりしておりますが、今日はIPoE化によりL2TP/IPSecが利用できなくなったのでOpenVPNサーバーの構築手順を改めて確認しておきたいと思います。
構築環境
Debian buster(10.5)ですがラズパイ(Raspberry Pi OS)でも同じ手順で構築可能です。
sandambara@srv001:~$ cat /etc/debian_version
10.5
思い違い・誤認と思われる箇所がありましたら是非ご指摘下さいm(__)m
参考にさせて頂いたサイト
大変分かりやすくまとめられており、とても勉強になりました。素晴らしい記事を公開頂き有難うございました。
@noraworldさま
OpenVPNインストール
sudo apt -y install openvpn
Easy RSAのダウンロードと初期設定
公式リポジトリからダウンロードします。
sandambara@srv001:~$ git clone https://github.com/OpenVPN/easy-rsa.git
Cloning into 'easy-rsa'...
remote: Enumerating objects: 124, done.
remote: Counting objects: 100% (124/124), done.
remote: Compressing objects: 100% (98/98), done.
remote: Total 2077 (delta 48), reused 93 (delta 26), pack-reused 1953
Receiving objects: 100% (2077/2077), 12.94 MiB | 1.24 MiB/s, done.
Resolving deltas: 100% (900/900), done.
なお、この先の作業は必ずeasyrsa3のディレクトリで行いましょう。ディレクトリを移動したら初期設定を済ませます。
sandambara@srv001:~$ cd easy-rsa/easyrsa3
sandambara@srv001:~/easy-rsa/easyrsa3$ ./easyrsa init-pki
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /home/sandambara/easy-rsa/easyrsa3/pki
初期化成功のメッセージを確認したら次へ進みます。
CA証明書を作る
CA証明書を作成します。ここでパスフレーズとCN(コモンネーム)を入力することになりますので予め決めておきましょう。コモンネームの文字列はなんでも構いません。
sandambara@srv001:~/easy-rsa/easyrsa3$ ./easyrsa build-ca
Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Enter New CA Key Passphrase:パスフレーズ
Re-Enter New CA Key Passphrase:パスフレーズ(確認用)
Generating RSA private key, 2048 bit long modulus (2 primes)
.................................+++++
..................+++++
e is 65537 (0x010001)
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:
/home/sandambara/easy-rsa/easyrsa3/pki/ca.crt
作成された証明書をコピーします。
sandambara@srv001:~/easy-rsa/easyrsa3$ sudo cp pki/ca.crt /etc/openvpn
サーバー証明書を作る
ここでCA証明書を作成するときに入力したパスフレーズを聞かれます。
sandambara@srv001:~/easy-rsa/easyrsa3$ ./easyrsa build-server-full server nopass
Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Generating a RSA private key
.......................................................................+++++
.........................+++++
writing new private key to '/home/sandambara/easy-rsa/easyrsa3/pki/easy-rsa-4484.P8z0p0/tmp.qcEmGa'
-----
Using configuration from /home/sandambara/easy-rsa/easyrsa3/pki/easy-rsa-4484.P8z0p0/tmp.5RaS0a
Enter pass phrase for /home/sandambara/easy-rsa/easyrsa3/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 Dec 27 09:20:37 2022 GMT (825 days)
Write out database with 1 new entries
Data Base Updated
作成した証明書とキーをCA証明書と同じディレクトリへコピーします。
sandambara@srv001:~/easy-rsa/easyrsa3$ sudo cp pki/issued/server.crt /etc/openvpn
sandambara@srv001:~/easy-rsa/easyrsa3$ sudo cp pki/private/server.key /etc/openvpn
DH(Diffie-Hellman)鍵を作る
コマンド入力後、完了まで数十秒といったところでしょうか。
sandambara@srv001:~/easy-rsa/easyrsa3$ ./easyrsa gen-dh
Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
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 /home/sandambara/easy-rsa/easyrsa3/pki/dh.pem
終わったら証明書と同じディレクトリへコピーしましょう。
sandambara@srv001:~/easy-rsa/easyrsa3$ sudo cp pki/dh.pem /etc/openvpn
クライアント証明書失効リストを作る
続いてクライアント証明書失効リストを作成します。コマンド入力後、CA証明書作成時に入力したパスフレーズを聞かれます。作成後はサーバー側の証明書と同じディレクトリへコピーし、アクセス権を付与します。
sandambara@srv001:~/easy-rsa/easyrsa3$ ./easyrsa gen-crl
Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Using configuration from /home/sandambara/easy-rsa/easyrsa3/pki/easy-rsa-4612.KNHs7Y/tmp.OAIkJi
Enter pass phrase for /home/sandambara/easy-rsa/easyrsa3/pki/private/ca.key:
パスフレーズ
An updated CRL has been created.
CRL file: /home/sandambara/easy-rsa/easyrsa3/pki/crl.pem
sandambara@srv001:~/easy-rsa/easyrsa3$ sudo cp pki/crl.pem /etc/openvpn
sandambara@srv001:~/easy-rsa/easyrsa3$ sudo chmod o+r /etc/openvpn/crl.pem
server.confを作る
サーバーのコンフィグを作成します。下記はポート番号を除き実際に私の環境で利用しているものです(OpenVPN既定のポートはudp 1194ですが、IPoE環境では変更が必要と思っておいてください)。
sandambara@srv001:~/easy-rsa/easyrsa3$ sudo nano /etc/openvpn/server.conf
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
crl-verify crl.pem
ifconfig-pool-persist ipp.txt
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "route 10.8.0.0 255.255.255.0"
push "dhcp-option DNS 8.8.8.8"
link-mtu 1500
client-to-client
keepalive 10 120
comp-lzo
cipher AES-256-GCM
auth SHA256
user nobody
group nogroup
persist-key
persist-tun
status /var/log/openvpn-status.log
log /var/log/openvpn.log
log-append /var/log/openvpn.log
# LINEで通知を送るスクリプトを実行する際は下記を追加
script-security 2
verb 3
「LINEで通知を送るスクリプトを・・・」の箇所はでこちらの記事でご紹介した内容を実行する際に必要な設定です。純粋にOpenVPNを利用するだけであればここの行は無くてもかまいません。
他の設定については冒頭でご紹介した参考先の記事がとっても秀逸ですので、そちらをご覧頂ければと思います。私が色々書くより絶対に分かりやすいです(キッパリ)。
この後行うファイアウォールとルーターの設定、クライアント証明書の作成については次回以降の記事でご紹介したいと思います!
コメント