OpenVPN : VPN サーバーの設定2026/02/16 |
|
OpenVPN をインストールして、仮想プライベートネットワーク経由で OpenVPN サーバーに接続できるよう設定します。
当例では以下のような環境で OpenVPN サーバーを設定します。
下例の場合は、前提として、ルーター側で NAT の設定が必要です。
+----------------------+
| [ OpenVPN Server ] |172.16.100.1
| dlp.srv.world +--------+
| |tun |
+-----------+----------+ |
vtnet0|10.0.0.30 |
| |
| Local Network |
10.0.0.1| |
+------+-----+ |
-------| Router |-------------|-----
+------+-----+ |
| |
| Internet |
--------------+-------------------|-----
| |
| Local Network |
192.168.0.30| |
+-----------+----------+ |
| |tun |
| VPN Client +--------+
| |172.16.100.x
+----------------------+
|
| [1] | OpenVPN をインストールします。 |
|
root@dlp:~ # pkg install -y openvpn easy-rsa
|
| [2] | 認証局や証明書等々を作成します。 |
|
root@dlp:~ # easyrsa init-pki Notice ------ 'init-pki' complete; you may now create a CA or requests. Your newly created PKI dir is: * /root/pki # 認証局 作成 root@dlp:~ # easyrsa build-ca # 任意のパスフレーズを設定 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. Your new CA certificate is at: * /root/pki/ca.crt Build-ca completed successfully. # サーバー証明書 作成 # [server1] の箇所は任意の名称で OK # (証明書のファイル名や commonName 等に設定される) root@dlp:~ # easyrsa build-server-full server1 nopass
Notice
------
Private-Key and Public-Certificate-Request files created.
Your files are:
* req: /root/pki/reqs/server1.req
* key: /root/pki/private/server1.key
You are about to sign the following certificate:
Requested CN: 'server1'
Requested type: 'server'
Valid 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 /root/pki/openssl-easyrsa.cnf
Enter pass phrase for /root/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 May 21 00:10:44 2028 GMT (825 days)
Write out database with 1 new entries
Database updated
WARNING
=======
INCOMPLETE Inline file created:
* /root/pki/inline/private/server1.inline
Notice
------
Certificate created at:
* /root/pki/issued/server1.crt
# クライアント証明書 作成 # [client1] の箇所は任意の名称で OK # (証明書のファイル名や commonName 等に設定される) root@dlp:~ # easyrsa build-client-full client1 nopass
Notice
------
Private-Key and Public-Certificate-Request files created.
Your files are:
* req: /root/pki/reqs/client1.req
* key: /root/pki/private/client1.key
You are about to sign the following certificate:
Requested CN: 'client1'
Requested type: 'client'
Valid 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 /root/pki/openssl-easyrsa.cnf
# 認証局の作成で設定したパスフレーズで応答
Enter pass phrase for /root/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 May 21 00:13:07 2028 GMT (825 days)
Write out database with 1 new entries
Database updated
WARNING
=======
INCOMPLETE Inline file created:
* /root/pki/inline/private/client1.inline
Notice
------
Certificate created at:
* /root/pki/issued/client1.crt
# Diffie Hellman ( DH ) パラメーター生成 root@dlp:~ # easyrsa gen-dh DH parameters appear to be ok. Notice ------ DH parameters of size 2048 created at: * /root/pki/dh.pem # TLS-Auth キー作成 root@dlp:~ # openvpn --genkey secret ./pki/ta.key
# 生成した証明書をコピー root@dlp:~ # mkdir /usr/local/etc/openvpn root@dlp:~ # cp -pR ./pki/issued /usr/local/etc/openvpn/ root@dlp:~ # cp -pR ./pki/private /usr/local/etc/openvpn/ root@dlp:~ # cp -pR ./pki/ca.crt /usr/local/etc/openvpn/ root@dlp:~ # cp -pR ./pki/dh.pem /usr/local/etc/openvpn/ root@dlp:~ # cp -pR ./pki/ta.key /usr/local/etc/openvpn/
|
| [3] | OpenVPN を設定して起動します。 |
|
# サンプル設定ファイル コピー root@dlp:~ # cp /usr/local/share/examples/openvpn/sample-config-files/server.conf /usr/local/etc/openvpn/openvpn.conf
root@dlp:~ #
vi /usr/local/etc/openvpn/openvpn.conf # 32行目 : 必要があれば変更 (OpenVPN がリスンするポート) port 1194 # 35行目 : 必要があれば変更 (当例は UDP 使用) ;proto tcp proto udp # 53行目 : 必要があれば変更 (当例は tun 使用) ;dev tap dev tun # 86行目 : コピーした証明書を指定 ca ca.crt cert issued/server1.crt key private/server1.key dh dh.pem
# 110行目 : VPN で使用するネットワークを指定 # ローカルネットワークと重複しなければ何でも OK server 172.16.100.0 255.255.255.0
# 150行目 : コメント解除してローカルネットワークに変更 push "route 10.0.0.0 255.255.255.0 "
# 240行目 : キープアライブ (10秒毎に生存確認, 120秒無応答でダウンと判断) keepalive 10 120 # 253行目 : コメント解除してコピーした TLS-Auth キーを指定 tls-auth ta.key 0
# 271行目 : 確認 (persist オプション有効) persist-key persist-tun # 296行目 : ログレベルを指定 (0 は fatal errors のみ, 9 は軽微なログまで全て出力) verb 3 service openvpn enable openvpn enabled in /etc/rc.conf root@dlp:~ # service openvpn start Starting openvpn. |
| [4] |
以下のファイルを VPN 接続したいクライアントコンピュータへ転送しておきます。
* /usr/local/etc/openvpn/ca.crt |
| [5] | VPN クライアントがローカルネットワークにアクセスできるよう、パケットフィルターを設定します。 |
|
root@dlp:~ #
sysctl net.inet.ip.forwarding=1 net.inet.ip.forwarding: 0 -> 1 root@dlp:~ # echo 'net.inet.ip.forwarding=1' >> /etc/sysctl.conf
root@dlp:~ #
vi /etc/pf.conf # 新規作成 ext_if = "vtnet0" wg_net = "172.16.100.0/24" set skip on lo scrub in on $ext_if all fragment reassemble nat on $ext_if from $wg_net to any -> ($ext_if)
root@dlp:~ #
service pflog enable pflog enabled in /etc/rc.conf root@dlp:~ # service pf enable pf enabled in /etc/rc.conf root@dlp:~ # service pf start root@dlp:~ # service pflog start
# OpenVPN サーバーを仮想マシン上で起動している場合はチェックサムオフロードをオフにする root@dlp:~ # ifconfig vtnet0 -rxcsum -txcsum -rxcsum6 -txcsum6 -tso -lro root@dlp:~ # echo "ifconfig vtnet0 -rxcsum -txcsum -rxcsum6 -txcsum6 -tso -lro" >> /etc/rc.conf
|
| Sponsored Link |