具有 NAT 的多個路由和網關

具有 NAT 的多個路由和網關

我的設置是一台具有兩個網絡接口的虛擬機主機,我想將一個網絡接口用作私有/內部網絡接口,另一個用作公共網絡接口(這將在主機上獲得公共地址),其中將發生對來賓電腦的所有NAT 魔法。

兩個介面意味著兩個網關(一個 viaeth0, 10.0.0.1和 上的假位址eth1, 2.0.0.1)。

如果我正確理解路由,我必須為2.0.0.1具有自己的預設網關的網路設定路由表。不幸的是,除非我將預設閘道新增至主機上的預設表,否則無法路由訪客的流量。我希望主機用作10.0.0.1其預設網關,但我不知道該怎麼做。

細節:

具體來說,我想default via 2.0.0.1 dev eth1在主機上刪除它,但如果刪除它,tcpdump -i vbr0看起來不太好(在下面的範例中嘗試從來賓中查找 serverfault.com):

IP 192.168.10.2.32804 > 8.8.4.4.domain: 41116+ A? serverfault.com. (24)
IP 192.168.10.1 > 192.168.10.2: ICMP net 8.8.4.4 unreachable, length 60

儘管如此,訪客仍然能夠到達192.168.10.1(其網關)並且2.0.0.1。此外,使用2.0.0.1從來賓查找位址(該路由器也有 DNS 服務)是可行的,因此本地路由似乎可以工作,「僅」預設網關部分似乎以某種方式搞砸了。

2.0.0.1難道來自 guest(或 vb0)的所有流量都不應使用路由表「public」中定義的預設閘道嗎?


設定:

# ip route
default via 2.0.0.1 dev eth1
10.0.0.0/24 dev eth0  proto kernel  scope link  src 10.0.0.15
2.0.0.0/24 dev eth1  proto kernel  scope link  src 2.0.0.55
192.168.10.0/24 dev vbr0  proto kernel  scope link  src 192.168.10.1


# ip route show public
default via 2.0.0.1 dev eth1
2.0.0.0/24 dev eth1  scope link  src 2.0.0.55
192.168.10.0/24 dev vbr0  scope link  src 192.168.10.1


# ip rule
0:      from all lookup local
32761:  from 192.168.10.1 lookup public
32762:  from all to 192.168.10.1 lookup public
32763:  from all to 2.0.0.55 lookup public
32765:  from 2.0.0.55 lookup public
32766:  from all lookup main
32767:  from all lookup default


# iptables -vnL
Chain INPUT (policy ACCEPT 1222 packets, 91256 bytes)
 pkts bytes target     prot opt in     out     source               destination
   13   648 REJECT     all  --  *      *       0.0.0.0/0            2.0.0.55          reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 193 packets, 297K bytes)
 pkts bytes target     prot opt in     out     source               destination
  266 51860 ACCEPT     all  --  *      *       0.0.0.0/0            192.168.10.0/24      ctstate NEW,RELATED,ESTABLISHED

Chain OUTPUT (policy ACCEPT 1157 packets, 149K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain LOGDROP (0 references)
 pkts bytes target     prot opt in     out     source               destination


# iptables -vnL -t nat
Chain PREROUTING (policy ACCEPT 49 packets, 5494 bytes)
 pkts bytes target     prot opt in     out     source               destination
    6   360 DNAT       tcp  --  *      *       0.0.0.0/0            2.0.0.55          tcp dpt:80 to:192.168.10.2:80
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            2.0.0.55          tcp dpt:443 to:192.168.10.2:443
    1    60 DNAT       tcp  --  *      *       0.0.0.0/0            2.0.0.55          tcp dpt:22022 to:192.168.10.2:22

Chain INPUT (policy ACCEPT 24 packets, 3414 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 5 packets, 317 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 12 packets, 737 bytes)
 pkts bytes target     prot opt in     out     source               destination
    5   318 SNAT       all  --  *      *       192.168.10.2         0.0.0.0/0            to:2.0.0.55

相關內容