首页 > 代码库 > iptables之nat转发
iptables之nat转发
主要分为路由前转发PREROUTING,和路由后转发POSTROUTING。
何为路由前?也就是从外部传入数据,在到达主机网卡,还未进入网卡的瞬间。
何为路由后?也就是从外部传入数据,进入并经过主机某个网卡后,向外传出数据的瞬间。
一个简单的模型如下:
外部数据------>主机网卡(如eth0、eth1等)------>目的地
路由前 路由后
首先把转发策略打开
[root@localhost~]# vi /etc/sysctl.conf net.ipv4.ip_forward=1 [root@localhost~]# sysctl -p
一、路由前转发
[root@localhost ~]# iptables -t nat -A POSTROUTING -d 192.168.1.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.125:8000
查看路由前nat表
[root@localhost ~]# 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
删除路由前第一条规则,注意删除后规则标签会变化
[root@localhost ~]#iptables -t nat -D PREROUTING 1 [root@localhost ~]# 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表
[root@localhost ~]# iptables -t nat -F PREROUTING [root@localhost ~]# 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
二、路由后转发
[root@localhost ~]# iptables -t nat -A POSTROUTING -d 192.168.125 -p tcp --dport 80 -j DNAT --to-source 192.168.1.125:8000
查看路由后nat表
[root@localhost ~]# 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
删除路由后第二条规则,注意删除后规则标签会变化
[root@localhost ~]# iptables -t nat -D POSTROUTING 2 [root@localhost ~]# 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表
[root@localhost ~]# iptables -t nat -F POSTROUTING [root@localhost ~]# 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表
[root@localhost ~]# iptables -t nat -F
特别注意:以上所有命令执行后的结果都是保存在内存中,重启系统后重置。要执行:
[root@localhost ~]# /sbin/iptables-save
命令保存到磁盘中。不同系统命令略有区别,这里是RHEL7或Centos7
本文出自 “运维笔记” 博客,请务必保留此出处http://quliren.blog.51cto.com/9849266/1943295
iptables之nat转发
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。