WireGuardによるVPN(AWS: EC2 or Lightsail)

WireGuardによる VPNサーバAWS(EC2 or Lightsail) に構築します。

WireGuardそのものはサーバソフトではなく、ピア・トゥ・ピアで端末間どうしの接続を確立する VPN(Virtual Private Network) ソフトウェアです。接続をリクエストする側とリクエストされる側双方にインストールされるソフトウェアは同一のもので、設定ファイルによりそれぞれがピア(クライアント)またはサーバとしての役割を担います。OpenVPNなど従来のVPNサーバと比較して、よりセキュアかつ軽量であり接続も非常に安定しています。

VPNにおけるサーバとピア(クライアント)の関係は、家庭内におけるホームルータとPCとの関係に相当すると考えると理解しやすいでしょう。

WireGuardによるVPN導入のメリットは次の通りです。

  • サーバのリージョン指定により、日本国内から海外現地ユーザ限定のサイトへのアクセス(各国のメディアサイト提供の番組視聴など)
  • グローバルな固定IPが割当てられていないホームサーバへのアクセス(リバースプロキシまたは iptables による)

参考サイト

WireGuard: fast, modern, secure VPN tunnel

How To Set Up WireGuard on Ubuntu 20.04 | DigitalOcean

Getting Started with WireGuard

Archi Linux WireGuard
https://wiki.archlinux.org/title/WireGuard

GitHub - linuxserver/docker-wireguard

wireguard-tools man page
https://manpages.debian.org/unstable/wireguard-tools/wg.8.en.html


0. 事前準備

事前準備として、サーバ側にAWSによるインスタンス(EC2またはLightsail)を用意して下さい。インスタンスにはSSH接続できるようにしておきます。推奨インスタンスはLightsailによるものです。WireGuardによるVPNサーバとして3.5USDのLinuxインスタンスを選択してもデータ転送量上限が1TBに設定されており、メモリ512MB、ディスク容量も20GBと十分なスペックです。


1. インストール

サーバ側、クライアント側共にインストール手順は同じです。同じ作業をサーバ側とピア(クライアント)側で実行します

$ sudo apt update
$ sudo apt install wireguard

インストール後、WireGuardwg genkeywg pubkeyコマンドからプライベートキーとパブリックキーを作成します。umask コマンドでシェル内で作成されるこれらファイルやディレクトリへのアクセスにオーナのみの制限を設けます。この設定はシェル終了時点でリセットされます。

$ umask 077

プライベートキーの作成

wg genkey コマンドによりプライベートキーを作成し、private.keyとして保存します。

$ wg genkey | sudo tee /etc/wireguard/private.key

パブリックキーの作成

プライベートキーprivate.keyからwg pubkeyコマンドによりパブリックキーを作成し、public.keyとして保存します。

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

2. ローカルIPの定義

WireGuardで使用するVPNネットワークのIP範囲とその種類(IP4, IP6または両方)を定義します。今回はIP4のみを定義し、以下の代表的にローカルIPとして使用されているアドレスの中から10.8.0.0/24を選択します。

  • 10.0.0.0 to 10.255.255.255 (10/8 prefix)
  • 172.16.0.0 to 172.31.255.255 (172.16/12 prefix)
  • 192.168.0.0 to 192.168.255.255 (192.168/16 prefix)

3. サーバ側のIPフォワーディング設定

WireGuardによるネットワークインターフェイスwg0からサーバのネットワークインターフェイスeth0経由でインターネットに繋げるためIPフォワーディングの設定をします。

$ sudo nano /etc/sysctl.conf

以下のセクションをコメントアウトして1に設定します。

sysctl.conf

net.ipv4.ip_forward=1

上記設定を反映させます。

$ sudo sysctl -p
output
net.ipv4.ip_forward = 1

ネットワークインターフェイスの確認は以下コマンドで行って下さい。

$ ip route list default

Output
default via 203.0.113.1 dev eth0 proto static

この場合、インターネットに繋がっているインターフェイスはeth0となります。


