GNU/Linux >> Linux の 問題 >  >> Panels >> OpenVPN

Ubuntu22.04でWireGuardを設定する方法

はじめに

WireGuardは、IPv4およびIPv6接続をサポートする軽量の仮想プライベートネットワーク(VPN)です。 VPNを使用すると、信頼できないネットワークをプライベートネットワーク上にいるかのようにトラバースできます。ホテルやコーヒーショップのWiFiなど、信頼できないネットワークに接続している場合は、スマートフォンやラップトップからインターネットに安全かつ確実にアクセスできます。

WireGuardの暗号化は、ピアが相互に暗号化されたトンネルを確立するための公開鍵と秘密鍵に依存しています。 WireGuardの各バージョンは、特定の暗号化暗号スイートを使用して、シンプルさ、セキュリティ、およびピアとの互換性を確保します。

対照的に、OpenVPNやIPSecなどの他のVPNソフトウェアは、トランスポート層セキュリティ(TLS)と証明書を使用して、システム間の暗号化されたトンネルを認証および確立します。 TLSのさまざまなバージョンには、何百ものさまざまな暗号化スイートとアルゴリズムのサポートが含まれています。これにより、さまざまなクライアントをサポートするための優れた柔軟性が得られますが、TLSを使用するVPNの構成は、より時間がかかり、複雑で、エラーが発生しやすくなります。

このチュートリアルでは、Ubuntu 22.04サーバーでWireGuardをセットアップし、IPv4接続とIPv6接続の両方を使用してピアとして接続するように別のマシンを構成します(一般にデュアルスタックと呼ばれます)。 繋がり)。また、暗号化されたピアツーピアトンネルにVPNを使用することに加えて、ゲートウェイ構成でWireGuardサーバーを介してピアのインターネットトラフィックをルーティングする方法についても学習します。

このチュートリアルでは、別のUbuntu 22.04システムをWireGuardサーバーのピア(クライアントとも呼ばれる)として構成します。このシリーズの後続のチュートリアルでは、Windows、macOS、Android、およびiOSのシステムとデバイスにWireGuardをインストールして実行する方法について説明します。

注: DigitalOcean DropletにWireGuardをセットアップする場合は、多くのホスティングプロバイダーと同様に、帯域幅の超過に対して課金されることに注意してください。このため、サーバーが処理しているトラフィックの量に注意してください。詳細については、このページを参照してください。

前提条件

このチュートリアルに従うには、次のものが必要です。

  • sudo非rootユーザーとファイアウォールが有効になっている1台のUbuntu22.04サーバー。これを設定するには、Ubuntu22.04を使用したサーバーの初期設定のチュートリアルに従います。これをWireGuardサーバーと呼びます このガイド全体。
  • WireGuardサーバーへの接続に使用するクライアントマシンが必要です。このチュートリアルでは、このマシンを WireGuard Peerと呼びます。 。このチュートリアルでは、ローカルマシンをWireGuard Peerとして使用することをお勧めしますが、必要に応じて、リモートサーバーまたは携帯電話をクライアントとして使用できます。リモートシステムを使用している場合は、このチュートリアルの後半にあるすべてのオプションのセクションに必ず従ってください。そうしないと、システムからロックアウトされる可能性があります。
  • IPv6でWireGuardを使用するには、サーバーがそのタイプのトラフィックをサポートするように構成されていることも確認する必要があります。 WireGuardでIPv6サポートを有効にし、DigitalOceanドロップレットを使用している場合は、このドキュメントページ「ドロップレットでIPv6を有効にする方法」を参照してください。ドロップレットを作成するとき、または後でそのページの手順を使用して、IPv6サポートを追加できます。
ステップ1—WireGuardのインストールとキーペアの生成

このチュートリアルの最初のステップは、サーバーにWireGuardをインストールすることです。まず、WireGuard Serverのパッケージインデックスを更新し、次のコマンドを使用してWireGuardをインストールします。 sudoを初めて使用する場合は、sudoユーザーのパスワードの入力を求められることがあります。 このセッションの内容:

  1. sudo apt update
  2. sudo apt install wireguard

WireGuardがインストールされたので、次のステップはサーバーの秘密鍵と公開鍵のペアを生成することです。組み込みのwg genkeyを使用します およびwg pubkey コマンドを使用してキーを作成し、秘密キーをWireGuardの構成ファイルに追加します。

また、chmodを使用して作成したキーの権限も変更する必要があります。 デフォルトでは、ファイルはサーバー上のすべてのユーザーが読み取ることができるため、コマンド。

WireGuardの秘密鍵を作成し、次のコマンドを使用してそのアクセス許可を変更します。

  1. wg genkey | sudo tee /etc/wireguard/private.key
  2. sudo chmod go= /etc/wireguard/private.key

sudo chmod go=... コマンドは、rootユーザー以外のユーザーおよびグループのファイルに対するすべてのアクセス許可を削除して、rootユーザーのみが秘密鍵にアクセスできるようにします。

base64の1行を受け取る必要があります 秘密鍵であるエンコードされた出力。出力のコピーも/etc/wireguard/private.keyに保存されます teeによる将来の参照用のファイル コマンドの一部。このセクションの後半でWireGuardの構成ファイルに追加する必要があるため、出力される秘密鍵を注意深くメモしてください。

次のステップは、秘密鍵から派生した対応する公開鍵を作成することです。次のコマンドを使用して、公開鍵ファイルを作成します。

  1. sudo cat /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key

