我已将 Ubuntu 22.04 LTS 设置为路由器,用于以下场景:
Client (192.168.222.100) <--> Switch <--> Ubuntu Router eth0 (192.168.222.1) <--> Ubuntu Router enx2887ba778049 (192.168.178.111) <--> WAN Router (192.168.178.1) <--> Internet
这是我的 netplan 配置:
network:
ethernets:
eth0:
dhcp4: false
enx2887ba778049:
dhcp4: true
optional: true
bridges:
br0:
interfaces:
- eth0
addresses:
- 192.168.222.1/24
nameservers:
addresses: [8.8.8.8]
search: []
optional: true
version: 2
这是我的 dnsmasq 配置:
port=53
domain-needed
bogus-priv
strict-order
except-interface=enx2887ba778049
expand-hosts
domain=example.com
dhcp-range=192.168.222.50,192.168.222.240,255.255.255.0,24h
dhcp-option=option:router,192.168.222.1
dhcp-option=option:dns-server,192.168.222.1
dhcp-option=option:netmask,255.255.255.0
从我的客户端来看,网址得到解析:
dig google.com
; <<>> DiG 9.10.6 <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47720
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 269 IN A 142.250.74.206
然而,问题是我无法联系到他们:
ping google.com
PING google.com (142.250.74.206): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
我想我必须对 iptables 做些什么,但是以下操作失败了:
sudo iptables -t nat -A POSTROUTING -o enx2887ba778049 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
有什么想法我需要做什么才能从客户端连接到互联网?