4. 設定ファイルの作成

サーバ側、ピア(クライアント)側それぞれに設定ファイルを作成します。設定ファイルの名称から拡張子を除いたwg0がネットワークインターフェイスの名称となります。

サーバ側の設定ファイル

必要な情報として、

  • サーバ側で作成したプライベートキーprivate.keyの内容
  • ピア(クライアント)側で作成したパブリックキーpublic.keyの内容
  • サーバ側に割当てるローカルIP(範囲): 10.8.0.1/24
  • ピア(クライアント)側の受入IP(範囲): 10.8.0.2/32
  • ポートはデフォルトの51820を指定 注) AWSのインスタンスでこのポート(in)は開放して下さい。
$ sudo nano /etc/wireguard/wg0.conf

wg0.conf

[Interface]
Address = 10.8.0.1/24
SaveConfig = true
PostUp = iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
PreDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820
PrivateKey = server_private_key

[Peer]
PublicKey = peer_client_public_key
AllowedIPs = 10.8.0.2/32
  • iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE - iptablesのマスカレーディングによりVPNのインターフェイスwg0eth0と見なされます。
  • SaveConfigにより終了時点の接続ピアの情報などが保存されます。

ピア(クライアント)側の設定ファイル

必要な情報として、

  • ピア(クライアント)側で作成したプライベートキーprivate.keyの内容
  • サーバ側のパブリックキーpublic.keyの内容
  • サーバ側のグローバルIP:ポート: instance_global_IP:51820
  • ピア(クライアント)側に割当てるローカルIP(範囲): 10.8.0.2/24
$ sudo nano /etc/wireguard/wg0.conf

wg0.conf

[Interface]
Address = 10.8.0.2/24
PrivateKey = peer_client_private_key

#optional below
#PostUp = ip rule add table 200 from 192.168.1.xxx
#PostUp = ip route add table 200 default via 192.168.1.1
#PreDown = ip route delete table 200 default via 192.168.1.1
#PreDown = ip rule delete table 200 from 192.168.1.xxx
#DNS = 1.1.1.1

[Peer]
PublicKey = server_public_key
AllowedIPs = 0.0.0.0/0
Endpoint = instance_global_IP:51820
PersistentKeepalive = 25

サーバをインターネットへのゲートウェイとして利用するため、AllowedIPsには0.0.0.0/0を指定。

注)#オプション箇所の詳細については、

How To Set Up WireGuard on Ubuntu 20.04 | DigitalOcean

を参照して下さい。DNSオプションを設定した場合、resolvconfをインストールする必要があります。

$ sudo apt install resolvconf

ピア(クライアント)がNATやファイヤーウォールの背後に配置されている場合、PersistentKeepalive = を指定しないとNATまたはファイヤーウォールから接続が切断されてしまします。詳しくは、

NAT and Firewall Traversal Persistence

By default, WireGuard tries to be as silent as possible when not being used; it is not a chatty protocol. For the most part, it only transmits data when a peer wishes to send packets. When it’s not being asked to send packets, it stops sending packets until it is asked again. In the majority of configurations, this works well. However, when a peer is behind NAT or a firewall, it might wish to be able to receive incoming packets even when it is not sending any packets. Because NAT and stateful firewalls keep track of “connections”, if a peer behind NAT or a firewall wishes to receive incoming packets, he must keep the NAT/firewall mapping valid, by periodically sending keepalive packets. This is called persistent keepalives. When this option is enabled, a keepalive packet is sent to the server endpoint once every interval seconds. A sensible interval that works with a wide variety of firewalls is 25 seconds. Setting it to 0 turns the feature off, which is the default, since most users will not need this, and it makes WireGuard slightly more chatty. This feature may be specified by adding the PersistentKeepalive = field to a peer in the configuration file, or setting persistent-keepalive at the command line. If you don’t need this feature, don’t enable it. But if you’re behind NAT or a firewall and you want to receive incoming connections long after network traffic has gone silent, this option will keep the “connection” open in the eyes of NAT.

ip ruleマニュアル

