Debian 12 bookworm
Sponsored Link

OpenVPN : VPN サーバーの設定2023/07/18

 
OpenVPN をインストールして、仮想プライベートネットワーク経由で OpenVPN サーバーに接続できるよう設定します。
当例では以下のような環境で OpenVPN サーバーを設定します。
設定により、サーバー/クライアント上の [tun] インターフェースは自動設定され、クライアントは VPN 接続後、VPN サーバーのローカルネットワーク宛てへアクセス可能となります。
下例の場合は、前提として、ルーター側で NAT の設定が必要です。
(WAN 側から x.x.x.x:1194 (udp) 宛にきたパケットを LAN 側の 10.0.0.29:1194 (udp) へフォワード)
  +----------------------+
  | [  OpenVPN Server  ] |192.168.100.1
  |    dlp.srv.world     +--------+
  |                      |tun     |
  +-----------+----------+        |
        enp1s0|10.0.0.30          |
              |                   |
              |  Local Network    |
      10.0.0.1|                   |
       +------+-----+             |
-------|   Router   |-------------|-----
       +------+-----+             |
              |                   |
              |  Internet         |
--------------+-------------------|-----
              |                   |
              |  Local Network    |
 192.168.0.100|                   |
  +-----------+----------+        |
  |                      |tun     |
  |      VPN Client      +--------+
  |                      |192.168.100.x
  +----------------------+

[1] OpenVPN をインストールします。
root@dlp:~#
apt -y install openvpn easy-rsa iptables
[2] 認証局や証明書等々を作成します。
root@dlp:~#
cd /usr/share/easy-rsa
# 初期化

root@dlp:/usr/share/easy-rsa#
./easyrsa init-pki


* Notice:

  init-pki complete; you may now create a CA or requests.

  Your newly created PKI dir is:
  * /usr/share/easy-rsa/pki

* Notice:
  IMPORTANT: Easy-RSA 'vars' file has now been moved to your PKI above.

# 認証局 作成

root@dlp:/usr/share/easy-rsa#
./easyrsa build-ca

* Notice:
Using SSL: openssl OpenSSL 3.0.9 30 May 2023 (Library: OpenSSL 3.0.9 30 May 2023)

# 任意のパスフレーズを設定
Enter New CA Key Passphrase:
Re-Enter New CA Key Passphrase:
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]:Server-CA

* Notice:

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/pki/ca.crt

# サーバー証明書 作成
# [server1] の箇所は任意の名称で OK
# (証明書のファイル名や commonName 等に設定される)

root@dlp:/usr/share/easy-rsa#
./easyrsa build-server-full server1 nopass

* Notice:
Using SSL: openssl OpenSSL 3.0.9 30 May 2023 (Library: OpenSSL 3.0.9 30 May 2023)

-----
* Notice:

Keypair and certificate request completed. Your files are:
req: /usr/share/easy-rsa/pki/reqs/server1.req
key: /usr/share/easy-rsa/pki/private/server1.key


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate for 825 days:

subject=
    commonName                = server1

Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes

Using configuration from /usr/share/easy-rsa/pki/8931a156/temp.a29069b3
# 認証局の作成で設定したパスフレーズで応答
Enter pass phrase for /usr/share/easy-rsa/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:'server1'
Certificate is to be certified until Oct 20 04:34:15 2025 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

* Notice:
Certificate created at: /usr/share/easy-rsa/pki/issued/server1.crt

# クライアント証明書 作成
# [client1] の箇所は任意の名称で OK
# (証明書のファイル名や commonName 等に設定される)

root@dlp:/usr/share/easy-rsa#
./easyrsa build-client-full client1 nopass

* Notice:
Using Easy-RSA configuration from: /usr/share/easy-rsa/pki/vars

* Notice:
Using SSL: openssl OpenSSL 3.0.9 30 May 2023 (Library: OpenSSL 3.0.9 30 May 2023)

* Notice:

Keypair and certificate request completed. Your files are:
req: /usr/share/easy-rsa/pki/reqs/client1.req
key: /usr/share/easy-rsa/pki/private/client1.key

You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a client certificate for 825 days:

subject=
    commonName                = client1

Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes

Using configuration from /usr/share/easy-rsa/pki/f58905b9/temp.79e305f7
# 認証局の作成で設定したパスフレーズで応答
Enter pass phrase for /usr/share/easy-rsa/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:'client1'
Certificate is to be certified until Oct 20 04:37:39 2025 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