このコマンドは、|を使用してチェーン化された3つの個別のコマンドで構成されています (パイプ)演算子:

  • sudo cat /etc/wireguard/private.key :このコマンドは秘密鍵ファイルを読み取り、それを標準出力ストリームに出力します。
  • wg pubkey :2番目のコマンドは、最初のコマンドからの出力を標準入力として受け取り、それを処理して公開鍵を生成します。
  • sudo tee /etc/wireguard/public.key :最後のコマンドは、公開鍵生成コマンドの出力を取得し、それを/etc/wireguard/public.keyという名前のファイルにリダイレクトします。 。

コマンドを実行すると、base64の1行が再び表示されます。 エンコードされた出力。これは、WireGuardサーバーの公開鍵です。サーバーに接続するすべてのピアに公開鍵を配布する必要があるため、参照用にどこかにコピーしてください。

ステップ2—IPv4およびIPv6アドレスの選択

前のセクションでは、WireGuardをインストールし、サーバーとの間のトラフィックを暗号化するために使用されるキーペアを生成しました。このセクションでは、サーバーの構成ファイルを作成し、サーバーの再起動時に自動的に起動するようにWireGuardを設定します。また、WireGuardサーバーおよびピアで使用するプライベートIPv4およびIPv6アドレスを定義します。

IPv4アドレスとIPv6アドレスの両方を使用する場合は、これらのセクションの両方に従ってください。それ以外の場合は、VPNのネットワークニーズに適したセクションの手順に従ってください。

ステップ2(a)—IPv4範囲の選択

WireGuardサーバーをIPv4ピアで使用している場合、サーバーには、クライアントとそのトンネルインターフェイスに使用する一連のプライベートIPv4アドレスが必要です。次の予約済みアドレスブロックから任意の範囲のIPアドレスを選択できます(これらのブロックの割り当て方法について詳しく知りたい場合は、RFC 1918仕様を参照してください):

  • 10.0.0.0 10.255.255.255へ (10/8プレフィックス)
  • 172.16.0.0 172.31.255.255へ (172.16 / 12プレフィックス)
  • 192.168.0.0 192.168.255.255へ (192.168 / 16プレフィックス)

このチュートリアルでは、10.8.0.0/24を使用します 予約済みIPの最初の範囲からのIPアドレスのブロックとして。この範囲では、最大255の異なるピア接続が可能であり、通常、他のプライベートIP範囲と重複または競合するアドレスを持つべきではありません。この範囲の例がネットワークと互換性がない場合は、ネットワーク構成で機能するアドレスの範囲を自由に選択してください。

WireGuard Serverは、その範囲の単一のIPアドレスをプライベートトンネルIPv4アドレスに使用します。 10.8.0.1/24を使用します ここにありますが、10.8.0.1の範囲内の任意のアドレス 10.8.0.255へ に使える。 10.8.0.1/24とは異なるものを使用する場合は、選択したIPアドレスをメモしてください。 。このIPv4アドレスを、ステップ3 —WireGuardサーバー構成の作成で定義した構成ファイルに追加します。

ステップ2(b)—IPv6範囲の選択

IPv6でWireGuardを使用している場合は、RFC 4193のアルゴリズムに基づいて、一意のローカルIPv6ユニキャストアドレスプレフィックスを生成する必要があります。WireGuardで使用するアドレスは、仮想トンネルインターフェイスに関連付けられます。予約済みのfd00::/8内にランダムで一意のIPv6プレフィックスを生成するには、いくつかの手順を完了する必要があります。 プライベートIPv6アドレスのブロック。

RFCによると、一意のIPv6プレフィックスを取得するための推奨される方法は、時刻を、シリアル番号やデバイスIDなどのシステムからの一意の識別値と組み合わせることです。次に、これらの値はハッシュされて切り捨てられ、予約されたプライベートfd00::/8内で一意のアドレスとして使用できるビットのセットになります。 IPのブロック。

WireGuardサーバーのIPv6範囲の生成を開始するには、dateを使用して64ビットのタイムスタンプを収集します 次のコマンドを使用したユーティリティ:

  1. date +%s%N

次のような秒数(%s)を受け取ります。 date コマンド)、およびナノ秒(%N )1970-01-01 00:00:00 UTCを組み合わせてから:

Output1650301699497770167

このセクションの後半で使用するために、値をどこかに記録します。次に、machine-idをコピーします /var/lib/dbus/machine-idからのサーバーの値 ファイル。この識別子はシステムに固有であり、サーバーが存在する限り変更しないでください。

  1. cat /var/lib/dbus/machine-id

次のような出力が表示されます:

/var/lib/dbus/machine-id610cef4946ed46da8f71dba9d66c67fb

次に、タイムスタンプをmachine-idと組み合わせる必要があります そして、SHA-1アルゴリズムを使用して結果の値をハッシュします。コマンドは次の形式を使用します:

printf <timestamp><machine-id> | sha1sum

タイムスタンプとマシンIDの値を代入してコマンドを実行します:

  1. printf 1650301699497770167610cef4946ed46da8f71dba9d66c67fb | sha1sum

次のようなハッシュ値を受け取ります:

Output442adea1488d96388dae9ab816045b24609a6c18  -

sha1sumの出力に注意してください コマンドは16進数であるため、出力では2文字を使用して1バイトのデータを表します。例:4f および26 例の出力では、ハッシュされたデータの最初の2バイトです。

RFCのアルゴリズムは、ハッシュ出力の最下位(末尾)の40ビットまたは5バイトのみを必要とします。 cutを使用する ハッシュから最後の5つの16進エンコードされたバイトを出力するコマンド:

  1. printf 442adea1488d96388dae9ab816045b24609a6c18 | cut -c 31-

