Iptables

在同一物理網路上的兩個子網之間設置網關

  • June 27, 2014

我需要在物理層上配置一個啟用 DHCP 的網路 (192.168.2.),該物理層位於一個具有靜態 IP 的現有網路 (192.168.1.) 上。我有一個 Debian 7 伺服器,有兩個介面(伺服器和介面都是虛擬的),我想將 ip 設置為我的網路的網關。我使用 eth0 將數據包路由到原始網路(訪問位於 192.168.1.5 的 Internet 網關),使用 eth1 處理進出我的網路的流量。

如果配置

eth0      Link encap:Ethernet  HWaddr 00:0c:29:d4:02:1b  
         inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0
         inet6 addr: fe80::20c:29ff:fed4:21b/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:21668983 errors:0 dropped:0 overruns:0 frame:0
         TX packets:10044848 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:10931368249 (10.1 GiB)  TX bytes:2383839079 (2.2 GiB)

eth1      Link encap:Ethernet  HWaddr 00:0c:29:d4:02:25  
         inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
         inet6 addr: fe80::20c:29ff:fed4:225/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:14113604 errors:0 dropped:0 overruns:0 frame:0
         TX packets:11269734 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:1871598524 (1.7 GiB)  TX bytes:10331981618 (9.6 GiB)

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:16436  Metric:1
         RX packets:8158 errors:0 dropped:0 overruns:0 frame:0
         TX packets:8158 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:629690 (614.9 KiB)  TX bytes:629690 (614.9 KiB)

路線

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.5     0.0.0.0         UG    0      0        0 eth0
localnet        *               255.255.255.0   U     0      0        0 eth0
192.168.2.0     *               255.255.255.0   U     0      0        0 eth1

iptables -vL

Chain INPUT (policy ACCEPT 5603K packets, 822M bytes)
pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy DROP 138K packets, 8597K bytes)
pkts bytes target     prot opt in     out     source               destination         
 14M 9542M ACCEPT     all  --  any    any     anywhere             anywhere             state RELATED,ESTABLISHED
398K   27M ACCEPT     all  --  eth1   any     anywhere             anywhere            

Chain OUTPUT (policy ACCEPT 2915K packets, 1432M bytes)
pkts bytes target     prot opt in     out     source               destination         

iptables -tnat -vL

Chain PREROUTING (policy ACCEPT 607K packets, 49M bytes)
pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 112K packets, 17M bytes)
pkts bytes target     prot opt in     out     source               destination         

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

Chain POSTROUTING (policy ACCEPT 2391 packets, 374K bytes)
pkts bytes target     prot opt in     out     source               destination         
363K   24M MASQUERADE  all  --  any    eth0    anywhere             anywhere   

之後,我在 eth1 上啟用了一個權威的 DHCP 伺服器。

現在的問題:與伺服器的連接總是有效(我有一個 samba 共享和一個 MySQL 伺服器)但有時(似乎隨機)客戶端(主要是 Windows 7 或 XP)在可變的時間段內無法連接到網際網路. 在這種情況下,我可以 ping 到 192.168.1.110,但不能 ping 到 192.168.1.5。

附錄

FORWARD 鏈丟棄數據包的事實似乎很可疑,所以我暫時禁用了過濾:

iptables -A FORWARD -j ACCEPT

有了這個新規則,一切都會正常進行。不過,我仍然需要澄清發生了什麼…

附錄 2

這些是實際的 iptables 規則:

iptables-保存

# Generated by iptables-save v1.4.14 on Fri Jun 27 20:53:32 2014
*mangle
:PREROUTING ACCEPT [28129147:14012989399]
:INPUT ACCEPT [8479051:1218948772]
:FORWARD ACCEPT [19639349:12792010625]
:OUTPUT ACCEPT [4434912:3183821941]
:POSTROUTING ACCEPT [23940877:15968783924]
COMMIT
# Completed on Fri Jun 27 20:53:32 2014
# Generated by iptables-save v1.4.14 on Fri Jun 27 20:53:32 2014
*nat
:PREROUTING ACCEPT [931027:74896097]
:INPUT ACCEPT [153578:23398245]
:OUTPUT ACCEPT [9169:1292388]
:POSTROUTING ACCEPT [3186:492868]
-A POSTROUTING -o eth0 -j MASQUERADE
COMMIT
# Completed on Fri Jun 27 20:53:32 2014
# Generated by iptables-save v1.4.14 on Fri Jun 27 20:53:32 2014
*filter
:INPUT ACCEPT [2415796:331288771]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [1218435:1654003511]
-A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i eth1 -j ACCEPT
-A FORWARD -j ACCEPT
COMMIT
# Completed on Fri Jun 27 20:53:32 2014

為了幫助您診斷數據包流經 iptables 鏈的位置,您可以使用 -j LOG 參數,最後使用 –log-prefix “一些文本以輕鬆辨識 kern.log 或 syslog 中的日誌” 您可以讓預設的 Accept 策略和在啟用日誌記錄的 FORWARD 鏈末尾添加拒絕所有規則,以便您可以更好地了解正在丟棄的數據包類型。

你可以看看這個示意圖http://www.linuxnetmag.com/share/issue9/iptables3.jpg,它顯示了 iptables 鏈中數據包的基本流。

擁有您定義的完整 iptable 規則以給出更好的答案會很有趣。

引用自:https://unix.stackexchange.com/questions/139086