OpenVPNは、機能豊富なオープンソースのSecure Socket Layer(SSL)VPNです。 VPNを使用すると、ホテル、空港、モールのWIFIネットワークなどの信頼できないネットワークに安全に接続できます。 VPNを使用すると、企業ネットワークへの安全な接続でリソースにアクセスすることもできます。トンネルは、SSL / TLS認証、証明書、資格情報を使用して保護されています。
このチュートリアルでは、 VPNを設定する方法を紹介します OpenVPNを使用する Ubuntu 20.04 。
前提条件
バージョン20.04で実行されている2つのUbuntuサーバーを使用します:
- 認証局(CA)サーバー リクエストを検証し、クライアントの証明書に署名します。
- OpenVPN VPNをインストールするサーバー。
CAサーバーの構成
CA(認証局)として機能するスタンドアロンサーバーを維持することをお勧めします。これはセキュリティ上の理由によるものです。 CAサーバーの構成に進みましょう。
まず、システムが更新されていることを確認します。次のコマンドを実行します:
$ sudo apt update
認証局サーバーの構成には、root以外のユーザーを作成する必要があります。
$ sudo adduser malain
次に、ユーザーにsudo権限を付与します。
$ sudo usermod -aG sudo malain
次に、ログアウトしてから、root以外のユーザーで再度ログインします。
CAサーバーにEasyRSAをインストールする
Easy-rsaは、PKICAを構築および管理するためのCLIユーティリティです。 Easy-RSAは、CAサーバーが秘密鍵と公開ルート証明書を生成するために使用します。これらの証明書は、CAに依存するクライアントとサーバーからの要求に署名するために使用されます。
easy-rsaをインストールするには、wgetを使用してgithubからPKI管理ツールをダウンロードします。
$ wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.8/EasyRSA-3.0.8.tgz
次に、tgzファイルを解凍します:
$ tar xvf EasyRSA-3.0.8.tgz
ユーザーのみにアクセスを制限するには、次を使用します:
$ chmod 700 EasyRSA-3.0.8
次に、EasyRSAをopt
に移動します ディレクトリ
$ sudo mv EasyRSA-3.0.8 /opt/
EasyRSA 3.0.8を使用しています この構成のバージョン。
EasyRSAを使用してCAを作成する
まず、vars
という名前のファイルを作成する必要があります 組織情報を保存します。このために、EasyRSA-3.0.8ディレクトリにあるサンプルファイルを使用できます。
EasyRSAディレクトリの下のファイルを一覧表示するには、次を使用します。
$ cd EasyRSA-3.0.8/ && ls -l
出力:
-rw-rw-r-- 1 malain malain 1305 Sep 9 2020 COPYING.md
-rw-rw-r-- 1 malain malain 5056 Sep 9 2020 ChangeLog
-rw-rw-r-- 1 malain malain 2049 Sep 9 2020 README.md
-rw-rw-r-- 1 malain malain 3335 Sep 9 2020 README.quickstart.md
drwxrwxr-x 2 malain malain 4096 Sep 9 2020 doc
-rwxrwxr-x 1 malain malain 76946 Sep 9 2020 easyrsa
-rw-rw-r-- 1 malain malain 18093 Sep 9 2020 gpl-2.0.txt
-rw-rw-r-- 1 malain malain 1036 Sep 9 2020 mktemp.txt
-rw-rw-r-- 1 malain malain 4616 Sep 9 2020 openssl-easyrsa.cnf
-rw-rw-r-- 1 malain malain 8925 Sep 9 2020 vars.example
drwxrwxr-x 2 malain malain 4096 Mar 28 14:14 x509-types
vars.exampleファイルのコピーをvarsとして作成します:
$ cp vars.example vars
次に、varsファイルを開き、ファイルの最後に組織情報を追加します。
$ vim vars
set_var EASYRSA_REQ_COUNTRY "CM"
set_var EASYRSA_REQ_PROVINCE "Littoral"
set_var EASYRSA_REQ_CITY "Douala"
set_var EASYRSA_REQ_ORG "OPEN-SHARE"
set_var EASYRSA_REQ_EMAIL "[email protected]"
set_var EASYRSA_REQ_OU "Com"
set_var EASYRSA_ALGO "ec"
set_var EASYRSA_DIGEST "sha512"
次に、CAサーバーでPKIを初期化します。これにより、pkiフォルダーが作成されます。
$ ./easyrsa init-pki
これで、ルートパブリックを生成できます および秘密鍵ペア 私たちのCAのために。プロセス中、通常、証明書に署名または取り消す必要があるときはいつでも、キーペアのパスフレーズを入力するように求められます。この例では、パスフレーズの入力を求められないようにコマンドを使用します。また、一般名を指定するように求められます (CN)ただし、Enterキーを押してデフォルトのままにします。
$ ./easyrsa build-ca nopass
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/vars
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
read EC key
writing EC key
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:
/opt/EasyRSA-3.0.8/pki/ca.crt
これにより、ルート証明書が作成されます pkiディレクトリの名前付きca.crtファイルとp秘密鍵 pki/privateディレクトリのca.key。
$ ls -l pki/
total 52
-rw------- 1 malain malain 749 Mar 28 14:30 ca.crt
drwx------ 2 malain malain 4096 Mar 28 14:29 certs_by_serial
drwx------ 2 malain malain 4096 Mar 28 14:29 ecparams
-rw------- 1 malain malain 0 Mar 28 14:29 index.txt
-rw------- 1 malain malain 0 Mar 28 14:29 index.txt.attr
drwx------ 2 malain malain 4096 Mar 28 14:29 issued
-rw------- 1 malain malain 4616 Mar 28 14:24 openssl-easyrsa.cnf
drwx------ 2 malain malain 4096 Mar 28 14:30 private
drwx------ 5 malain malain 4096 Mar 28 14:29 renewed
drwx------ 2 malain malain 4096 Mar 28 14:24 reqs
drwx------ 5 malain malain 4096 Mar 28 14:29 revoked
-rw------- 1 malain malain 4575 Mar 28 14:24 safessl-easyrsa.cnf
-rw------- 1 malain malain 3 Mar 28 14:29 serial
$ ls -l pki/private/
total 4
-rw------- 1 malain malain 288 Mar 28 14:29 ca.key
これで、CAサーバーの準備が整いました。
Openvpnサーバーのインストールと構成
2番目のサーバーにジャンプして、openvpnをインストールして構成しましょう。また、このサーバーでroot以外のユーザーを作成し、そのユーザーにsudo
を与える必要があります。 特権。
openvpnをインストールするには ubuntuで、次のコマンドを実行します:
$ sudo apt update
$ sudo apt install openvpn
openvpnサーバーでは、easyrsaを使用して、CAサーバーによって検証および署名される証明書要求を生成します。前のセクションで行ったのと同じ手順に従って、Easyrsaをインストールします。
$ wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.8/EasyRSA-3.0.8.tgz
ダウンロードしたtgzファイルを解凍します:
$ tar xvf EasyRSA-3.0.8.tgz
ユーザーのみにアクセスを制限します:
$ chmod 700 EasyRSA-3.0.8
EasyRSA-3.0.8をopt
に移動します ディレクトリ:
$ sudo mv EasyRSA-3.0.8 /opt/
PKIを作成する
次に、PKIを作成する必要があります これは、VPNに接続するクライアントおよびその他のサーバーのTLS証明書を要求および管理するのに役立ちます。
すでに利用可能なサンプルファイルを使用してvarsファイルを作成しましょう:
$ cp vars.example vars
次に、ファイルの最後に次の行を追加して、varsファイルを編集します。
$ vim vars
set_var EASYRSA_ALGO "ec"
set_var EASYRSA_DIGEST "sha512"
PKIフォルダを作成するには openvpnサーバーで、easyrsa
を実行します スクリプトファイル:
$ ./easyrsa init-pki
出力:
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/vars init-pki complete; you may now create a CA or requests. Your newly created PKI dir is: /opt/EasyRSA-3.0.8/pkiです。
サーバー証明書リクエストと秘密鍵を作成します
次に、秘密鍵を生成します および証明書のリクエスト OpenVPNサーバー上。次に、必要な証明書を作成するために、署名する証明書要求ファイルをCAサーバーに転送します
同じフォルダーにあるため、nopass
を使用してリクエストを生成できます オプション。次のコマンドは、openvpn-server.keyという名前の秘密鍵ファイルとopenvpn-server.reqという名前の証明書要求ファイルを作成します。
$ ./easyrsa gen-req openvpn-server nopass
出力:
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/vars
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
Generating an EC private key
writing new private key to '/opt/EasyRSA-3.0.8/pki/easy-rsa-4737.CGhQHN/tmp.UGQ9wi'
-----
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) [openvpn-server]:
Keypair and certificate request completed. Your files are:
req: /opt/EasyRSA-3.0.8/pki/reqs/openvpn-server.req
key: /opt/EasyRSA-3.0.8/pki/private/openvpn-server.key
次に、openvpn-server.keyという名前のサーバーキーファイルを/ etc / openvpn/serverという名前のディレクトリにコピーします。
$ sudo cp /opt/EasyRSA-3.0.8/pki/private/openvpn-server.key /etc/openvpn/server
次に、証明書要求ファイルをCAサーバーにコピーします :
$ scp /opt/EasyRSA-3.0.8/pki/reqs/openvpn-server.req [email protected]:/tmp
CAサーバーで、easyrsaディレクトリに移動します。
$ cd /opt/EasyRSA-3.0.8/
リクエストをインポートする 、次のコマンドを実行します:
$ ./easyrsa import-req /tmp/openvpn-server.req openvpn-server
出力:
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/vars
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
The request has been successfully imported with a short name of: openvpn-server
You may now use this name to perform signing operations on this request.
次に、リクエストに署名します。サーバーのリクエストに署名しているので、ディレクティブserver
を使用する必要があります openvpnサーバーの共通名の前。クライアントのリクエストの場合は、ディレクティブclient
を使用する必要があります 代わりに。
リクエストに署名する :
$ ./easyrsa sign-req server openvpn-server
出力:
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/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 server certificate for 825 days:
subject=
commonName = openvpn-server
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes
Using configuration from /opt/EasyRSA-3.0.8/pki/easy-rsa-4100.IbygpP/tmp.hJY2T5
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'openvpn-server'
Certificate is to be certified until Jul 1 19:50:36 2023 GMT (825 days)
Write out database with 1 new entries
Data Base Updated
Certificate created at: /opt/EasyRSA-3.0.8/pki/issued/openvpn-server.crt
これで、OpenVPNサーバーの証明書要求がCAサーバーによって署名されたので、署名された要求と公開証明書をOpenVPNサーバーに転送する必要があります。
$ scp pki/{ca.crt,issued/openvpn-server.crt} [email protected]:/tmp
$ sudo cp /tmp/{ca.crt,openvpn-server.crt} /etc/openvpn/server
ここで、tls-crypt事前共有キーを生成して、OpenVPNサーバーが認証されていないトラフィック、ポートスキャン、およびサーバーのリソースを大量に使用する可能性のある一部の攻撃に対処できるようにします。
$ ./easyrsa gen-dh
出力:
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/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 /opt/EasyRSA-3.0.8/pki/dh.pem
$ openvpn --genkey --secret ta.key
次に、キーファイルとpemファイルをディレクトリ/ etc / openvpn / server:
にコピーします。$ sudo cp ta.key pki/dh.pem /etc/openvpn/server
クライアント証明書とキーのペアを生成する
クライアントの証明書とキーを保持するためのディレクトリを作成する必要があります。 root以外のユーザーに許可を与えるようにしてください。
$ sudo mkdir -p /opt/client-configs/keys
$ sudo chown franck:franck -R /opt/client-configs
$ sudo chmod 700 -R /opt/client-configs
クライアント証明書のリクエストを生成します:
$ cd /opt/EasyRSA-3.0.8
$ ./easyrsa gen-req my-pc nopass
出力:
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/vars Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020 Generating an EC private key writing new private key to '/opt/EasyRSA-3.0.8/pki/easy-rsa-6961.m7fBMu/tmp.dkqaZI' ----- 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) [my-pc]: Keypair and certificate request completed. Your files are: req: /opt/EasyRSA-3.0.8/pki/reqs/my-pc.req key: /opt/EasyRSA-3.0.8/pki/private/my-pc.key
次に、クライアントキーをclient-configsディレクトリにコピーします。
$ cp pki/private/my-pc.key /opt/client-configs/keys/
クライアント証明書要求ファイルをCAサーバーにコピーします:
$ scp pki/reqs/my-pc.req [email protected]:/tmp
CAサーバーで、CSRをインポートします:
$ cd /opt/EasyRSA-3.0.8
$ ./easyrsa import-req /tmp/my-pc.req my-pc
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/vars
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
The request has been successfully imported with a short name of: my-pc
You may now use this name to perform signing operations on this request.
次に、クライアントのリクエストに署名する必要があります。次のように入力します。
$ ./easyrsa sign-req client my-pc
出力:
Note: using Easy-RSA configuration from: /opt/EasyRSA-3.0.8/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 825 days:
subject=
commonName = my-pc
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes
Using configuration from /opt/EasyRSA-3.0.8/pki/easy-rsa-5511.IwDcbS/tmp.doUbCv
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'my-pc'
Certificate is to be certified until Jul 1 21:33:52 2023 GMT (825 days)
Write out database with 1 new entries
Data Base Updated
Certificate created at: /opt/EasyRSA-3.0.8/pki/issued/my-pc.crt
openvpnサーバーにクライアント証明書をコピーします:
$ scp pki/issued/my-pc.crt [email protected]:/tmp
ここで、openvpnサーバーで、すべてのクライアントファイルを以前に作成したクライアントディレクトリにコピーする必要があります。
$ sudo cp /tmp/my-pc.crt /opt/client-configs/keys/
$ sudo cp /etc/openvpn/server/ca.crt /opt/client-configs/keys/
$ cp /opt/EasyRSA-3.0.8/ta.key /opt/client-configs/keys/
root以外のユーザーがファイルに対する権限を持っていることを確認してください。
$ sudo chown franck:franck /opt/client-configs/keys/*
VPNサービスの構成
まず、使用するテンプレートのコピーを作成する必要があります
$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/server/
次に、ファイルを解凍します
$ sudo gzip -d /etc/openvpn/server/server.conf.gz
そしてフォルダに入る
$ cd /etc/openvpn/server
一部の行は、構成に合わせて置き換えられます::
- 暗号化暗号は
AES-256-CBC
に設定されます - HMACメッセージダイジェストアルゴリズムは
sha256
になります - Diffie-Hellmanパラメーターも使用するため、
dh.pem
に設定します。 - ユーザーを使用します誰も およびグループnogroup 開始後に権限なしでopenvpnを実行するには
- dnsの変更をプッシュして、値が
push redirect-gateway def1 bypass-dhcp
のVPNを介してすべてのトラフィックをリダイレクトします。 、push dhcp-option DNS 208.67.222.222
、push dhcp-option DNS 208.67.220.220
- デフォルトのポート
1194
を維持します -
udp
を使用します プロトコル - キー
openvpn-server.key
を指定する必要があります および証明書openvpn-server.crt
クレデンシャルとして使用する
したがって、ファイルは次のようになります。
$ sudo vim /etc/openvpn/server/server.conf
tls-crypt ta.key
cipher AES-256-CBC
auth SHA256
dh dh.pem
user nobody
group nogroup
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
port 1194
proto udp
explicit-exit-notify 1
cert openvpn-server.crt
key openvpn-server.key
/etc/sysctl.confのnet.ipv4.ip_forward値を編集して、IPv転送をアクティブ化する必要があります。
$ sudo vim /etc/sysctl.conf
net.ipv4.ip_forward=1
/etc/sysctl.confで行った変更を有効にするには、次のコマンドを実行します。
$ sudo sysctl -p
net.ipv4.ip_forward = 1
次に、マスカレードを有効にして、ファイアウォールを通過するOpenVPNを許可する必要があります。そのためには、パブリックネットワークインターフェースの名前を特定する必要があります。
$ ip route | grep default
default via X.X.X.X dev eth0 proto static
私たちの場合、それはeth0です。次に、ファイアウォール構成にマスカレードルールを追加する必要があるため、ルールのファイルの先頭に以下の行を追加します。
$ sudo vim /etc/ufw/before.rules
# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to the public server interface eth0
-A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE
COMMIT
# END OPENVPN RULES
次に、ufwを編集して、デフォルトで転送されたパケットを許可しましょう。
$ sudo vim /etc/default/ufw
DEFAULT_FORWARD_POLICY="ACCEPT"
ファイアウォールでopenvpnポートを開きます:
$ sudo ufw allow '1194/udp'
ufwサービスを再起動するには、次のように入力します。
$ sudo ufw disable && sudo ufw enable
システムの起動時にopenvpnサービスを開始できるようにするには、次のように入力します。
$ sudo systemctl enable openvpn-server@server
openvpnを開始するには、次のように入力します:
$ sudo systemctl start openvpn-server@server
openvpnのステータスは次の方法で確認できます:
$ sudo systemctl status openvpn-server@server
出力:
● [email protected] - OpenVPN service for server
Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled)
Active: active (running) since Sun 2021-03-28 23:30:57 UTC; 8s ago
Docs: man:openvpn(8)
https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
https://community.openvpn.net/openvpn/wiki/HOWTO
Main PID: 78132 (openvpn)
Status: "Initialization Sequence Completed"
Tasks: 1 (limit: 1073)
Memory: 1.0M
CGroup: /system.slice/system-openvpn\x2dserver.slice/[email protected]
└─78132 /usr/sbin/openvpn --status /run/openvpn-server/status-server.log --status-version 2 --suppress-timestamps --config server.conf
Mar 28 23:30:57 localhost systemd[1]: Starting OpenVPN service for server...
Mar 28 23:30:57 localhost systemd[1]: Started OpenVPN service for server.
トンネルインターフェースを確認するには、次のように入力します。
$ ip addr show tun0
出力:
3: 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::c3d9:85d1:e2a9:6b2c/64 scope link stable-privacy
valid_lft forever preferred_lft forever
クライアントのVPNファイルを構成する
$ sudo mkdir -p /opt/client-configs/files
次に、サンプル構成ファイルをコピーする必要があります
$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /opt/client-configs/base.conf
root以外のユーザーに権限を付与する
$ sudo chown franck:franck -R /opt/client-configs/
base.confファイルを編集します
$ vim /opt/client-configs/base.conf
remote your_server_ip 1194
proto udp
# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup
#ca ca.crt
#cert client.crt
#key client.key
#tls-auth ta.key 1
cipher AES-256-CBC
auth SHA256
key-direction 1
# For the clients using resolvconf for DNS resolution, uncomment the lines below on the client computer
; script-security 2
; up /etc/openvpn/update-resolv-conf
; down /etc/openvpn/update-resolv-conf
# Instead for the clients using systemd-resolved for DNS resolution, uncomment the lines below on the client computer
; script-security 2
; up /etc/openvpn/update-systemd-resolved
; down /etc/openvpn/update-systemd-resolved
; down-pre
; dhcp-option DOMAIN-ROUTE .
次に、クライアントで証明書と暗号化ファイルを生成するスクリプトを作成します。このスクリプトは、base.confファイルのコピーも作成し、クライアント用に作成されたすべてのキーと証明書を収集します。クライアントごとに、スクリプトを実行する前に証明書とキーを生成する必要があります
$ vim /opt/client-configs/make_config.sh
#!/bin/bash
# First argument: Client identifier
KEY_DIR=/opt/client-configs/keys
OUTPUT_DIR=/opt/client-configs/files
BASE_CONFIG=/opt/client-configs/base.conf
cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>\n<cert>') \
${KEY_DIR}/${1}.crt \
<(echo -e '</cert>\n<key>') \
${KEY_DIR}/${1}.key \
<(echo -e '</key>\n<tls-auth>') \
${KEY_DIR}/ta.key \
<(echo -e '</tls-auth>') \
> ${OUTPUT_DIR}/${1}.ovpn
root以外のユーザーのみがスクリプトを実行できることを確認してください。
$ chmod 700 /opt/client-configs/make_config.sh
これで、クライアントキーと証明書の構成(my-pc.crtおよびmy-pc.key)に基づいてクライアント接続ファイルを生成できます
$ cd /opt/client-configs
次に、スクリプトを実行してから、使用するクライアントVPNファイルを作成するクライアントに使用される一般名を実行します。
$ ./make_config.sh my-pc
結果を確認できます:
$ ls -l files/
total 12
-rw-rw-r-- 1 franck franck 8598 Mar 30 11:12 my-pc.ovpn
クライアントをOpenVPN接続に接続します
クライアントにopenvpnをインストールします。これは、サーバーとのVPN接続を確立するために使用されます
$ sudo apt update && sudo apt install openvpn -y
次に、サーバー上にあるOpenVPNクライアントファイルをクライアントコンピューターにコピーします。したがって、クライアントコンピュータで、次のコマンドを実行します。
$ rsync -av [email protected]:/opt/client-configs/files/my-pc.ovpn .
クライアントのOpenVPN構成ファイルを編集する前に、DNS解決にresolvconfまたはsystemd-resolvedのどちらを使用しているかを確認する必要があります
$ cat /etc/resolv.conf
OUTPUT:
# This file is managed by man:systemd-resolved(8). Do not edit.
. . .
nameserver 127.0.0.53
options edns0
値がnameserver 127.0.0.53
の場合 、systemd-resolvedを使用していることを示しています。そのため、接続時にDNS解決にVPNを使用するようにsystemd-resolvedを支援するパッケージをインストールしてください。
$ sudo apt install openvpn-systemd-resolved
これで、systemd-resolvedに必要な行のコメントを解除することでVPNクライアントファイルを編集できます
$ vim my-pc.ovpn
script-security 2
up /etc/openvpn/update-systemd-resolved
down /etc/openvpn/update-systemd-resolved
down-pre
dhcp-option DOMAIN-ROUTE .
update-resolv-confを使用しているシステムの場合、行のコメントを解除します
script-security 2
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf
それでは、VPNへの接続を試してみましょう。 クライアント構成ファイルへのopenvpnコマンドの使用:
$ sudo openvpn --config my-pc.ovpn
トンネルインターフェースのIP情報を確認できます
$ ip a
これにより、トンネルIPアドレスが表示され、OpenVPNサーバーにpingを実行できます。これにより、同じプライベートネットワークにいるかのようにサーバーにアクセスできることを確認できます。
結論
OpenVPNを使用すると、VPN接続を簡単に設定できます。これは、仮想ファイアウォールなどの特殊なソリューションを使用せずに、クラウドサーバーにVPNソリューションをセットアップする場合に適したソリューションです。これは高速で安全なソリューションです。