GNU/Linux >> Linux の 問題 >  >> Debian

Debian10でOpenVPNサーバーをセットアップする方法

仮想プライベートネットワーク(VPN)は、2つのネットワークと個々のユーザーの間の安全で暗号化された接続であり、インターネット接続とオンラインのプライバシーを保護します。 OpenVPNは、ルーティングされた構成で安全なポイントツーポイントを作成するための技術を実装する、無料のオープンソースVPNプロトコルです。クロスプラットフォームであり、すべての主要なオペレーティングシステムと互換性があります。

このチュートリアルでは、Debian10サーバーにOpenVPNをインストールしてセットアップする方法を紹介します。

前提条件
  • Debian 10 VPS(NVMe 2 VPSプランを使用します)
  • rootユーザーアカウントへのアクセス(またはroot権限を持つ管理者アカウントへのアクセス)

ステップ1:サーバーにログインしてサーバーOSパッケージを更新する

まず、rootユーザーとしてSSH経由でDebian10サーバーにログインします。

ssh root@IP_Address -p Port_number

「IP_Address」と「Port_number」をサーバーのそれぞれのIPアドレスとSSHポート番号に置き換える必要があります。さらに、必要に応じて「root」を管理者アカウントのユーザー名に置き換えます。

開始する前に、サーバーにインストールされているすべてのDebianOSパッケージが最新であることを確認する必要があります。これを行うには、次のコマンドを実行します。

apt-get update -y
apt-get upgrade -y

ステップ2:OpenVPNとEasyRSAをインストールする

デフォルトでは、OpenVPNはDebianのデフォルトリポジトリに含まれています。次のコマンドでインストールできます:

apt-get install openvpn -y

OpenVPNパッケージをインストールしたら、EasyRSAをシステムにダウンロードする必要があります。

EasyRSAは、PKICAを構築および管理するためのコマンドラインユーティリティです。複数の種類の証明書を生成できます。
ルート認証局を作成し、OpenVPNの証明書を要求して署名するために使用されます。

次のコマンドを使用して、GitリポジトリからEasyRSAの最新バージョンをダウンロードできます。

wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.8/EasyRSA-3.0.8.tgz

ダウンロードが完了したら、次のコマンドを使用してダウンロードしたファイルを抽出します。

tar -xvzf EasyRSA-3.0.8.tgz

次に、抽出したディレクトリをOpenVPNディレクトリにコピーします。

cp -r EasyRSA-3.0.8 /etc/openvpn/easy-rsa

この時点で、OpenVPNとEasyRSAがサーバーにインストールされています。

ステップ3:認証局を構築する

次に、OpenVPNの認証局(CA)を構築する必要があります。

まず、次のコマンドを使用してディレクトリをEasyRSAに変更します。

cd /etc/openvpn/easy-rsa

次に、この中にvarsファイルを作成する必要があります。 varsファイルは、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     "USA"
set_var EASYRSA_REQ_PROVINCE    "Newyork"
set_var EASYRSA_REQ_CITY        "Newyork"
set_var EASYRSA_REQ_ORG         "ROSE CERTIFICATE AUTHORITY"
set_var EASYRSA_REQ_EMAIL    "[email protected]"
set_var EASYRSA_REQ_OU          "ROSE 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    "ROSE CERTIFICATE AUTHORITY"
set_var EASYRSA_EXT_DIR         "$EASYRSA/x509-types"
set_var EASYRSA_SSL_CONF        "$EASYRSA/openssl-easyrsa.cnf"
set_var EASYRSA_DIGEST          "sha256"

終了したら、ファイルを保存して閉じます。

次に、次のコマンドを使用して公開鍵インフラストラクチャを開始します。

./easyrsa init-pki

次の出力が得られるはずです:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki
です。

次に、build-caコマンドを実行してca.crtおよびca.keyファイルを作成する必要があります。次のコマンドで実行できます:

./easyrsa build-ca nopass

以下に示すように、いくつかの質問があります。

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/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)
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]:vpnserver

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

ステップ4:サーバー証明書とキーファイルを生成する

次に、gen-reqコマンドに続けて一般名を使用して、サーバーキーを生成する必要があります。

./easyrsa gen-req vpnserver nopass

次の出力が表示されます。

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/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/easy-rsa-1428.Angtmh/tmp.C9prw4'
-----
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) [vpnserver]:

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/vpnserver.req
key: /etc/openvpn/easy-rsa/pki/private/vpnserver.key

次に、CA証明書を使用してvpnserverキーに署名する必要があります。次のコマンドで実行できます:

./easyrsa sign-req server vpnserver

次の出力が得られるはずです:

Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'vpnserver'
Certificate is to be certified until Feb  6 14:38:52 2022 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/vpnserver.crt
で作成されました

次に、鍵交換に使用する強力なDiffie-Hellman鍵を生成する必要があります。次のコマンドで生成できます:

./easyrsa gen-dh

ステップ5:すべての証明書とキーファイルをコピーする

次に、すべての証明書とキーファイルを/ etc / openvpn /server/ディレクトリにコピーする必要があります。次のコマンドを使用してコピーできます:

