1. 程式人生 > >iptables之nat轉發

iptables之nat轉發

iptables

主要分為路由前轉發PREROUTING,和路由後轉發POSTROUTING。

何為路由前?也就是從外部傳入數據,在到達主機網卡,還未進入網卡的瞬間。

何為路由後?也就是從外部傳入數據,進入並經過主機某個網卡後,向外傳出數據的瞬間。

一個簡單的模型如下:

外部數據------>主機網卡(如eth0、eth1等)------>目的地

路由前 路由後

首先把轉發策略打開

[[email protected]~]# vi /etc/sysctl.conf 
net.ipv4.ip_forward=1 
[[email protected]
/* */~]# sysctl -p

一、路由前轉發

[[email protected] ~]# iptables -t nat -A POSTROUTING -d 192.168.1.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.125:8000

查看路由前nat表

[[email protected] ~]# iptables -t nat -vnL PREROUTING --line-number
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       52  6436 PREROUTING_direct  all  --  *      *       0.0.0.0/0          0.0.0.0/0           
2       52  6436 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0     0.0.0.0/0           
3       52  6436 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.125.0        tcp dpt:80 to:192.168.1.125:8000
5        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.1.125        tcp dpt:80 to:192.168.1.125:8000
6        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.1.125        tcp dpt:80 to:192.168.1.125

刪除路由前第一條規則,註意刪除後規則標簽會變化

[[email protected] ~]#iptables -t nat -D PREROUTING 1
[[email protected] ~]# iptables -t nat -vnL PREROUTING --line-number
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      148 16474 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0     0.0.0.0/0           
2      148 16474 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.125.0        tcp dpt:80 to:192.168.1.125:8000
4        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.1.125        tcp dpt:80 to:192.168.1.125:8000
5        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.1.125        tcp dpt:80 to:192.168.1.125

清空路由前nat表

[[email protected] ~]# iptables -t nat -F PREROUTING
[[email protected] ~]# iptables -t nat -vnL PREROUTING --line-number
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination

二、路由後轉發

[[email protected] ~]# iptables -t nat -A POSTROUTING -d 192.168.125 -p tcp --dport 80 -j DNAT --to-source 192.168.1.125:8000

查看路由後nat表

[[email protected] ~]# iptables -t nat -vnL POSTROUTING --line-number
Chain POSTROUTING (policy ACCEPT 1 packets, 76 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        2   267 RETURN     all  --  *      *       192.168.122.0/24     224.0.0.0/24        
2        0     0 RETURN     all  --  *      *       192.168.122.0/24     255.255.255.255     
3        0     0 MASQUERADE  tcp  --  *      *       192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
4        0     0 MASQUERADE  udp  --  *      *       192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
5        0     0 MASQUERADE  all  --  *      *       192.168.122.0/24    !192.168.122.0/24    
6      129 10207 POSTROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
7      129 10207 POSTROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0    0.0.0.0/0           
8      129 10207 POSTROUTING_ZONES  all  --  *      *       0.0.0.0/0           0.0.0.0/0           
9        0     0 SNAT       tcp  --  *      *       192.168.125.0        0.0.0.0/0            tcp dpt:80 to:192.168.1.125:8000
10       0     0 SNAT       tcp  --  *      eth0    192.168.1.125        0.0.0.0/0            tcp dpt:80 to:192.168.1.125:8000

刪除路由後第二條規則,註意刪除後規則標簽會變化

[[email protected] ~]# iptables -t nat -D POSTROUTING 2
[[email protected] ~]# iptables -t nat -vnL POSTROUTING --line-number
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        2   267 RETURN     all  --  *      *       192.168.122.0/24     224.0.0.0/24        
2        0     0 MASQUERADE  tcp  --  *      *       192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
3        0     0 MASQUERADE  udp  --  *      *       192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
4        0     0 MASQUERADE  all  --  *      *       192.168.122.0/24    !192.168.122.0/24    
5      133 10511 POSTROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
6      133 10511 POSTROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
7      133 10511 POSTROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
8        0     0 SNAT       tcp  --  *      *       192.168.125.0        0.0.0.0/0            tcp dpt:80 to:192.168.1.125:8000
9        0     0 SNAT       tcp  --  *      eth0    192.168.1.125        0.0.0.0/0            tcp dpt:80 to:192.168.1.125:8000

清空路由後nat表

[[email protected] ~]# iptables -t nat -F POSTROUTING
[[email protected] ~]# iptables -t nat -vnL POSTROUTING --line-number
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination

清空所有nat表

[[email protected] ~]# iptables -t nat -F


特別註意:以上所有命令執行後的結果都是保存在內存中,重啟系統後重置。要執行:

[[email protected] ~]# /sbin/iptables-save

命令保存到磁盤中。不同系統命令略有區別,這裏是RHEL7或Centos7


本文出自 “運維筆記” 博客,請務必保留此出處http://quliren.blog.51cto.com/9849266/1943295

iptables之nat轉發