-c 引数はcutに指示します 指定された文字セットのみを選択するコマンド。 31- 引数はcutに指示します 31番目の位置から入力行の終わりまでのすべての文字を印刷します。

次のような出力を受け取るはずです:

Output24609a6c18

この出力例では、バイトのセットは次のとおりです。24 60 9a 6c 18

これで、生成した5バイトをfdで追加することにより、独自のIPv6ネットワークプレフィックスを作成できます。 プレフィックス、すべての 2を区切る :のバイト 読みやすさのためのコロン。一意のプレフィックス内の各サブネットは合計18,446,744,073,709,551,616の可能なIPv6アドレスを保持できるため、サブネットを標準サイズの/64に制限できます。 簡単にするために。

/64で以前に生成されたバイトを使用する 結果のプレフィックスのサブネットサイズは次のようになります:

Unique Local IPv6 Address Prefixfd24:609a:6c18::/64

このfd24:609a:6c18::/64 rangeは、サーバーとピアのWireGuardトンネルインターフェースに個々のIPアドレスを割り当てるために使用するものです。サーバーにIPを割り当てるには、1を追加します 最後の::の後 文字。結果のアドレスはfd24:609a:6c18::1/64 。ピアは範囲内の任意のIPを使用できますが、通常、ピアを追加するたびに値が1ずつ増加します。 fd24:609a:6c18::2/64 。 IPをメモし、このチュートリアルの次のセクションでWireGuardサーバーの構成に進みます。

ステップ3—WireGuardサーバー構成の作成