* Notice:
Certificate created at: /usr/share/easy-rsa/pki/issued/client1.crt

# Diffie Hellman ( DH ) パラメーター生成

root@dlp:/usr/share/easy-rsa#
./easyrsa gen-dh

* Notice:
Using Easy-RSA configuration from: /usr/share/easy-rsa/pki/vars

* Notice:
Using SSL: openssl OpenSSL 3.0.9 30 May 2023 (Library: OpenSSL 3.0.9 30 May 2023)

Generating DH parameters, 2048 bit long safe prime

* Notice:

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

# TLS-Auth キー作成

root@dlp:/usr/share/easy-rsa#
openvpn --genkey secret ./pki/ta.key
# 生成した証明書をコピー

root@dlp:/usr/share/easy-rsa#
cp -pR /usr/share/easy-rsa/pki/{issued,private,ca.crt,dh.pem,ta.key} /etc/openvpn/server/

[3] OpenVPN を設定して起動します。
Firewalld によるルーティングルールも設定するため、Firewalld 稼働中が前提です。
# サンプル設定ファイル コピー

root@dlp:~#
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf /etc/openvpn/server/
root@dlp:~#
vi /etc/openvpn/server/server.conf
# 32行目 : 必要があれば変更 (OpenVPN がリスンするポート)

port 1194
# 35行目 : 必要があれば変更 (当例は UDP 使用)

;proto tcp
proto udp
# 53行目 : 必要があれば変更 (当例は tun 使用)

;dev tap
dev tun
# 78行目 : コピーした証明書を指定

ca
ca.crt

cert
issued/server1.crt

key
private/server1.key
# 85行目 : コピーした DH ファイルを指定

dh
dh.pem
# 101行目 : VPN で使用するネットワークを指定
# ローカルネットワークと重複しなければ何でも OK

server
192.168.100.0 255.255.255.0
# 142行目 : コメント解除してローカルネットワークに変更

push "route
10.0.0.0 255.255.255.0
"
# 231行目 : キープアライブ (10秒毎に生存確認, 120秒無応答でダウンと判断)

keepalive 10 120
# 244行目 : コピーしたTLS-Auth キーを指定

tls-auth
ta.key 0
# 281行目 : 確認 (persist オプション有効)

persist-key
persist-tun
# 306行目 : ログレベルを指定 (0 は fatal errors のみ, 9 は軽微なログまで全て出力)

verb 3
root@dlp:~#
vi /etc/openvpn/server/add-bridge.sh
# 新規作成

#!/bin/bash

# ローカルネットワークに接続可能なネットワークインターフェース
IF=enp1s0
# VPN で使用するインターフェース
# 当例のように設定ファイルで [tun] を指定した場合、通常は [tun0]
VPNIF=tun0

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A FORWARD -i ${VPNIF} -j ACCEPT
iptables -t nat -A POSTROUTING -o ${IF} -j MASQUERADE

root@dlp:~#
vi /etc/openvpn/server/remove-bridge.sh
# 新規作成

#!/bin/bash

# ローカルネットワークに接続可能なネットワークインターフェース
IF=enp1s0
# VPN で使用するインターフェース
# 当例のように設定ファイルで [tun] を指定した場合、通常は [tun0]
VPNIF=tun0

echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -D FORWARD -i  ${VPNIF} -j ACCEPT
iptables -t nat -D POSTROUTING -o ${IF} -j MASQUERADE

root@dlp:~#
chmod 700 /etc/openvpn/server/{add-bridge.sh,remove-bridge.sh}

root@dlp:~#
systemctl edit openvpn-server@.service
# 以下の内容で追記

[Service]
ExecStartPost=/etc/openvpn/server/add-bridge.sh
ExecStopPost=/etc/openvpn/server/remove-bridge.sh

root@dlp:~#
systemctl enable --now openvpn-server@server

[4]
以下のファイルを VPN 接続したいクライアントコンピュータへ転送しておきます。
以上で OpenVPN サーバーの設定は完了です。
・/etc/openvpn/server/ca.crt
・/etc/openvpn/server/ta.key
・/etc/openvpn/server/issued/client1.crt
・/etc/openvpn/server/private/client1.key
関連コンテンツ