cp pki/ca.crt /etc/openvpn/server/
cp pki/dh.pem /etc/openvpn/server/
cp pki/private/vpnserver.key /etc/openvpn/server/
cp pki/issued/vpnserver.crt /etc/openvpn/server/

ステップ6:クライアント証明書とキーファイルを作成する

次に、クライアントシステムの証明書とキーファイルを生成する必要があります。

次のコマンドで作成できます:

./easyrsa gen-req vpnclient nopass

次の出力が得られるはずです:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/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/easy-rsa-1563.TeOf5v/tmp.i4YxLz'
-----
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) [vpnclient]:

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/vpnclient.req
key: /etc/openvpn/easy-rsa/pki/private/vpnclient.key

次に、次のコマンドを使用してクライアントキーに署名します。

./easyrsa sign-req client vpnclient

次の出力が得られるはずです:

Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'vpnclient'
Certificate is to be certified until Feb  6 14:43:18 2022 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/vpnclient.crt

次に、すべてのクライアント証明書とキーを/ etc / openvpn /client/ディレクトリにコピーします。

cp pki/ca.crt /etc/openvpn/client/
cp pki/issued/vpnclient.crt /etc/openvpn/client/
cp pki/private/vpnclient.key /etc/openvpn/client/

ステップ7:OpenVPNサーバーを構成する

この時点で、サーバーとクライアントの両方の証明書とキーの準備ができています。次に、OpenVPN構成ファイルを作成し、すべての証明書とキーを定義する必要があります。

nano /etc/openvpn/server.conf

次の行を追加します:

port 1194
proto udp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/vpnserver.crt
key /etc/openvpn/server/vpnserver.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 openvpn@server
systemctl enable openvpn@server

すべてが正常であれば、新しいインターフェースが作成されます。次のコマンドを使用して確認できます:

ip a show tun0

次の出力が得られるはずです:

4: tun0:  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::153d:f29:39a2:571a/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

ステップ8:IP転送を有効にする

IP転送により、オペレーティングシステムは着信ネットワークパケットを受け入れ、それを他のネットワークに転送できます。次のコマンドで有効にできます:

nano /etc/sysctl.conf

コメントを外すか、次の行を追加します:

net.ipv4.ip_forward = 1

ファイルを保存してから、次のコマンドを使用して構成の変更を適用します。

sysctl -p

ステップ9:OpenVPNクライアントのインストールと構成

次に、OpenVPNクライアントを別のシステムにインストールし、OpenVPNサーバーに接続する必要があります。

まず、次のコマンドを使用してOpenVPNをインストールします。

apt-get install openvpn -y

インストールしたら、すべてのクライアント証明書とキーをOpenVPNサーバーからクライアントマシンにコピーします。次のコマンドで実行できます:

scp -r root@vpn-server-ip:/etc/openvpn/client .

次に、ディレクトリをclientに変更し、クライアント構成ファイルを作成します。

cd client
nano client.ovpn

次の行を追加します:

client
dev tun
proto udp
remote vpn-server-ip 1194
ca ca.crt
cert vpnclient.crt
key vpnclient.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

ファイルを保存して閉じてから、次のコマンドを使用してOpenVPNサーバーに接続します。

openvpn --config client.ovpn

接続が確立されると、次の出力が表示されます。

Sat Feb  6 14:53:50 2021 SENT CONTROL [vpnserver]: 'PUSH_REQUEST' (status=1)
Sat Feb  6 14:53:50 2021 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'
Sat Feb  6 14:53:50 2021 OPTIONS IMPORT: timers and/or timeouts modified
Sat Feb  6 14:53:50 2021 OPTIONS IMPORT: --ifconfig/up options modified
Sat Feb  6 14:53:50 2021 OPTIONS IMPORT: route options modified

次のコマンドを使用して、クライアントマシンのOpenVPNインターフェースを確認できます。

ip a show tun0

次の出力が得られるはずです:

4: tun0:  mtu 1500 qdisc pfifo_fast 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::9206:94d7:8fb2:6b21/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

もちろん、マネージドOpenVPNホスティングサービスのいずれかを使用している場合は、OpenVPNをインストールする必要はありません。その場合は、専門のLinux管理者にインストールを依頼するだけです。 24時間年中無休でご利用いただけます。リクエストはすぐに処理されます。

PS 。 UbuntuにOpenVPNをインストールする方法に関するこの投稿が気に入った場合は、左側のボタンを使用してソーシャルネットワーク上の友達と共有するか、下に返信を残してください。ありがとう。


Debian
  1. Debian10にOpenVPNサーバーをインストールして設定する方法

  2. Debian 11に静的IPを設定する-その方法は?

  3. Debian11サーバーでSFTPサーバーをセットアップする方法

  1. Debian10BusterでSambaサーバーをセットアップする方法

  2. Debian10BusterでLAMPサーバーをセットアップする方法

  3. Debian10BusterでNFSサーバーをセットアップする方法

  1. Debian10BusterでLEMPサーバーをセットアップする方法

  2. Ubuntu18.04でOpenVPNサーバーをセットアップする方法

  3. CentOS7でOpenVPNサーバーをセットアップする方法