WireGuard Serverの構成を作成する前に、次の情報が必要になります。

  1. 手順1—WireGuardのインストールとキーペアの生成で使用できる秘密キーがあることを確認してください。

  2. IPv4でWireGuardを使用している場合は、ステップ2(a)でサーバーに選択したIPアドレスが必要になります— IPv4範囲の選択(この例では10.8.0.1/24

  3. IPv6でWireGuardを使用している場合は、手順2(b)—IPv6範囲の選択で生成したサーバーのIPアドレスが必要になります。この例では、IPはfd24:609a:6c18::1/64

必要な秘密鍵とIPアドレスを取得したら、nanoを使用して新しい構成ファイルを作成します または、次のコマンドを実行して、好みのエディタを作成します。

  1. sudo nano /etc/wireguard/wg0.conf

強調表示されたbase64_encoded_private_key_goes_hereの代わりに秘密鍵を使用して、ファイルに次の行を追加します 値、およびAddressのIPアドレス ライン。 ListenPortを変更することもできます WireGuardを別のポートで使用できるようにする場合は、次の行に入力してください:

/etc/wireguard/wg0.conf[Interface]
PrivateKey = base64_encoded_private_key_goes_here
Address = 10.8.0.1/24, fd24:609a:6c18::1/64
ListenPort = 51820
SaveConfig = true

SaveConfig lineは、WireGuardインターフェースがシャットダウンされたときに、変更が構成ファイルに保存されることを保証します。

/etc/wireguard/wg0.confを保存して閉じます ファイル。 nanoを使用している場合 、CTRL+Xでこれを行うことができます 、次にY およびENTER 確認するために。これで、WireGuardVPNサーバーの使用方法に応じて構築できる初期サーバー構成ができました。

ステップ4—WireGuardサーバーのネットワーク構成の調整

サーバーのみのサービスにアクセスするためにWireGuardを使用してピアをWireGuardサーバーに接続している場合 、このセクションを完了する必要はありません。 WireGuard PeerのインターネットトラフィックをWireGuardサーバー経由でルーティングする場合は、チュートリアルのこのセクションに従ってIP転送を構成する必要があります。

転送を構成するには、/etc/sysctl.confを開きます nanoを使用したファイル またはお好みのエディター:

  1. sudo nano /etc/sysctl.conf

WireGuardでIPv4を使用している場合は、ファイルの最後に次の行を追加します。

/etc/sysctl.conf
net.ipv4.ip_forward=1

WireGuardでIPv6を使用している場合は、ファイルの最後に次の行を追加します。

/etc/sysctl.conf
net.ipv6.conf.all.forwarding=1

IPv4とIPv6の両方を使用している場合は、両方の回線が含まれていることを確認してください。終了したら、ファイルを保存して閉じます。

ファイルを読み取り、現在のターミナルセッションの新しい値をロードするには、次のコマンドを実行します。

  1. sudo sysctl -p
Outputnet.ipv6.conf.all.forwarding = 1
net.ipv4.ip_forward = 1

これで、WireGuard Serverは、仮想VPNイーサネットデバイスからサーバー上の他のデバイスに、そしてそこからパブリックインターネットに着信トラフィックを転送できるようになります。この構成を使用すると、WireGuard PeerからサーバーのIPアドレスを介してすべてのWebトラフィックをルーティングでき、クライアントのパブリックIPアドレスが効果的に非表示になります。

ただし、トラフィックをサーバー経由で正しくルーティングする前に、いくつかのファイアウォールルールを構成する必要があります。これらのルールにより、WireGuardサーバーとピアとの間のトラフィックが適切に流れるようになります。

ステップ5—WireGuardサーバーのファイアウォールの構成

このセクションでは、WireGuard Serverの構成を編集して、サーバーとクライアントとの間のトラフィックが正しくルーティングされるようにするファイアウォールルールを追加します。前のセクションと同様に、VPNに制限されているリソースにアクセスするためのマシン間接続にWireGuard VPNのみを使用している場合は、この手順をスキップしてください。

WireGuard VPNトラフィックがサーバーのファイアウォールを通過できるようにするには、マスカレードを有効にする必要があります。これは、クライアント接続を正しくルーティングするためのオンザフライの動的ネットワークアドレス変換(NAT)を提供するiptablesの概念です。

まず、ip routeを使用して、WireGuardサーバーのパブリックネットワークインターフェイスを見つけます。 サブコマンド:

  1. ip route list default

パブリックインターフェイスは、このコマンドの出力内にある「dev」という単語に続く文字列です。たとえば、この結果はeth0という名前のインターフェースを示しています 、以下で強調表示されています:

Outputdefault via 203.0.113.1 dev eth0 proto static

デバイスの名前をiptablesに追加するので、メモしてください 次のステップのルール。

WireGuardサーバーにファイアウォールルールを追加するには、/etc/wireguard/wg0.confを開きます nanoのファイル またはお好みのエディタをもう一度。

  1. sudo nano /etc/wireguard/wg0.conf

SaveConfig = trueの後のファイルの下部 行、次の行を貼り付けます:

/etc/wireguard/wg0.conf. . .
PostUp = ufw route allow in on wg0 out on eth0
PostUp = iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
PostUp = ip6tables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
PreDown = ufw route delete allow in on wg0 out on eth0
PreDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
PreDown = ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

PostUp WireGuardサーバーが仮想VPNトンネルを開始すると回線が実行されます。この例では、3つのufwを追加します およびiptables ルール:

  • ufw route allow in on wg0 out on eth0 -このルールは、wg0で着信するIPv4およびIPv6トラフィックの転送を許可します eth0へのVPNインターフェース サーバー上のネットワークインターフェース。 net.ipv4.ip_forwardと連携して機能します およびnet.ipv6.conf.all.forwarding 前のセクションで構成したsysctl値。
  • iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE -このルールはマスカレードを設定し、wg0に着信するIPv4トラフィックを書き換えます。 WireGuardサーバーのパブリックIPv4アドレスから直接発信されているように見せるためのVPNインターフェース。
  • ip6tables -t nat -I POSTROUTING -o eth0 -j MASQUERADE -このルールはマスカレードを設定し、wg0に着信するIPv6トラフィックを書き換えます。 WireGuardサーバーのパブリックIPv6アドレスから直接発信されているように見せるためのVPNインターフェース。

PreDown ルールは、WireGuardサーバーが仮想VPNトンネルを停止したときに実行されます。これらのルールは、PostUpの逆です。 ルール、およびVPNが停止したときにVPNインターフェースの転送およびマスカレードルールを元に戻す機能。

どちらの場合も、VPNに適したIPv4およびIPv6ルールを含めるか除外するように構成を編集します。たとえば、IPv4のみを使用している場合は、ip6tablesの行を除外できます。 コマンド。

逆に、IPv6のみを使用している場合は、構成を編集してip6tablesのみを含めます。 コマンド。 ufw IPv4ネットワークとIPv6ネットワークの任意の組み合わせに対して回線が存在する必要があります。終了したら、ファイルを保存して閉じます。

WireGuardサーバーでファイアウォールを構成する最後の部分は、WireGuardUDPポート自体との間のトラフィックを許可することです。サーバーの/etc/wireguard/wg0.confのポートを変更しなかった場合 ファイルの場合、開くポートは51820です。 。設定を編集するときに別のポートを選択した場合は、必ず次のUFWコマンドでそのポートに置き換えてください。

前提条件のチュートリアルに従うときにSSHポートを開くのを忘れた場合は、ここにも追加してください:

  1. sudo ufw allow 51820/udp
  2. sudo ufw allow OpenSSH

:別のファイアウォールを使用している場合、またはUFW構成をカスタマイズしている場合は、ファイアウォールルールを追加する必要がある場合があります。たとえば、VPN接続を介してすべてのネットワークトラフィックをトンネリングする場合は、ポートが53であることを確認する必要があります。 DNSリクエスト、および80などのポートに対してトラフィックが許可されます および443 それぞれHTTPおよびHTTPSトラフィック用。 VPNを介して使用している他のプロトコルがある場合は、それらのルールも追加する必要があります。

これらのルールを追加したら、UFWを無効にしてから再度有効にして再起動し、変更したすべてのファイルから変更をロードします。

  1. sudo ufw disable
  2. sudo ufw enable

ufw statusを実行すると、ルールが適用されていることを確認できます。 指図。実行すると、次のような出力が表示されます。

  1. sudo ufw status
OutputStatus: active

To                         Action      From
--                         ------      ----
51280/udp                  ALLOW       Anywhere                  
22/tcp                     ALLOW       Anywhere                  
51280/udp (v6)             ALLOW       Anywhere (v6)             
22/tcp (v6)                ALLOW       Anywhere (v6)

これで、WireGuardサーバーは、ピアの転送やマスカレードなど、VPNのトラフィックを正しく処理するように構成されました。ファイアウォールルールを設定すると、WireGuardサービス自体を開始してピア接続をリッスンできます。

ステップ6—WireGuardサーバーの起動

WireGuardは、systemdとして実行するように構成できます 組み込みのwg-quickを使用したサービス 脚本。 wgを手動で使用することもできますが VPNを使用するたびにトンネルを作成するコマンド。これは手動のプロセスであり、繰り返してエラーが発生しやすくなります。代わりに、systemctlを使用できます wg-quickを使用してトンネルを管理する スクリプト。

systemdを使用する サービスとは、サーバーが実行されている限りいつでもVPNに接続できるように、起動時に起動するようにWireGuardを構成できることを意味します。これを行うには、wg-quickを有効にします wg0のサービス systemctlに追加して定義したトンネル :

  1. sudo systemctl enable [email protected]

コマンドがトンネルの名前を指定していることに注意してくださいwg0 サービス名の一部としてのデバイス名。この名前は、/etc/wireguard/wg0.conf 構成ファイル。この命名方法は、サーバーを使用して必要な数の個別のVPNトンネルを作成できることを意味します。

たとえば、トンネルデバイスとprodの名前を設定できます。 その構成ファイルは/etc/wireguard/prod.confになります 。各トンネル構成には、異なるIPv4、IPv6、およびクライアントファイアウォール設定を含めることができます。このようにして、それぞれが独自のIPアドレスとルーティングルールを持つ複数の異なるピア接続をサポートできます。

今すぐサービスを開始します:

  1. sudo systemctl start [email protected]

次のコマンドを使用して、WireGuardサービスがアクティブであることを再確認します。 active (running)が表示されます 出力:

  1. sudo systemctl status [email protected]
Output● [email protected] - WireGuard via wg-quick(8) for wg0
     Loaded: loaded (/lib/systemd/system/[email protected]; enabled; vendor preset: enabled)
     Active: active (exited) since Mon 2022-04-18 17:22:13 UTC; 2s ago
       Docs: man:wg-quick(8)
             man:wg(8)
             https://www.wireguard.com/
             https://www.wireguard.com/quickstart/
             https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8
             https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8
    Process: 98834 ExecStart=/usr/bin/wg-quick up wg0 (code=exited, status=0/SUCCESS)
   Main PID: 98834 (code=exited, status=0/SUCCESS)
        CPU: 193ms

Apr 18 17:22:13 thats-my-jam wg-quick[98834]: [#] wg setconf wg0 /dev/fd/63
Apr 18 17:22:13 thats-my-jam wg-quick[98834]: [#] ip -4 address add 10.8.0.1/24 dev wg0
Apr 18 17:22:13 thats-my-jam wg-quick[98834]: [#] ip -6 address add fd24:609a:6c18::1/64 dev wg0
Apr 18 17:22:13 thats-my-jam wg-quick[98834]: [#] ip link set mtu 1420 up dev wg0
Apr 18 17:22:13 thats-my-jam wg-quick[98834]: [#] ufw route allow in on wg0 out on ens3
Apr 18 17:22:13 thats-my-jam wg-quick[98890]: Rule added
Apr 18 17:22:13 thats-my-jam wg-quick[98890]: Rule added (v6)
Apr 18 17:22:13 thats-my-jam wg-quick[98834]: [#] iptables -t nat -I POSTROUTING -o ens3 -j MASQUERADE
Apr 18 17:22:13 thats-my-jam wg-quick[98834]: [#] ip6tables -t nat -I POSTROUTING -o ens3 -j MASQUERADE
Apr 18 17:22:13 thats-my-jam systemd[1]: Finished WireGuard via wg-quick(8) for wg0.

出力にはipが表示されます 仮想wg0を作成するために使用されるコマンド デバイスに、構成ファイルに追加したIPv4アドレスとIPv6アドレスを割り当てます。これらのルールを使用して、トンネルのトラブルシューティングを行うか、wgを使用できます。 VPNインターフェースを手動で構成したい場合は、コマンド自体を使用してください。

サーバーを構成して実行したら、次のステップは、クライアントマシンをWireGuard Peerとして構成し、WireGuardサーバーに接続することです。

ステップ7—WireGuardピアの設定

WireGuardピアの設定は、WireGuardサーバーの設定に似ています。クライアントソフトウェアをインストールしたら、公開鍵と秘密鍵のペアを生成し、ピアの1つまたは複数のIPアドレスを決定し、ピアの構成ファイルを定義してから、wg-quick スクリプト。

次の手順を使用してキーペアと構成を生成することにより、VPNに必要な数のピアを追加できます。 VPNに複数のピアを追加する場合は、衝突を防ぐために、必ずプライベートIPアドレスを追跡してください。

WireGuard Peerを構成するには、次のaptを使用してWireGuardパッケージがインストールされていることを確認してください コマンド。 WireGuardピアの実行:

  1. sudo apt update
  2. sudo apt install wireguard

WireGuardピアのキーペアの作成

次に、サーバーで使用したのと同じ手順を使用して、ピアでキーペアを生成する必要があります。ピアとして機能するローカルマシンまたはリモートサーバーから、次のコマンドを使用して続行し、ピアの秘密鍵を作成します。

  1. wg genkey | sudo tee /etc/wireguard/private.key
  2. sudo chmod go= /etc/wireguard/private.key

ここでも、1行のbase64を受け取ります 秘密鍵であるエンコードされた出力。出力のコピーも/etc/wireguard/private.keyに保存されます 。このセクションの後半でWireGuardの構成ファイルに追加する必要があるため、出力される秘密鍵を注意深くメモしてください。

次に、次のコマンドを使用して公開鍵ファイルを作成します。

  1. sudo cat /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key

再びbase64の1行が届きます エンコードされた出力。これは、WireGuardPeerの公開鍵です。暗号化された接続を確立するには、公開鍵をWireGuardサーバーに配布する必要があるため、参照用にどこかにコピーしてください。

WireGuardピアの構成ファイルの作成

キーペアができたので、WireGuardサーバーへの接続を確立するために必要なすべての情報を含むピアの構成ファイルを作成できます。

構成ファイルにはいくつかの情報が必要です。

  • base64 ピアで生成したエンコードされた秘密鍵。

  • WireGuardサーバーで定義したIPv4およびIPv6アドレス範囲。

  • base64 WireGuardサーバーからのエンコードされた公開鍵。

  • WireGuardサーバーのパブリックIPアドレスとポート番号。通常、これはIPv4アドレスになりますが、サーバーにIPv6アドレスがあり、クライアントマシンにインターネットへのIPv6接続がある場合は、IPv4の代わりにこれを使用できます。

このすべての情報が手元にある状態で、新しい/etc/wireguard/wg0.confを開きます nanoを使用してWireGuardPeerマシン上のファイル またはお好みのエディター:

  1. sudo nano /etc/wireguard/wg0.conf

次の行をファイルに追加し、必要に応じて強調表示されたセクションにさまざまなデータを代入します。

/etc/wireguard/wg0.conf[Interface]
PrivateKey = base64_encoded_peer_private_key_goes_here
Address = 10.8.0.2/24
Address = fd24:609a:6c18::2/64

[Peer]
PublicKey = U9uE2kb/nrrzsEU58GD3pKFU3TLYDMCbetIsnV8eeFE=
AllowedIPs = 10.8.0.0/24, fd24:609a:6c18::/64
Endpoint = 203.0.113.1:51820

最初のAddress 行は10.8.0.0/24のIPv4アドレスを使用します 以前に選択したサブネット。このIPアドレスは、サーバーのIPと異なる限り、サブネット内の任意のアドレスにすることができます。ピアを追加するたびにアドレスを1ずつ増やすのが、一般的にIPを割り当てる最も簡単な方法です。

同様に、2番目のAddress lineは、前に生成したサブネットのIPv6アドレスを使用し、サーバーのアドレスを1つ増やします。繰り返しになりますが、別のアドレスを使用する場合は、範囲内のすべてのIPが有効です。

ファイルの他の注目すべき部分は、最後のAllowedIPsです。 ライン。 These two IPv4 and IPv6 ranges instruct the peer to only send traffic over the VPN if the destination system has an IP address in either range. Using the AllowedIPs directive, you can restrict the VPN on the peer to only connect to other peers and services on the VPN, or you can configure the setting to tunnel all traffic over the VPN and use the WireGuard Server as a gateway.

If you are only using IPv4, then omit the trailing fd24:609a:6c18::/64 range (including the , comma). Conversely, if you are only using IPv6, then only include the fd24:609a:6c18::/64 prefix and leave out the 10.8.0.0/24 IPv4 range.

In both cases, if you would like to send all your peer’s traffic over the VPN and use the WireGuard Server as a gateway for all traffic, then you can use 0.0.0.0/0 , which represents the entire IPv4 address space, and ::/0 for the entire IPv6 address space.

(Optional) Configuring a Peer to Route All Traffic Over the Tunnel

If you have opted to route all of the peer’s traffic over the tunnel using the 0.0.0.0/0 or ::/0 routes and the peer is a remote system, then you will need to complete the steps in this section. If your peer is a local system then it is best to skip this section.

For remote peers that you access via SSH or some other protocol using a public IP address, you will need to add some extra rules to the peer’s wg0.conf ファイル。 These rules will ensure that you can still connect to the system from outside of the tunnel when it is connected. Otherwise, when the tunnel is established, all traffic that would normally be handled on the public network interface will not be routed correctly to bypass the wg0 tunnel interface, leading to an inaccessible remote system.

First, you’ll need to determine the IP address that the system uses as its default gateway. Run the following ip route コマンド:

  1. ip route list table main default

次のような出力が表示されます:

Outputdefault via 203.0.113.1 dev eth0 proto static

Note the gateway’s highlighted IP address 203.0.113.1 for later use, and device eth0 。 Your device name may be different. If so, substitute it in place of eth0 in the following commands.

Next find the public IP for the system by examining the device with the ip address show コマンド:

  1. ip -brief address show eth0

次のような出力が表示されます:

Outputeth0             UP             203.0.113.5/20 10.20.30.40/16 2604:a880:400:d1::3d3:6001/64 fe80::68d5:beff:feff:974c/64

In this example output, the highlighted 203.0.113.5 IP (without the trailing /20 ) is the public address that is assigned to the eth0 device that you’ll need to add to the WireGuard configuration.

Now open the WireGuard Peer’s /etc/wireguard/wg0.conf nanoのファイル or your preferred editor.

  1. sudo nano /etc/wireguard/wg0.conf

Before the [Peer] line, add the following 4 lines:

PostUp = ip rule add table 200 from 203.0.113.5
PostUp = ip route add table 200 default via 203.0.113.1
PreDown = ip rule delete table 200 from 203.0.113.5
PreDown = ip route delete table 200 default via 203.0.113.1

[Peer]
. . .

These lines will create a custom routing rule, and add a custom route to ensure that public traffic to the system uses the default gateway.

  • PostUp = ip rule add table 200 from 203.0.113.5 - This command creates a rule that checks for any routing entries in the table numbered 200 when the IP matches the system’s public 203.0.113.5 address.
  • PostUp = ip route add table 200 default via 203.0.113.1 - This command ensures that any traffic being processed by the 200 table will use the 203.0.113.1 gateway for routing, instead of the WireGuard interface.

The PreDown lines remove the custom rule and route when the tunnel is shutdown.

Note:The table number 200 is arbitrary when constructing these rules. You can use a value between 2 and 252, or you can use a custom name by adding a label to the /etc/iproute2/rt_tables file and then referring to the name instead of the numeric value.

For more information about how routing tables work in Linux visit the Routing Tables Section of the Guide to IP Layer Network Administration with Linux.

If you are routing all the peer’s traffic over the VPN, ensure that you have configured the correct sysctl and iptables rules on the WireGuard Server in Step 4 — Adjusting the WireGuard Server’s Network Configuration and Step 5 — Configuring the WireGuard Server’s Firewall.

(Optional) Configuring the WireGuard Peer’s DNS Resolvers

If you are using the WireGuard Server as a VPN gateway for all your peer’s traffic, you will need to add a line to the [Interface] section that specifies DNS resolvers. If you do not add this setting, then your DNS requests may not be secured by the VPN, or they might be revealed to your Internet Service Provider or other third parties.

If you are only using WireGuard to access resources on the VPN network or in a peer-to-peer configuration then you can skip this section.

To add DNS resolvers to your peer’s configuration, first determine which DNS servers your WireGuard Server is using. Run the following command on the WireGuard Server , substituting in your ethernet device name in place of eth0 if it is different from this example:

  1. resolvectl dns eth0

You should receive output like the following:

OutputLink 2 (eth0): 67.207.67.2 67.207.67.3 2001:4860:4860::8844 2001:4860:4860::8888

The IP addresses that are output are the DNS resolvers that the server is using. You can choose to use any or all of them, or only IPv4 or IPv6 depending on your needs. Make a note of the resolvers that you will use.

Next you will need to add your chosen resolvers to the WireGuard Peer’s configuration file. Back on the WireGuard Peer , open /etc/wireguard/wg0.conf file using nano or your preferred editor:

  1. sudo nano /etc/wireguard/wg0.conf

Before the [Peer] line, add the following:

DNS = 67.207.67.2 2001:4860:4860::8844

[Peer]
. . .

Again, depending on your preference or requirements for IPv4 and IPv6, you can edit the list according to your needs.

Once you are connected to the VPN in the following step, you can check that you are sending DNS queries over the VPN by using a site like DNS leak test.com.

You can also check that your peer is using the configured resolvers with the resolvectl dns command like you ran on the server. You should receive output like the following, showing the DNS resolvers that you configured for the VPN tunnel:

OutputGlobal: 67.207.67.2 67.207.67.3
. . .

With all of these DNS resolver settings in place, you are now ready to add the peer’s public key to the server, and then start the WireGuard tunnel on the peer.

Step 8 — Adding the Peer’s Public Key to the WireGuard Server

Before connecting the peer to the server, it is important to add the peer’s public key to the WireGuard Server. This step ensures that you will be able to connect to and route traffic over the VPN. Without completing this step the WireGuard server will not allow the peer to send or receive any traffic over the tunnel.

Ensure that you have a copy of the base64 encoded public key for the WireGuard Peer by running:

  1. sudo cat /etc/wireguard/public.key
OutputPeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg=

Now log into the WireGuard server, and run the following command:

  1. sudo wg set wg0 peer PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg= allowed-ips 10.8.0.2,fd24:609a:6c18::2

Note that the allowed-ips portion of the command takes a comma separated list of IPv4 and IPv6 addresses. You can specify individual IPs if you would like to restrict the IP address that a peer can assign itself, or a range like in the example if your peers can use any IP address in the VPN range. Also note that no two peers can have the same allowed-ips setting.

If you would like to update the allowed-ips for an existing peer, you can run the same command again, but change the IP addresses. Multiple IP addresses are supported. For example, to change the WireGuard Peer that you just added to add an IP like 10.8.0.100 to the existing 10.8.0.2 and fd24:609a:6c18::2 IPs, you would run the following:

  1. sudo wg set wg0 peer PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg= allowed-ips 10.8.0.2,10.8.0.100,fd24:609a:6c18::2

Once you have run the command to add the peer, check the status of the tunnel on the server using the wg コマンド:

  1. sudo wg
Outputinterface: wg0
 public key: U9uE2kb/nrrzsEU58GD3pKFU3TLYDMCbetIsnV8eeFE=
 private key: (hidden)
 listening port: 51820

peer: PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg=
 allowed ips: 10.8.0.2/32, fd24:609a:6c18::/128

Notice how the peer line shows the WireGuard Peer’s public key, and the IP addresses, or ranges of addresses that it is allowed to use to assign itself an IP.

Now that you have defined the peer’s connection parameters on the server, the next step is to start the tunnel on the peer.

Step 9 — Connecting the WireGuard Peer to the Tunnel

Now that your server and peer are both configured to support your choice of IPv4, IPv6, packet forwarding, and DNS resolution, it is time to connect the peer to the VPN tunnel.

Since you may only want the VPN to be on for certain use cases, we’ll use the wg-quick command to establish the connection manually. If you would like to automate starting the tunnel like you did on the server, follow those steps in Step 6 — Starting the WireGuard Server section instead of using the wq-quick コマンド。

In case you are routing all traffic through the VPN and have set up DNS forwarding, you’ll need to install the resolvconf utility on the WireGuard Peer before you start the tunnel. Run the following command to set this up:

  1. sudo apt install resolvconf

To start the tunnel, run the following on the WireGuard Peer:

  1. sudo wg-quick up wg0

次のような出力が表示されます:

Output[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.8.0.2/24 dev wg0
[#] ip -6 address add fd24:609a:6c18::2/64 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] resolvconf -a tun.wg0 -m 0 -x

Notice the highlighted IPv4 and IPv6 addresses that you assigned to the peer.

If you set the AllowedIPs on the peer to 0.0.0.0/0 and ::/0 (or to use ranges other than the ones that you chose for the VPN), then your output will resemble the following:

Output[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.8.0.2/24 dev wg0
[#] ip -6 address add fd24:609a:6c18::2/64 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] resolvconf -a tun.wg0 -m 0 -x
[#] wg set wg0 fwmark 51820
[#] ip -6 route add ::/0 dev wg0 table 51820
[#] ip -6 rule add not fwmark 51820 table 51820
[#] ip -6 rule add table main suppress_prefixlength 0
[#] ip6tables-restore -n
[#] ip -4 route add 0.0.0.0/0 dev wg0 table 51820
[#] ip -4 rule add not fwmark 51820 table 51820
[#] ip -4 rule add table main suppress_prefixlength 0
[#] sysctl -q net.ipv4.conf.all.src_valid_mark=1
[#] iptables-restore -n

In this example, notice the highlighted routes that the command added, which correspond to the AllowedIPs in the peer configuration.

You can check the status of the tunnel on the peer using the wg コマンド:

  1. sudo wg
Outputinterface: wg0
 public key: PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg=
 private key: (hidden)
 listening port: 49338
 fwmark: 0xca6c

peer: U9uE2kb/nrrzsEU58GD3pKFU3TLYDMCbetIsnV8eeFE=
 endpoint: 203.0.113.1:51820
 allowed ips: 10.8.0.0/24, fd24:609a:6c18::/64
 latest handshake: 1 second ago
 transfer: 6.50 KiB received, 15.41 KiB sent

You can also check the status on the server again, and you will receive similar output.

Verify that your peer is using the VPN by using the ip route and ip -6 route コマンド。 If you are using the VPN as a gateway for all your Internet traffic, check which interface will be used for traffic destined to CloudFlare’s 1.1.1.1 and 2606:4700:4700::1111 DNS resolvers.

If you are only using WireGuard to access resources on the VPN, substitute a valid IPv4 or IPv6 address like the gateway itself into these commands. For example 10.8.0.1 or fd24:609a:6c18::1

  1. ip route get 1.1.1.1
Output1.1.1.1 dev wg0 table 51820 src 10.8.0.2 uid 1000
   cache

Notice the wg0 device is used and the IPv4 address 10.8.0.2 that you assigned to the peer. Likewise, if you are using IPv6, run the following:

  1. ip -6 route get 2606:4700:4700::1111
Output2606:4700:4700::1111 from :: dev wg0 table 51820 src fd24:609a:6c18::2 metric 1024 pref medium

Again note the wg0 interface, and the IPv6 address fd24:609a:6c18::2 that you assigned to the peer.

If your peer has a browser installed, you can also visit ipleak.net and ipv6-test.com to confirm that your peer is routing its traffic over the VPN.

Once you are ready to disconnect from the VPN on the peer, use the wg-quick コマンド:

  1. sudo wg-quick down wg0

You will receive output like the following indicating that the VPN tunnel is shut down:

Output[#] ip link delete dev wg0
[#] resolvconf -d tun.wg0 -f

If you set the AllowedIPs on the peer to 0.0.0.0/0 and ::/0 (or to use ranges other than the ones that you chose for the VPN), then your output will resemble the following:

Output[#] ip rule delete table 200 from 203.0.113.5
[#] ip route delete table 200 default via 203.0.113.1
[#] ip -4 rule delete table 51820
[#] ip -4 rule delete table main suppress_prefixlength 0
[#] ip -6 rule delete table 51820
[#] ip -6 rule delete table main suppress_prefixlength 0
[#] ip link delete dev wg0
[#] resolvconf -d tun.wg0 -f
[#] iptables-restore -n
[#] ip6tables-restore -n

To reconnect to the VPN, run the wg-quick up wg0 command again on the peer. If you would like to completely remove a peer’s configuration from the WireGuard Server, you can run the following command, being sure to substitute the correct public key for the peer that you want to remove:

  1. sudo wg set wg0 peer PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg= remove

Typically you will only need to remove a peer configuration if the peer no longer exists, or if its encryption keys are compromised or changed. Otherwise it is better to leave the configuration in place so that the peer can reconnect to the VPN without requiring that you add its key and allowed-ips each time.

結論

In this tutorial you installed the WireGuard package and tools on both the server and client Ubuntu 22.04 systems. You set up firewall rules for WireGuard, and configured kernel settings to allow packet forwarding using the sysctl command on the server. You learned how to generate private and public WireGuard encryption keys, and how to configure the server and peer (or peers) to connect to each other.

If your network uses IPv6, you also learned how to generate a unique local address range to use with peer connections. Finally, you learned how to limit which traffic should go over the VPN by restricting the network prefixes that the peer can use, as well as how to use the WireGuard Server as a VPN gateway to handle all Internet traffic for peers.

If you would like to learn more about WireGuard, including how to configure more advanced tunnels, or use WireGuard with containers, visit the official WireGuard documentation.


OpenVPN
  1. Ubuntu /DebianLAMPサーバーをセットアップする方法

  2. UbuntuでWireGuardVPNを設定する方法(ステップバイステップガイド)

  3. Ubuntu22.04で認証局を設定および構成する方法

  1. Ubuntu20.04でWireGuardVPNを設定する方法

  2. Ubuntu20.04でリバースプロキシとしてNginxを設定する方法

  3. Ubuntu16.04でTeamSpeakサーバーをセットアップする方法

  1. Ubuntu18.04にSquidプロキシサーバーをセットアップしてインストールする方法

  2. Ubuntu20.04でNFSサーバーとクライアントをセットアップする方法

  3. Ubuntu – Ubuntuで静的IPを設定する方法は?