0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WireGuard /w firewalldのゾーン間転送

0
Last updated at Posted at 2026-06-29

WireGuardをRHEL系のOSで構築する記事はいくつかあるのですが、近年のfirewaldのバックエンドであるnftablesバックエンドで使用できないACCEPTを含むdirectルールを使用している記事しか見つからなかったので、今回はそれを使用せずゾーン間転送でWireGuardを実装する例を紹介したいと思います。

環境

WireGuard Server
・AlmaLinux 10.2 + WireGuard tools v1.0.20260223
・DMZ側のIPアドレスとして172.16.3.200/24を持ち、LAN側のIPアドレスとして172.16.1.200/24を持つこととします。
・WANからWireGuard Serverを通ってLANに抜けることができればOKとします。
WireGuard Client: Windows 11 + WireGuard v1.1

サーバー側の構築

必要なパッケージをインストールします。

$ sudo dnf install wireguard-tools

WireGuardの内部NWからNICへパケットの転送を許可するようにカーネルパラメーターを設定します。

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

再起動します。

$ sudo reboot

サーバー用の鍵ペアを作成します。

$ wg genkey | sudo tee /etc/wireguard/server.key
$ sudo cat /etc/wireguard/server.key | wg pubkey | sudo tee /etc/wireguard/server.pub
$ sudo chmod 600 /etc/wireguard/server.key
$ sudo chmod 600 /etc/wireguard/server.pub

クライアント側の鍵ペアの作成

配布のインストーラーを使用してWireGuardをインストールしたあと、Powershellで以下を実行します。

wg genkey > "<適切なファイルパス>\client.key"
get-content "<適切なファイルパス>\client.key" | wg pubkey > "<適切なファイルパス>\client.pub"

サーバーの設定

NICの設定を行います。
ens19がLAN側のNICです。(IPアドレス=172.16.1.200)
このNICをゾーンexternalに割り当てます。
ポリシーを作成し、ターゲットをACCEPT(ルールに一致しないパケットをすべて通す)にします。

$ sudo firewall-cmd --permanent --new-policy wg0-to-ens19-fwd
$ sudo firewall-cmd --permanent --change-interface=ens19 --zone=external 
$ sudo firewall-cmd --permanent --policy wg0-to-ens19-fwd --set-target ACCEPT
$ sudo firewall-cmd --reload

次に、WireGuardの定義ファイルを作成します。
まず最初に、MTUを正しく設定しないと速度が全く出ないのでそれを回避します。
指定すべきMTUは使用する回線のうち小さい方のMTU - WireGuardのヘッダ長なので、私の環境では自宅のXpassの1460 - WireGuardのIPv4のヘッダ長 60 ということで1400になります。
PostUpでは起動時にWireGuardの仮想ネットワーク用のNIC wg0をゾーンinternalに割り当てます。
ゾーンinternalを先ほど作成したポリシーの進入(ingress)ゾーンとして指定します。
また、ゾーンexternalについて、ポリシーの出口(egress)ゾーンに指定します。
これによって、ポリシーwg0-to-ens19-fwdにて入り口と出口が決まり、トラフィックを制限するルールがないのですべての通信がACCEPTされ、WireGuardの内部ネットワークとLAN172.16.1.0/24が通信できるようになります。
ゾーンinternalについてIPマスカレードを設定して、172.16.1.0/24上のホストに戻りの経路がなくても外部と通信できるようにします。
最後にpublicゾーン(DMZにあるNICの所属するゾーン)に対して51820/udpを許可し、外部からの接続を受け入れるようにします。
PostDownはPostUpの逆を行います。

/etc/wireguard/wg0.conf
[Interface]
PrivateKey = <server.key>の内容
Address = 10.0.0.1/24 # WireGuard内NWでサーバーに割り当てるIPアドレス
ListenPort = 51820
MTU = 1400

PostUp = firewall-cmd --change-interface=wg0 --zone=internal
PostUp = firewall-cmd --policy wg0-to-ens19-fwd --add-ingress-zone internal
PostUp = firewall-cmd --policy wg0-to-ens19-fwd --add-egress-zone external
PostUp = firewall-cmd --zone=internal --add-masquerade
PostUp = firewall-cmd --add-port=51820/udp --zone=public

PostDown = firewall-cmd --policy wg0-to-ens19-fwd --remove-ingress-zone internal
PostDown = firewall-cmd --policy wg0-to-ens19-fwd --remove-egress-zone external
PostDown = firewall-cmd --zone=internal --remove-masquerade
PostDown = firewall-cmd --remove-port=51820/udp --zone=public

[Peer]
PublicKey = <clienct.pub>の内容
AllowedIPs = 10.0.0.10/32 # クライアントに割り当てるIPアドレス

クライアント側の設定

次のファイルを作成します。

[Interface]
PrivateKey = <client.key>の内容
Address = 10.0.0.10/32 # クライアントに割り当てるIPアドレス

[Peer]
PublicKey = <server.pub>の内容
EndPoint = <WireGuardサーバーのIPアドレス>:51820
AllowedIPs = 10.0.0.0/24, 172.16.1.0/24 # クライアントからWireGuard経由で通信する先の設定

Wireguardアプリを起動し、「ファイルからトンネルをインポート」します。
スクリーンショット 2026-06-29 193206.png

起動

作成した定義wg0を指定して、WireGuardを起動します。

$ wg-quick up wg0
$ sudo systemctl enable wg-quick@wg0

クライアント側でトンネルを有効化します。
スクリーンショット 2026-06-29 193545.png
正常に機能しています。

]$ sudo wg show
interface: wg0
  public key: vYdmtB5ll3rQZOeDTVB/XGi11wCW2qcEu0PlXQ1Q/gM=
  private key: (hidden)
  listening port: 51820

peer: wspG7m/sCbjLTvyyBiepJNPwrDUibQRRV8j1ATj4ghA=
  endpoint: <クライアント側グローバルIP>:4652
  allowed ips: 10.0.0.10/32
  latest handshake: 32 seconds ago
  transfer: 2.45 KiB received, 312 B sent

クライアント側から172.16.1.30にアクセスしてみると、正しく172.16.1.200からアクセスしてきていることがわかります。

ssh -l <username> 172.16.1.30
<username>@172.16.1.30's password:
Last login: Mon Jun 29 19:47:23 2026 from 172.16.1.200
$
/var/log/secure
Jun 29 19:52:11 localhost sshd-session[2429]: Accepted password for <username> from 172.16.1.200 port 61080 ssh2

おまけ

ゾーン内転送を活用する方式。(ここではすべてのNICがpublicに存在する場合の設定例を挙げていますが、同じゾーンであれば何でも構いません)
ゾーン内転送は簡単ですが、DMZ、LAN、WireGuard内部NW用のNICがすべて同じゾーンに存在することになり適切なfirewall ruleの設定が難しくなると考えます。

/etc/wireguard/wg0.conf
[Interface]
PrivateKey = <server.key>の内容
Address = 10.0.0.1/24 # WireGuard内NWでサーバーに割り当てるIPアドレス
ListenPort = 51820
PostUp = firewall-cmd --add-masquerade; firewall-cmd --zone=public --add-forward; firewall-cmd --add-port=51820/udp
PostDown = firewall-cmd --remove-masquerade; firewall-cmd --zone=public --remove-forward; firewall-cmd --remove-port=51820/udp

[Peer]
PublicKey = <client.pub>の内容
AllowedIPs = 10.0.0.10/32 # クライアントに割り当てるIPアドレス

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?