見出し画像

LinuC LV2資格学習101日目

本:LinuC レベル2

公式資料

OpenVPNサーバーの設定

OpenVPNのサーバー構築を行います。構築環境はCnetOS7.9になります。

OpenVPNサーバーを構築するには、CetnOS7ではEPELリポジトリからeasy-rasをインストールして行います。

まずは、公開鍵基盤を作成します。

/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: /root/pki

認証局(CA)を作成します。

/usr/share/easy-rsa/3/easyrsa build-ca

# 実行結果
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
Enter New CA Key Passphrase: #CAのパスワード
Re-Enter New CA Key Passphrase: #確認用
Generating RSA private key, 2048 bit long modulus
....................+++
...........+++
e is 65537 (0x10001)
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]:MyCA #CA局名
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/root/pki/ca.crt

認証局を作成したら次にサーバー証明書を作成します。サーバー名は「myvpnsrv」とします。

/usr/share/easy-rsa/3/easyrsa build-server-full myvpnserv nopass

# 実行結果
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
Generating a 2048 bit RSA private key
...........................................+++
...................+++
writing new private key to '/root/pki/easy-rsa-3685.zYxd5r/tmp.6xXZ4d'
-----
Using configuration from /root/pki/easy-rsa-3685.zYxd5r/tmp.qvAZHJ
Enter pass phrase for /root/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:'myvpnserv'
Certificate is to be certified until Sep 19 10:54:05 2023 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

次にクライアント証明書を作成します。クライアント名を「linucvpncli」とします。

/usr/share/easy-rsa/3/easyrsa build-client-full linucvpncli nopass

# 実行結果
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
Generating a 2048 bit RSA private key
.....................+++
.....................................................................+++
writing new private key to '/root/pki/easy-rsa-3773.SuIy2r/tmp.Y6ICCV'
-----
Using configuration from /root/pki/easy-rsa-3773.SuIy2r/tmp.T5rynY
Enter pass phrase for /root/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:'linucvpncli'
Certificate is to be certified until Sep 19 10:57:15 2023 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

鍵交換を安全に行うためのDH(Diffie Hellman)パラメーターを作成します。

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

#実行結果
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
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 /root/pki/dh.pem

作成した証明書ファイル類を/etc/openvpn/serverへ配置します。

cp -r ~/pki /etc/openvpn/server/

TLS証明書を作成します。

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

サーバーの設定を行います。サンプル設定ファイルが「/usr/share/doc/openvpn-<バージョンNo>/sample/sample-config-files/server.conf」にあるのでそれを「/etc/openvpn/server/」にコピーして編集を行います。

cp -iv /usr/share/doc/openvpn-2.4.11/sample/sample-config-files/server.conf /etc/openvpn/server/
#編集
vi /etc/openvpn/server/server.conf

主要な有効設定はいかになります。

port 1194
proto udp
dev tun
ca /etc/openvpn/server/pki/ca.crt
cert /etc/openvpn/server/pki/issued/myvpnserv.crt
key /etc/openvpn/server/pki/private/myvpnserv.key # This file should be kept secret
dh /etc/openvpn/server/pki/dh.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
keepalive 10 120
tls-auth /etc/openvpn/server/pki/ta.key 0 # This file is secret
cipher AES-256-CBC
persist-key
persist-tun
status openvpn-status.log
log         openvpn.log
verb 3

OpenVPNを起動

systemctl start openvpn-server@server

VPN用のネットワークインターフェースが生成されます。

ip a

6: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100
   link/none 
   inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
      valid_lft forever preferred_lft forever
   inet6 fe80::9f1a:777b:ec5d:5b32/64 scope link flags 800 
      valid_lft forever preferred_lft forever
      
   

OpenVPNでVPNを利用するには、設定したポート番号(今回は1194/udp)を開放してIPフォワードを有効にする必要があります。

以上の今日の学習内容です。

久しぶりにOpenVPNの設定をしてみましたが初めて実装しようとした時は、大変苦労しましたが今回は、すんなりサーバーを構築する事が出来ました。

あすは、クライアントの設定になります。

それではまた明日、ありがとうございました。


この記事が気に入ったらサポートをしてみませんか?