来自 Wireguard 的 wg-quick 手册页的示例:它如何工作?

来自 Wireguard 的 wg-quick 手册页的示例:它如何工作?

这是来自 wg-quick 手册页的示例部分:

A combination of the `Table', `PostUp', and `PreDown'
fields may be used for policy routing as well. For example,
the following may be used to send SSH traffic (TCP port 22)
traffic through the tunnel:

[Interface]
Address = 10.192.122.1/24
PrivateKey = yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=
ListenPort = 51820
Table = 1234
PostUp = ip rule add ipproto tcp dport 22 table 1234
PreDown = ip rule delete ipproto tcp dport 22 table 1234

[Peer]
PublicKey = xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=
AllowedIPs = 0.0.0.0/0

我觉得这个例子不完整。难道不应该有一个ip route指定table 1234来完成通过隧道发送 SSH 流量的操作吗?我远不是 Wireguard 和路由方面的专家,所以也许幕后发生了一些我没注意到的事情。

答案1

默认情况下,wg-quick 会自动通过 wg0 添加与此接口上所有对等点的 AllowedIP 相对应的路由。这就是您示例中 Table= 参数的全部用途。

(尽管即使未指定 Table= 也会发生这种情况 - 然后路由会被添加到主表中。可以使用 Table=off 来选择退出此行为。)

systemd-networkd 的 WireGuard 支持中也存在类似的功能,但是是选择加入的(即,即使对于主表也必须指定 Table=)。

相关内容