https://man7.org/linux/man-pages/man8/ip-rule.8.html


5. サーバ起動

初めにサーバ側を起動します。WireGuardのシステムデーモンを有効にしてから起動します。

$ sudo systemctl enable [email protected]
$ sudo systemctl start [email protected]

ステータス確認

$ 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 Wed 2021-08-25 15:24:14 UTC; 5s 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: 3245 ExecStart=/usr/bin/wg-quick up wg0 (code=exited, status=0/SUCCESS)
   Main PID: 3245 (code=exited, status=0/SUCCESS)

Aug 25 15:24:14 wg0 wg-quick[3245]: [#] wg setconf wg0 /dev/fd/63
Aug 25 15:24:14 wg0 wg-quick[3245]: [#] ip -4 address add 10.8.0.1/24 dev wg0
Aug 25 15:24:14 wg0 wg-quick[3245]: [#] ip -6 address add fd0d:86fa:c3bc::1/64 dev wg0
Aug 25 15:24:14 wg0 wg-quick[3245]: [#] ip link set mtu 1420 up dev wg0
Aug 25 15:24:14 wg0 wg-quick[3245]: [#] ufw route allow in on wg0 out on eth0
Aug 25 15:24:14 wg0 wg-quick[3279]: Rule added
Aug 25 15:24:14 wg0 wg-quick[3279]: Rule added (v6)
Aug 25 15:24:14 wg0 wg-quick[3245]: [#] iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
Aug 25 15:24:14 wg0 wg-quick[3245]: [#] ip6tables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
Aug 25 15:24:14 wg0 systemd[1]: Finished WireGuard via wg-quick(8) for wg0.

停止

$ sudo systemctl stop [email protected]

接続状態の確認

どのクライアントと接続しているか、通信量などが確認できます。

$ sudo wg
 
Output
interface: wg0
 public key: U9uE2kb/nrrzsEU58GD3pKFU3TLYDMCbetIsnV8eeFE=
 private key: (hidden)
 listening port: 51820

peer: PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg=
 allowed ips: 10.8.0.2/32, fd0d:86fa:c3bc::/128

6. ピア(クライアント)からサーバへ接続

サーバ側が起動している状態で接続します。

接続

$ 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 fd0d:86fa:c3bc::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

停止

$ sudo wg-quick down wg0

接続状態の確認

$ sudo wg
 
Output
interface: 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, fd0d:86fa:c3bc::/64
 latest handshake: 1 second ago
 transfer: 6.50 KiB received, 15.41 KiB sent

ピア(クライアント)のIPがサーバのパブリックIPに変更したかどうかの確認は、以下のcurlコマンドで行います。

$ curl https://ipinfo.io/ip

オプションで共有キーを作成し、その中身をサーバとクライアントの設定ファイルの [Peer] セクションに追加します。共有キーの作成はサーバ、クライアント側のどちらでも構いません。

$ wg genpsk > presharedkey

/etc/wireguard/wg0.conf

......
......

[Peer]
.....
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
.....

複数のピア(クライアント)を接続するには、新たに接続するピア2でもピア1と同様にWireGuardのインストール、プライベート・パブリックキーの作成、設定ファイルwg0.confにサーバ情報などを記載します。

Peer2 IP Address:10.8.0.3

Peer2: wg0.conf

[Interface]
Address = 10.8.0.3/24
PrivateKey = peer2_client2_private_key

#optional below
#PostUp = ip rule add table 200 from 192.168.1.xxx
#PostUp = ip route add table 200 default via 192.168.1.1
#PreDown = ip rule delete table 200 from 192.168.1.xxx
#PreDown = ip route delete table 200 default via 192.168.1.1
#DNS = 1.1.1.1

[Peer]
PublicKey = server_public_key
AllowedIPs = 0.0.0.0/0
Endpoint = instance_global_IP:51820
PersistentKeepalive = 25

サーバ側の設定ファイルwg0.confに新しく加わるピア2の情報を追加します。

Server: wg0.conf

[Interface]
Address = 10.8.0.1/24
SaveConfig = true
PostUp = iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
PreDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820
PrivateKey = server_private_key

[Peer]
PublicKey = peer_client_public_key
AllowedIPs = 10.8.0.2/32

[Peer]
PublicKey = peer2_client2_public_key
AllowedIPs = 10.8.0.3/32

サーバ側のWireGuardを再起動後、

$ sudo systemctl restart [email protected]

ピア2のWireGuardを起動します。

$ sudo wg-quick up wg0

注) 起動順序はありません。ピアを起動してからサーバを起動しても接続は確立されます。


コマンドラインによるピアの追加・削除

追加するピアのパブリックキーとIPアドレスを指定して以下コマンドを実行。

$ sudo wg set wg0 peer PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg= allowed-ips 10.8.0.2,fd0d:86fa:c3bc::2

削除する場合は、

$ sudo wg set wg0 peer PeURxj4Q75RaVhBKkRTpNsBPiPSGb5oQijgJsTa29hg= remove

WireGuardの使用例

WireGuardのネットワークを利用すると、グローバルIPが割当てられていないホストマシンでも 固定IP(AWSインスタンス) によるサーバ運営(マルチドメイン)が可能です。

:warning:下記ポートフォワードの使用例ではサーバ側のIP Masquerade(wg0.conf:PostUp,PreDown)の設定は省略できます。


方法1:VPNサーバにプロキシーサーバを配置
Nginx のプロキシーサーバを VPN Server に配置し、ドメインごとに Peer2:10.8.0.3 のポート番号を指定します。


方法2:iptablesのポートフォワードルールを適用
VPN Serveriptables によるポートフォワーディングルールを追加します。

$ sudo iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 443 -j DNAT --to-destination 10.8.0.3:443
$ sudo iptables -A FORWARD -p tcp -d 10.8.0.3 --dport 443 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

注) 上記コマンドの下段はファイヤーウォールを設定している場合に必要です。

Peer2にプロキシーサーバを配置。

Wireguard and iptables restrictions for multiple users

ネットワーク通信処理ツールをiptablesからiftablesへ移行した場合、サーバ側の設定ファイルwg0.confPostUp, PreDowniptablesコマンドからnftコマンドへ変更します。

Server: wg0.conf

[Interface]
Address = 10.8.0.1/24
SaveConfig = true
PostUp = nft 'add chain ip nat POSTROUTING {type nat hook postrouting priority srcnat; policy accept;}'
PostUp = nft 'add rule ip nat POSTROUTING oifname "eth0" counter masquerade'
PreDown = nft delete chain ip nat POSTROUTING
ListenPort = 51820
PrivateKey = server_private_key

[Peer]
PublicKey = peer_client_public_key
AllowedIPs = 10.8.0.2/32

[Peer]
PublicKey = peer2_client2_public_key
AllowedIPs = 10.8.0.3/32

nftablesの設定ファイル /etc/nftables.conf には、以下のように予めnatテーブルを記述し、ポートフォワードルール(必要な場合)もここで定義しておきます。

/etc/nftables.conf

#!/usr/sbin/nft -f

flush ruleset

table ip nat {
        chain PREROUTING {
                type nat hook prerouting priority dstnat; policy accept;
                iifname "eth0" tcp dport { 80, 443 } counter dnat to 10.8.0.2
        }
}

wg0.confPostUp,PreDownの記述を省くため、 nftables.conf に事前にルールを記述しても構いません。

/etc/nftables.conf

#!/usr/sbin/nft -f

flush ruleset

table ip nat {
        chain PREROUTING {
                type nat hook prerouting priority dstnat; policy accept;
                iifname "eth0" tcp dport { 80, 443 } counter dnat to 10.8.0.2
        }

        chain POSTROUTING {
                type nat hook postrouting priority srcnat; policy accept;
                oifname "eth0" counter masquerade
        }
}

"eth0" に該当するインターフェースの確認は

$ sudo ip addr

IPv6対応

IPv6にも対応するよう設定ファイルを見直します。

IPフォワーディングの設定

IPv4のIPフォワーディングに加え、IPv6のIPフォワーディングも有効にします。(これはサーバがインターネットからのアクセスを受け入れる場合に必要な処置です。IPv4については既に適用済です。)

/etc/sysctl.conf

net.ipv6.conf.all.forwarding=1

設定を反映させるため以下コマンドを実行します。

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

IPv6ローカルIPアドレスの設定

WireGuardで使用するIPv6ローカルIPアドレスを設定します。IPv6によるローカルIPのフォーマットは、以下のネットワーク標準化カテゴリの中で定義されています。

RFC 4193
https://www.rfc-editor.org/rfc/rfc4193#section-3

3.1. Format

The Local IPv6 addresses are created using a pseudo-randomly
allocated global ID. They have the following format:

  | 7 bits |1|  40 bits   |  16 bits  |          64 bits           |
  +--------+-+------------+-----------+----------------------------+
  | Prefix |L| Global ID  | Subnet ID |        Interface ID        |
  +--------+-+------------+-----------+----------------------------+

Where:

  Prefix            FC00::/7 prefix to identify Local IPv6 unicast
                    addresses.

  L                 Set to 1 if the prefix is locally assigned.
                    Set to 0 may be defined in the future.  See
                    Section 3.2 for additional information.

  Global ID         40-bit global identifier used to create a
                    globally unique prefix.  See Section 3.2 for
                    additional information.

  Subnet ID         16-bit Subnet ID is an identifier of a subnet
                    within the site.

  Interface ID      64-bit Interface ID as defined in [ADDARCH].

上記のユニークローカルアドレスを参照し、 fd00::/8 とランダムな40ビットの組み合わせによりIPアドレスの接頭辞を決定します。ランダムな40ビットの作成方法は任意ですが、ここでは日付とホストマシンIDからその40ビットを作成します。

日付確認

$ date +%s%N
1628101352127592197

マシンID確認

$ cat /var/lib/dbus/machine-id
20086c25853947c7aeee2ca1ea849d7d

日付とマシンIDからランダムな数列を作成
printf <timestamp><machine-id> | sha1sum

$ printf 162810135212759219720086c25853947c7aeee2ca1ea849d7d | sha1sum
4f267c51857d6dc93a0bca107bca2f0d86fac3bc  -

下位5バイト(40ビット)のみ抽出

$ printf 4f267c51857d6dc93a0bca107bca2f0d86fac3bc | cut -c 31-
0d86fac3bc

fd00::/80d 86 fa c3 bc からIPv6アドレスの接頭辞 fd0d:86fa:c3bc::/64 を決定します。

Sever —> wg0.confの設定

WireGuardサーバ側設定ファイルにサーバとクライアントのIPv6アドレスを追加します。

:warning: WireGuardを一度停止してから編集して下さい。

$ sudo systemctl stop [email protected]

/etc/wireguard/wg0.conf

[Interface]
PrivateKey = base64_encoded_private_key_goes_here
Address = 10.8.0.1/24, fd0d:86fa:c3bc::1/64
ListenPort = 51820
SaveConfig = true

[Peer]
PublicKey = peer_client_public_key
AllowedIPs = 10.8.0.2/32, fd0d:86fa:c3bc::2/128

編集後WireGuardを起動します。

$ sudo systemctl start [email protected]

ステータスコマンドでIPv6が追加されていることを確認して下さい。

$ sudo systemctl status [email protected]
...
...
Aug 25 15:24:14 wg0 wg-quick[3245]: [#] ip -6 address add fd0d:86fa:c3bc::1/64 dev wg0
...
...

Peer —> wg0.confの設定

WireGuardピア(クライアント)側設定ファイルにピア(クライアント)のIPv6アドレスを追加します。

:warning: WireGuardを一度停止してから編集して下さい。

/etc/wireguard/wg0.conf

[Interface]
Address = 10.8.0.2/24, fd0d:86fa:c3bc::2/64
PrivateKey = peer_client_private_key
DNS = 1.1.1.1

[Peer]
PublicKey = server_public_key
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = instance_global_IP:51820
PersistentKeepalive = 25

nftablesの設定

以下設定ファイルにIPv6のテーブルファミリーを追加します。

/etc/nftables.conf

#!/usr/sbin/nft -f

flush ruleset

table ip nat {
        chain PREROUTING {
                type nat hook prerouting priority dstnat; policy accept;
                iifname "eth0" tcp dport { 80, 443 } counter dnat to 10.8.0.2
        }

        chain POSTROUTING {
                type nat hook postrouting priority srcnat; policy accept;
                oifname "eth0" counter masquerade
        }
}

table ip6 nat {
        chain PREROUTING {
                type nat hook prerouting priority dstnat; policy accept;
                iifname "eth0" tcp dport { 80, 443 } counter dnat to fd0d:86fa:c3bc::2
        }

        chain POSTROUTING {
                type nat hook postrouting priority srcnat; policy accept;
                oifname "eth0" counter masquerade
        }
}

WireGuardウェブインターフェイス

サーバ、ピアの設定ファイルを作成する軽量なウェブアプリ。設定ファイルのQRコードを作成しメール送信が可能。バイナリをダウンロードするかソースからビルドして下さい。

バイナリから起動する場合

$ wget https://github.com/ngoduykhanh/wireguard-ui/releases/download/v0.3.7/wireguard-ui-v0.3.7-linux-amd64.tar.gz
$ tar -xvf wireguard-ui-v0.3.7-linux-amd64.tar.gz 
$ sudo ./wireguard-ui

下記URLへアクセスし管理ユーザ:admin 、パスワード:admin でログインして設定ファイルを作成します。

http://localhost:5000

設定ファイルのメール送信設定

GmailなどのSMTPサーバを使用する場合には下記設定が必要です。

# cd /etc/nano environment

SMTP_HOSTNAME="smtp.gmail.com"
SMTP_PORT="587"
SMTP_USERNAME="[email protected]"
SMTP_PASSWORD="xxxxxxxxxxxxxxxxxxxxxxxx"
SMTP_AUTH_TYPE="LOGIN"
EMAIL_FROM_ADDRESS="[email protected]"
EMAIL_FROM_NAME="FICUSONLINE"

起動時に下記のメールオプションを指定

$ sudo ./wireguard-ui --help
Usage of ./wireguard-ui:
  -bind-address string
    	Address:Port to which the app will be bound. (default "0.0.0.0:5000")
  -disable-login
    	Disable authentication on the app. This is potentially dangerous.
  -email-from string
    	'From' email address. (default "[email protected]")
  -email-from-name string
    	'From' email name. (default "FICUSONLINE STORE")
  -sendgrid-api-key string
    	Your sendgrid api key.
  -session-secret string
    	The key used to encrypt session cookies.
  -smtp-auth-type string
    	SMTP Auth Type : Plain or None. (default "LOGIN")
  -smtp-hostname string
    	SMTP Hostname (default "smtp.gmail.com")
  -smtp-no-tls-check
    	Disable TLS verification for SMTP. This is potentially dangerous.
  -smtp-password string
    	SMTP Password (default "xxxxxxxxxxx")
  -smtp-port int
    	SMTP Port (default 587)
  -smtp-username string
    	SMTP Password (default "[email protected]")

コマンドwg-quickの補足

wg-quick では、主に以下の ip コマンドによるコマンドフローを実行するスクリプトが伴うため、wg0 インターフェイスが使用中のため起動できないなどの問題が生じた場合には ip link delete コマンドを実行してインターフェイス wg0 を削除して下さい。

注) 設定ファイルでDNSサーバを指定する場合には別途 resolvconf のインストールが必要です。

Oct 24 01:00:14 ubutnu2204 systemd[1]: Starting WireGuard via wg-quick(8) for wg0...
Oct 24 01:00:14 ubutnu2204 wg-quick[1945]: [#] ip link add wg0 type wireguard
Oct 24 01:00:14 ubutnu2204 wg-quick[1945]: [#] wg setconf wg0 /dev/fd/63
Oct 24 01:00:14 ubutnu2204 wg-quick[1945]: [#] ip -4 address add 10.8.0.1/24 dev wg0
Oct 24 01:00:14 ubutnu2204 wg-quick[1945]: [#] ip link set mtu 1420 up dev wg0
Oct 24 01:00:14 ubutnu2204 wg-quick[1974]: [#] resolvconf -a tun.wg0 -m 0 -x
Oct 24 01:00:14 ubutnu2204 systemd[1]: Finished WireGuard via wg-quick(8) for wg0.

wg0 インターフェイスを削除

$ sudo ip link delete wg0 type wireguard

トラブルシュート

リアルタイムでのサーバ・ピア間の接続状態の確認

$ sudo watch wg
Every 2.0s: wg                j-wg: Fri Aug 26 17:44:37 2022

interface: wg0
  public key: +T3T3HTMeyrEDvim8FBxbYjbz+/POeOtG3Rlvl9kJmM=
  private key: (hidden)
  listening port: 51000

peer: 2cJdFcNzXv4YUGyDTahtOfrbsrFsCByatPnNzKTs0Qo=
  endpoint: 10.172.196.106:51000 
  allowed ips: 10.10.11.2/32
  latest handshake: 3 hours, 27 minutes, 35 seconds ago
  transfer: 3.06 KiB received, 2.80 KiB sent

カーネルデバックメッセージの確認

$ echo "module wireguard +p" | sudo tee /sys/kernel/debug/dynamic_debug/control
$ sudo dmesg -wT
.....
.....

上記設定の解除

$ echo "module wireguard -p" | sudo tee /sys/kernel/debug/dynamic_debug/control

DNS(peer側)の設定

サーバ側で使用しているDNSサーバをピア側で指定

サーバ側のDNSチェック

$ resolvectl status
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (ens2)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 10.48.0.5
       DNS Servers: 10.48.0.5
        DNS Domain: openstacklocal

Link 5 (gateway0)
Current Scopes: none
     Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported

ネットワークインターフェイス ens2 で使用しているDNSをピア側で指定

PostUp = resolvectl dns %i 10.48.0.5; resolvectl domain %i \~.

[Interface]
PostUp = wg set %i private-key /etc/wireguard/wg0.key
PostUp = resolvectl dns %i 10.48.0.5; resolvectl domain %i \~.
ListenPort = 51000
Address = 10.90.90.1/24

[Peer]
PublicKey = <contents of gateway0.pub>
Endpoint = <public IP of gateway server>
AllowedIPs = 0.0.0.0/0

MTUの設定

V6プラス(IPoE/IPv4 over IPv6)のネットワークでWireGuardを使用する場合、サーバ側とピア側でMTUの値を調整する必要があります(パケットヘッダーの影響。特に再接続する場合に不具合が生じます)。
WireGuardのMTUデフォルト値は 1420 なので、この値か、より小さな値 1412,1400,1372 で調整します。

参考値:サーバ側:1420、ピア側:1384

上記に依らずMTU値の変更方法は下記参照。

DHCPによる変更
https://linuxhint.com/how-to-change-mtu-size-in-linux/

Netplanによる変更

Netplanの設定ファイルを新規に作成します。ファイル名頭の 01-, 02- は設定の優先度を表しているため必ず付与すること。デフォルトの設定ファイル 01-network-manager-all.yaml からコピーを作成 02-eth0-mtu.yaml し編集します。

$ cd /etc/netplan

$ ls /etc/netplan
01-network-manager-all.yaml

$ cp  01-network-manager-all.yaml 02-eth0-mtu.yaml

02-eth0-mtu.yaml

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eth0:
      dhcp4: true
      mtu: 1400

設定のトライ(問題が無ければリターンキーで確定)

$ sudo netplan try

設定の確認は $ ip addr コマンドでインターフェイス毎にMTU値が表示されます。