
Ubuntu Server (20.04)でVPNサーバを構築します。
記事の目次
OpenVPN用のサーバの設定を変更する!
クライアントから単独でVPN接続するのみの場合は必要ありませんが、OpenVPNに接続された他のネットワークに接続する場合などのために、OpenVPN用のサーバに対して、IPのフォーワーディングが可能になるように設定変更します。設定を有効にするために「sysctl -p」コマンドを実行するか、サーバを再起動します。
1 2 3 4 5 6 | # vi /etc/sysctl.conf ... net.ipv4.ip_forward = 1 ... # sysctl -p net.ipv4.ip_forward = 1 |
OpenVPNとease-rsaをインストールする!
OpenVPNとease-rsaをインストールします。
1 | # apt install openvpn easy-rsa |
証明書作成用のディレクトリを作成する!
証明書作成用のディレクトリを作成します。
1 2 3 4 5 6 7 8 | # make-cadir ~/openvpn-certs # cd openvpn-certs ls -l total 20 lrwxrwxrwx 1 root root 27 Jun 27 11:06 easyrsa -> /usr/share/easy-rsa/easyrsa -rw-r--r-- 1 root root 4651 Jun 27 11:06 openssl-easyrsa.cnf -rw-r--r-- 1 root root 8576 Jun 27 11:06 vars lrwxrwxrwx 1 root root 30 Jun 27 11:06 x509-types -> /usr/share/easy-rsa/x509-types |
証明書作成用のディレクトリを初期化する!
証明書作成用のディレクトリを初期化します。
1 2 3 4 5 6 | # ./easyrsa init-pki Note: using Easy-RSA configuration from: ./vars init-pki complete; you may now create a CA or requests. Your newly created PKI dir is: /root/openvpn-certs/pki |
CA局を作成する!
CA局を作成します。CNは内部的に使用されるだけなので、適当でも大丈夫です。作成完了したら、「/etc/openvpn」ディレクトリにコピーします。
1 2 3 4 5 6 7 8 9 | # ./easyrsa build-ca nopass ... Common Name (eg: your user, host, or server name) [Easy-RSA CA]:OPENVPN CA CA creation complete and you may now import and sign cert requests. Your new CA certificate file for publishing is at: /root/openvpn-certs/pki/ca.crt # cp pki/ca.crt /etc/openvpn |
OpenVPNサーバ用の証明書を作成する!
OpenVPNサーバ用の証明書を作成します。作成が完了したら、「server.crt」、「server.key」を「/etc/openvpn」ディレクトリにコピーします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # ./easyrsa build-server-full server nopass Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 Generating a RSA private key ...............................+++++ ...............................................................................................................+++++ writing new private key to '/root/openvpn-certs/pki/private/server.key.33mZwbU6B6' ----- Using configuration from /root/openvpn-certs/pki/safessl-easyrsa.cnf 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 Jun 11 02:30:25 2024 GMT (1080 days) Write out database with 1 new entries Data Base Updated # cp pki/issued/server.crt /etc/openvpn # cp pki/private/server.key /etc/openvpn |
DH鍵を作成する!
DH鍵を作成します。作成が完了したら、「dh.pem」を「/etc/openvpn」ディレクトリにコピーします。
1 2 3 4 5 6 7 8 9 10 11 12 | # ./easyrsa gen-dh Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 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/openvpn-certs/pki/dh.pem # cp pki/dh.pem /etc/openvpn |
CRLを作成する!
CRLを作成しておきます。
1 2 3 4 5 6 7 8 9 | # ./easyrsa gen-crl Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 Using configuration from /root/openvpn-certs/pki/safessl-easyrsa.cnf An updated CRL has been created. CRL file: /root/openvpn-certs/pki/crl.pem |
OpenVPNサーバの設定ファイルを作成する!
OpenVPNサーバの設定ファイルを作成します。dh鍵ファイルの名前の変更と、tls-authを無効化し、問題解析用にログのファイル出力を有効にします。
1 2 3 4 5 6 7 8 9 10 11 | # cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ # sudo gunzip /etc/openvpn/server.conf.gz # vi /etc/openvpn/server.conf ... dh dh.pem ... ;tls-auth ta.key 0 # This file is secret ...l log /var/log/openvpn/openvpn.log log-append /var/log/openvpn/openvpn.log ... |
OpenVPNサーバを起動する!
OpenVPNサーバを起動します。問題なく起動したら、サーバ起動時のも起動するように設定します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # systemctl start openvpn@server # systemctl status openvpn@server openvpn@server.service - OpenVPN connection to server Loaded: loaded (/lib/systemd/system/openvpn@.service; disabled; vendor preset: enabled) Active: active (running) since Sun 2021-06-27 15:09:17 JST; 2s ago Docs: man:openvpn(8) Main PID: 12652 (openvpn) Status: "Initialization Sequence Completed" Tasks: 1 (limit: 469) Memory: 1.0M CGroup: /system.slice/system-openvpn.slice/openvpn@server.service └─12652 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --> ... # systemctl enable openvpn@server |
クライアント接続用の証明書を作成する!
クライアント接続用の証明書を作成しておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # ./easyrsa build-client-full client1 nopass Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 Generating a RSA private key .................+++++ ........+++++ writing new private key to '/root/openvpn-certs/pki/private/client1.key.TuTcL3gKN9' ----- Using configuration from /root/openvpn-certs/pki/safessl-easyrsa.cnf 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 Jun 11 06:14:31 2024 GMT (1080 days) Write out database with 1 new entries Data Base Updated |
おわりに
OpenVPNサーバの設定は、証明書作成のステップが長く大変な道のりですが、辛抱強く設定していきます。
関連記事
OpenVPN書籍(Amazon)