我有两个位于不同子网的 Windows 环境(192.168.1.80/30&172.16.21.0/25),两者都静态分配地址,连接到具有两个 NIC 的单个 Debian 路由器。我已分配172.16.21.1到以太网1和192.168.1.81到以太坊2。每个 Windows 环境都使用各自的网关 IP。
如何允许 Windows 环境使用路由表相互 ping 通?我已经启用了net.ipv4.ip_forward=1在里面/etc/sysctl.conf文件。我尝试使用单独的路由表,但我的配置似乎不起作用。现在我只在每台机器上进行了IP配置,其他一切都是默认的。
ifconfig 输出:
eth1 Link encap:Ethernet HWaddr 00:0c:29:08:05:01
inet addr:172.16.21.1 Bcast:172.16.21.127 Mask:255.255.255.128
inet6 addr: fe80::20c:29ff:fe08:501/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:526 errors:0 dropped:0 overruns:0 frame:0
TX packets:562 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:44822 (43.7 KiB) TX bytes:40642 (39.6 KiB)
Interrupt:17 Base address:0x20a4
eth2 Link encap:Ethernet HWaddr 00:0c:29:08:05:0b
inet addr:192.168.1.81 Bcast:192.168.1.83 Mask:255.255.255.252
inet6 addr: fe80::20c:29ff:fe08:50b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:856 errors:0 dropped:0 overruns:0 frame:0
TX packets:909 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:71421 (69.7 KiB) TX bytes:85064 (83.0 KiB)
Interrupt:17 Base address:0x2424
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:47 errors:0 dropped:0 overruns:0 frame:0
TX packets:47 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4733 (4.6 KiB) TX bytes:4733 (4.6 KiB)
路由表(使用route -n):
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.21.1 0.0.0.0 UG 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth1
172.16.21.0 172.16.21.1 255.255.255.128 UG 0 0 0 eth1
192.168.1.80 192.168.1.81 255.255.255.252 UG 0 0 0 eth2
eth1 上的 tcpdump:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
14:35:38.591460 IP 172.16.21.2 > 192.168.1.82: ICMP echo request, id 1, seq 71, length 40
14:35:43.126147 ARP, Request who-has router (00:0c:29:08:05:01 (oui Unknown)) tell 172.16.21.2, length 46
14:35:43.126189 ARP, Reply router is-at 00:0c:29:08:05:01 (oui Unknown), length 28
14:35:43.141954 IP 172.16.21.2 > 192.168.1.82: ICMP echo request, id 1, seq 72, length 40
14:36:08.894329 IP router.mdns > 224.0.0.251.mdns: 0 [2q] PTR (QM)? _ipps._tcp.local. PTR (QM)? _ipp._tcp.local. (45)
14:36:09.658277 ARP, Request who-has 199.7.91.13 tell router, length 28
14:36:10.656763 ARP, Request who-has 199.7.91.13 tell router, length 28
14:36:10.707265 IP6 fe80::20c:29ff:fe08:501.mdns > ff02::fb.mdns: 0 [2q] PTR (QM)? _ipps._tcp.local. PTR (QM)? _ipp._tcp.local. (45)
答案1
要使 Linux 计算机充当路由器,您需要告诉它如何路由来自两个子网的流量。
您需要使用路由命令为每个子网添加路由,类似这样的事情应该可以工作:
route add -net 192.168.1.80/30 gw 192.168.1.81 dev eth2
route add -net 172.16.21.0/25 gw 172.16.21.1 dev eth1
如果您已经net.ipv4.ip_forward=1
像您所说的那样激活了,那么它应该可以工作。如果您在 debian 计算机上启用了防火墙,则需要对其进行适当的配置。
答案2
根据https://askubuntu.com/questions/95199/two-network-cards-and-ip-forwarding,从一个网络接口转发到另一个网络接口(例如从 eth0 到 eth1)需要以下 iptables 规则
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT
鉴于您希望双向转发工作,您还需要有相反的规则:
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -A FORWARD -i eth1 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT