OpenVPNは、仮想プライベートネットワーク技術を使用してリモートアクセス機能との安全なポイントツーポイント接続を作成する企業向けのオープンソースVPNソリューションです。 OpenVPNは、インターネット経由で送信されるデータが暗号化されてプライベートであることを確認するために多くのVPNで使用されている信頼できるテクノロジーです。
簡単に言うと、OpenVPNを使用すると、1つの安全なネットワーク内の他のデバイスに接続できます。クロスプラットフォームであり、Windows、Mac、Android、iOS、およびLinuxにすることができます。 OpenVPNは、次のような豊富な機能セットを提供します。
- 無制限の帯域幅
- 無制限のサーバースイッチ
- マルチデバイスの使用
- ログストレージなし
- プロトコルの選択
- 複数の同時接続
このチュートリアルでは、Ubuntu20.04VPSにOpenVPNサーバーとクライアントをインストールして構成する方法を学習します。
前提条件
- Atlantic.netクラウドプラットフォーム上の新しいUbuntu20.04VPS。
- ルートパスワードはサーバーで構成されています。
ステップ1:Atlantic.Netクラウドサーバーを作成する
まず、Atlantic.Netクラウドサーバーにログインします。オペレーティングシステムとしてUbuntu20.04を選択し、少なくとも1GBのRAMを搭載した新しいサーバーを作成します。 SSH経由でクラウドサーバーに接続し、ページの上部で強調表示されているクレデンシャルを使用してログインします。
Ubuntu 20.04サーバーにログインしたら、次のコマンドを実行して、ベースシステムを最新の利用可能なパッケージで更新します。
apt-get update -y
ステップ2:IP転送を有効にする
次に、OpenVPNがVPNを介してトラフィックを正しくルーティングできるように、システムでIP転送を有効にする必要があります。
ファイル/etc/sysctl.confを編集してIP転送を有効にできます:
nano /etc/sysctl.conf
次の行のコメントを解除します:
net.ipv4.ip_forward = 1
終了したらファイルを保存し、次のコマンドを実行して変更を適用します。
sysctl -p
ステップ3:OpenVPNサーバーをインストールする
apt-get install openvpn -y
インストールが完了したら、次の手順に進むことができます。
ステップ4:認証局を構築する
認証局とPKIインフラストラクチャを設定するには、システムにEasyRSAをダウンロードする必要があります。次のコマンドでダウンロードできます:
wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.6/EasyRSA-unix-v3.0.6.tgz
ダウンロードしたら、次のコマンドを使用してダウンロードしたファイルを抽出します。
tar -xvzf EasyRSA-unix-v3.0.6.tgz
次に、次のコマンドを使用して、抽出したディレクトリを/ etc /openvpn/に移動します。
mv EasyRSA-v3.0.6 /etc/openvpn/easy-rsa
次に、ディレクトリを/ etc / openvpn / easy-rsaに変更し、EasyRSA構成ファイルを作成します。
cd /etc/openvpn/easy-rsa nano vars
国、都市、優先メールアドレスを含む次の行を追加します。
set_var EASYRSA "$PWD" set_var EASYRSA_PKI "$EASYRSA/pki" set_var EASYRSA_DN "cn_only" set_var EASYRSA_REQ_COUNTRY "INDIA" set_var EASYRSA_REQ_PROVINCE "Gujarat" set_var EASYRSA_REQ_CITY "Junagadh" set_var EASYRSA_REQ_ORG "Atlantic CERTIFICATE AUTHORITY" set_var EASYRSA_REQ_EMAIL "[email protected]" set_var EASYRSA_REQ_OU "Atlantic EASY CA" set_var EASYRSA_KEY_SIZE 2048 set_var EASYRSA_ALGO rsa set_var EASYRSA_CA_EXPIRE 7500 set_var EASYRSA_CERT_EXPIRE 365 set_var EASYRSA_NS_SUPPORT "no" set_var EASYRSA_NS_COMMENT "Atlantic CERTIFICATE AUTHORITY" set_var EASYRSA_EXT_DIR "$EASYRSA/x509-types" set_var EASYRSA_SSL_CONF "$EASYRSA/openssl-easyrsa.cnf" set_var EASYRSA_DIGEST "sha256"
終了したらファイルを保存します。
次に、次のコマンドを使用してPKIディレクトリを開始します。
./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: /etc/openvpn/easy-rsa/pkiです。
次に、次のコマンドを使用してCA証明書を作成します。
./easyrsa build-ca
次の出力が得られるはずです:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 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) Can't load /etc/openvpn/easy-rsa/pki/.rnd into RNG 139636302492992:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:98:Filename=/etc/openvpn/easy-rsa/pki/.rnd 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: /etc/openvpn/easy-rsa/pki/ca.crt
上記のコマンドは、ca.keyおよびca.crtという名前の2つのファイルを生成します。これらの証明書は、サーバーとクライアントの証明書に署名するために使用されます。
ステップ5:サーバー証明書ファイルを作成する
次に、サーバーのキーペアと証明書のリクエストを生成する必要があります。
次のコマンドを実行して、atlantic-serverという名前のサーバーキーを生成します。
./easyrsa gen-req atlantic-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 '/etc/openvpn/easy-rsa/pki/private/atlantic-server.key.IMonKybM0y' ----- 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) [atlantic-server]: Keypair and certificate request completed. Your files are: req: /etc/openvpn/easy-rsa/pki/reqs/atlantic-server.req key: /etc/openvpn/easy-rsa/pki/private/atlantic-server.key
これにより、サーバーの秘密鍵と証明書要求ファイルが生成されます。
ステップ6:CAを使用してサーバーキーに署名する
次に、CA証明書を使用してatlantic-serverキーに署名する必要があります。
次のコマンドを使用してサーバーキーに署名できます。
./easyrsa sign-req server atlantic-server
次の出力が表示されます。
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 You are about to sign the following certi ficate. 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 365 days: subject= commonName = atlantic-server Type the word 'yes' to continue, or any other input to abort. Confirm request details: yes Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf Enter pass phrase for /etc/openvpn/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:'atlantic-server' Certificate is to be certified until Jun 29 11:43:05 2021 GMT (365 days) Write out database with 1 new entries Data Base Updated Certificate created at: /etc/openvpn/easy-rsa/pki/issued/atlantic-server.crt
次に、次のコマンドを使用して、生成された証明書ファイルを確認します。
openssl verify -CAfile pki/ca.crt pki/issued/atlantic-server.crt
次の出力が得られるはずです:
pki/issued/atlantic-server.crt: OK
次に、次のコマンドを実行して、鍵交換に使用する強力なDiffie-Hellman鍵を生成します。
./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 /etc/openvpn/easy-rsa/pki/dh.pem
すべての証明書ファイルを作成したら、それらを/ etc / openvpn /server/ディレクトリにコピーします。
cp pki/ca.crt /etc/openvpn/server/ cp pki/dh.pem /etc/openvpn/server/ cp pki/private/atlantic-server.key /etc/openvpn/server/ cp pki/issued/atlantic-server.crt /etc/openvpn/server/
ステップ7:クライアント証明書とキーファイルを生成する
次に、クライアントのキーと証明書ファイルを作成する必要があります。
まず、次のコマンドを実行してクライアントキーファイルを作成します。
./easyrsa gen-req client 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 '/etc/openvpn/easy-rsa/pki/private/client.key.JmBal6cmr8' ----- 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) [client]: Keypair and certificate request completed. Your files are: req: /etc/openvpn/easy-rsa/pki/reqs/client.req key: /etc/openvpn/easy-rsa/pki/private/client.key
次に、CA証明書を使用してクライアントキーに署名します。
./easyrsa sign-req client client
次の出力が得られるはずです:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 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 365 days: subject= commonName = client Type the word 'yes' to continue, or any other input to abort. Confirm request details: yes Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf Enter pass phrase for /etc/openvpn/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:'client' Certificate is to be certified until Jun 29 11:46:58 2021 GMT (365 days) Write out database with 1 new entries Data Base Updated Certificate created at: /etc/openvpn/easy-rsa/pki/issued/client.crt
次に、すべてのクライアント証明書とキーファイルを/ etc / openvpn /client/ディレクトリにコピーします。
cp pki/ca.crt /etc/openvpn/client/ cp pki/issued/client.crt /etc/openvpn/client/ cp pki/private/client.key /etc/openvpn/client/
ステップ8:OpenVPNサーバーを構成する
この時点で、すべての証明書とキーファイルの準備ができています。次に、/ etc /openvpn/ディレクトリ内に新しいOpenVPN構成ファイルを作成します。
nano /etc/openvpn/server.conf
証明書とキーパスごとに次の行を追加します。
port 1194 proto udp dev tun ca /etc/openvpn/server/ca.crt cert /etc/openvpn/server/atlantic-server.crt key /etc/openvpn/server/atlantic-server.key dh /etc/openvpn/server/dh.pem server 10.8.0.0 255.255.255.0 push "redirect-gateway def1" push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" duplicate-cn cipher AES-256-CBC tls-version-min 1.2 tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256- CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128- CBC-SHA256 auth SHA512 auth-nocache keepalive 20 60 persist-key persist-tun compress lz4 daemon user nobody group nogroup log-append /var/log/openvpn.log verb 3
終了したらファイルを保存し、OpenVPNサービスを開始し、次のコマンドを使用してシステムの再起動後に開始できるようにします。
systemctl start [email protected] systemctl enable [email protected]
次のコマンドを実行して、OpenVPNサービスのステータスを確認します。
systemctl status [email protected]
次の出力が得られるはずです:
[email protected] - OpenVPN connection to server
Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled) Active: active (running) since Mon 2020-06-29 11:48:25 UTC; 7s ago Docs: man:openvpn(8) https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage https://community.openvpn.net/openvpn/wiki/HOWTO Main PID: 2868 (openvpn) Status: "Initialization Sequence Completed" Tasks: 1 (limit: 2353) Memory: 2.0M CGroup: /system.slice/system-openvpn.slice/[email protected] └─2868 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --conf> Jun 29 11:48:25 vpnserver systemd[1]: Starting OpenVPN connection to server... Jun 29 11:48:25 vpnserver systemd[1]: Started OpenVPN connection to server.
OpenVPNサービスが正常に開始されると、tun0という名前の新しいネットワークインターフェイスが作成されます。次のコマンドで確認できます:
ip a show tun0
次の出力で新しいインターフェイスtun0を取得する必要があります。
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel 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::5f83:99a:30d:eb0/64 scope link stable-privacy valid_lft forever preferred_lft forever
ステップ9:クライアント構成ファイルを作成する
次に、client.ovpnという名前のOpenVPNクライアント構成ファイルを作成する必要があります。クライアントシステムからOpenVPNサーバーに接続するには、このファイルが必要です。
nano /etc/openvpn/client/client.ovpn
次の行を追加します:
client dev tun proto udp remote your-vpn-server-ip 1194 ca ca.crt cert client.crt key client.key cipher AES-256-CBC auth SHA512 auth-nocache tls-version-min 1.2 tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256- CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128- CBC-SHA256 resolv-retry infinite compress lz4 nobind persist-key persist-tun mute-replay-warnings verb 3
終了したらファイルを保存します。
ステップ10:OpenVPNクライアントのインストールと構成
次に、クライアントシステムにOpenVPNをインストールし、OpenVPNサーバーに接続する必要があります。
まず、クライアントマシンにログインし、次のコマンドを使用してOpenVPNパッケージをインストールします。
apt-get install openvpn -y
次に、OpenVPNクライアント構成ファイルをOpenVPNサーバーからクライアントマシンにコピーする必要があります。
クライアントマシンで、次のコマンドを実行して、すべてのクライアント構成ファイルをダウンロードします。
scp -r [email protected]:/etc/openvpn/client .
すべてのファイルがコピーされたら、ディレクトリをクライアントに変更し、次のコマンドを実行してOpenVPNサーバーに接続します。
cd client openvpn --config client.ovpn
OpenVPNサーバーに接続すると、次の出力が表示されます。
Jun 29 11:48:27 2020 TCP/UDP: Preserving recently used remote address: [AF_INET]69.87.218.145:1194 Jun 29 11:48:27 2020 Socket Buffers: R=[212992->212992] S=[212992->212992] Jun 29 11:48:27 2020 UDP link local: (not bound) Jun 29 11:48:27 2020 UDP link remote: [AF_INET]69.87.218.145:1194 Jun 29 11:48:27 2020 TLS: Initial packet from [AF_INET]69.87.218.145:1194, sid=6d27e1cb 524bd8cd Jun 29 11:48:27 2020 VERIFY OK: depth=1, CN=Easy-RSA CA Jun 29 11:48:27 2020 VERIFY OK: depth=0, CN=atlantic-server Jun 29 11:48:27 2020 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, 2048 bit RSA Jun 29 11:48:27 2020 [atlantic-server] Peer Connection Initiated with [AF_INET]69.87.218.145:1194 Jun 29 11:48:27 2020 SENT CONTROL [atlantic-server]: 'PUSH_REQUEST' (status=1) Jun 29 11:48:27 2020 PUSH: Received control message: 'PUSH_REPLY,redirect-gateway def1,dhcp-option DNS 208.67.222.222,dhcp-option DNS 208.67.220.220,route 10.8.0.1,topology net30,ping 20,ping-restart 60,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM' Jun 29 11:48:27 2020 OPTIONS IMPORT: timers and/or timeouts modified Jun 29 11:48:27 2020 OPTIONS IMPORT: --ifconfig/up options modified Jun 29 11:48:27 2020 OPTIONS IMPORT: route options modified
接続が成功すると、OpenVPNはシステムにIPアドレスを割り当てます。次のコマンドで確認できます:
ip a show tun0
出力:
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100 link/none inet 10.8.0.6 peer 10.8.0.5/32 scope global tun0 valid_lft forever preferred_lft forever inet6 fe80::7226:57b1:f101:313b/64 scope link stable-privacy valid_lft forever preferred_lft forever
次のコマンドを使用して、OpenVPNサーバーログをチェックして接続ステータスを確認することもできます。
tail -f /var/log/openvpn.log
結論
おめでとう!これで、Ubuntu20.04VPSでOpenVPNを使用してVPNサーバーを正常にセットアップできました。これで、インターネットに安全にアクセスして、ID、場所、およびトラフィックを保護できます。詳細については、OpenVPNの公式ドキュメントをご覧ください。