Iptables 完成埠對映

2021-06-13 11:14:13 字數 3872 閱讀 9638

q:一區域網192.168.1.0/24,有web和ftp伺服器192.168.1.10、192.168.1.11,閘道器linux,內網eth0,ip為192.168.1.1,外網eth1,ip為a.b.c.d,怎樣作nat能使內外網都能訪問公司的伺服器?

a:# 

web# 

用dnat作埠對映

iptables 

-t nat 

-a prerouting 

-d a.b.c.d 

-p tcp 

--dport 

80 -j 

dnat 

--to 

192.168.1.10

# 用snat作源位址轉換(關鍵),以使回應包能正確返回

iptables 

-t nat 

-a postrouting 

-d 192.168.1.10 

-p tcp 

--dport 

80 -j 

snat 

--to 

192.168.1.1

# 一些人經常忘了開啟forward鏈的相關埠,特此增加

iptables 

-a forward 

-o eth0 

-d 192.168.1.10 

-p tcp 

--dport 

80 -j 

accept

iptables 

-a forward 

-i eth0 

-s 192.168.1.10 

-p tcp 

--sport 

80 -m 

--state 

established 

-j accept

# ftp

modprobe 

ip_nat_ftp 

###載入ip_nat_ftp模組(若沒有編譯進核心),以使ftp能被正確nat

modprobe 

ip_conntrack_ftp 

###載入ip_conntrack_ftp模組

# 用dnat作埠對映

iptables 

-t nat 

-a prerouting 

-d a.b.c.d 

-p tcp 

--dport 

21 -j 

dnat 

--to 

192.168.1.11

iptables 

-a forward 

-o eth0 

-d 192.168.1.11 

-p tcp 

--dport 

21 -j 

accept

iptables 

-a forward 

-i eth0 

-s 192.168.1.11 

-p tcp 

--sport 

21 -m 

--state 

established 

-j accept

iptables 

-a forward 

-i eth0 

-s 192.168.1.11 

-p tcp 

--sport 

20 -m 

--state 

established,related 

-j accept

iptables 

-a forward 

-o eth0 

-d 192.168.1.11 

-p tcp 

--dport 

20 -m 

--state 

established 

-j accept

iptables 

-a forward 

-o eth0 

-d 192.168.1.11 

-p tcp 

--dport 

1024: 

-m --state 

established,related 

-j accept

iptables 

-a forward 

-i eth0 

-s 192.168.1.11 

-p tcp 

--sport 

1024: 

-m --state 

established 

-j accept

# 用snat作源位址轉換(關鍵),以使回應包能正確返回

iptables 

-t nat 

-a postrouting 

-d 192.168.1.11 

-p tcp 

--dport 

21 -i 

eth0 

-j snat 

--to 

192.168.1.1 

q:網路環境如上一問題,還在閘道器上用squid進行透明**,也作了snat了,為什麼內網還是不能訪問公司的web伺服器?iptables如下:

iptables 

-t nat 

-a prerouting 

-s 192.168.1.0/24 

-p tcp 

--dport 

80 -i 

eth0 

-j redirect 

--to 

3128

iptables 

-t nat 

-a prerouting 

-d a.b.c.d 

-p tcp 

--dport 

80 -j 

dnat 

--to 

192.168.1.10

iptables 

-t nat 

-a postrouting 

-d 192.168.1.10 

-p tcp 

--dport 

80 -j 

snat 

--to 

192.168.1.1

a:問題主要在prerouting鏈中redirect和dnat的順序,由於先進行了redirect(重定向),則到第二句dnat時,埠已變為3128,不匹配第二句的目的埠80,dnat也就不會執行,不能到達正確的目的地。解決的辦法有兩個:

1、把redirect語句放到dnat語句的後面,如下:

iptables 

-t nat 

-a prerouting 

-d a.b.c.d 

-p tcp 

--dport 

80 -j 

dnat 

--to 

192.168.1.10

iptables 

-t nat 

-a prerouting 

-s 192.168.1.0/24 

-p tcp 

--dport 

80 -i 

eth0 

-j redirect 

--to 

3128

2、在redirect語句中增加匹配目的位址"-d 

! a.b.c.d",如下:

iptables 

-t nat 

-a prerouting 

-s 192.168.1.0/24 

-d ! 

a.b.c.d 

-p tcp 

--dport 

80 -i 

eth0 

-j redirect 

--to 

3128

iptables 埠對映指令碼

echo 1 proc sys net ipv4 ip forward sbin iptables a input p tcp dport 3389 i eth0 j accept sbin iptables t nat a prerouting i eth0 d 192.168.5.7 p tcp...

iptables 埠對映設定

首先應該做的是 etc sysctl.conf配置檔案的 net.ipv4.ip forward 1 預設是0 臨時生效 echo 1 proc sys net ipv4 ip forward 設我們有一台計算機,有兩塊網絡卡,eth0連外網,ip為1.2.3.4 eth1連內網,ip為192.16...

iptables埠對映例項

環境 主機1 ip 192.168.10.25 能訪問外網 ip 192.168.100.1 閘道器 主機2 ip 192.168.100.23 web伺服器 要求 192.168.10.0 24網段ip能訪問web伺服器,web伺服器也能訪問外網。備註 192.168.10.0 24和192.16...