雙網卡實現反向NAT訪問

雙網卡實現反向NAT訪問

需要協助實現此目標:

Internet
    <-- TP_LINK Router(192.168.0.1)
        <-- PC1(eth0:192.168.0.8)
        <-- PC2(eth0:192.168.0.81)

當我使用電纜將 PC2 eth1 連接到 PC3 eth0 並將 PC2 eth1 IPV4 設定配置為「共用到其他電腦」時,PC2 和 PC3 獲得以下 IP:

    PC2(eth1:10.42.0.1)
        <-- PC3(eth0: 10.42.0.169)

現在我想在路由或 iptables 上做一些事情,以便我可以在 PC1 上「ping 10.42.0.169」。

這可能嗎?以下是我嘗試過的:

  • 設定TP_LINK路由器的靜態路由表:10.42.0.0(目標) - 255.255.255.0(網路遮罩) - 192.168.0.81(網關)。

現在我在 PC1 上得到結果:

$ traceroute 10.42.0.169
traceroute to 10.42.0.169 (10.42.0.169), 64 hops max, 52 byte packets
1  192.168.0.1 (192.168.0.1)  4.018 ms  0.905 ms  0.768 ms
2  ay11 (192.168.0.81)  1.140 ms  1.273 ms  1.482 ms
3  ay11 (192.168.0.81)  1.104 ms  1.041 ms  1.127 ms

我們可以看到,如果PC2能夠將封包轉送到10.42.0.0/24,也許一切就完美了?

PC2上的配置如下:

$ route
default         192.168.0.1     0.0.0.0         UG    100    0        0 eth1
10.42.0.0       *               255.255.255.0   U     100    0        0 eth0
link-local      *               255.255.0.0     U     1000   0        0 eth1
192.168.0.1     *               255.255.255.255 UH    100    0        0 eth1

$ sudo iptables -L
[sudo] password for mlhch: 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             10.42.0.0/24         state RELATED,ESTABLISHED
ACCEPT     all  --  10.42.0.0/24         anywhere            
ACCEPT     all  --  anywhere             anywhere            
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

我應該如何處理路由或 iptables?

謝謝!

答案1

嘗試將其設定為開啟PC2首先要做的就是啟用 IP 轉送。這可以透過使用來完成

echo "1" > /proc/sys/net/ipv4/ip_forward

然後,我們將新增一條規則來告訴轉送流量

sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT

答案2

感謝您的努力。

對於/proc/sys/net/ipv4/ip_forward,它已經是“1”並且我在/etc/sysctl.conf中配置了net.ipv4.ip_forward=1

我執行了你的 iptables 命令,它們只是在 Chain FORWARD 部分添加了以下 2 行:

ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED

然後就沒有奇蹟發生了。但這讓我注意到了 2 條 REJECT 行

REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

我刪除它們,一切都完美!事實上我也刪除了新新增的行,只保留 1 行「ACCEPT all --anywhere Anywhere」。

相關內容