码迷,mamicode.com
首页 > 其他好文 > 详细

利用iptables实现SNAT及DNAT

时间:2016-12-04 17:44:50      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:iptables   目标地址   源地址   



SNAT


技术分享


// 此时 filter 表上的 FORWARD 链 要打开(会经过此表中的此链)
[root@nat ~]# iptables -t nat -F
[root@nat ~]# iptables -t nat -L -n
[root@nat ~]# iptables -t nat -A POSTROUTING -s 10.1.249.158 -j SNAT --to-source 192.168.2.3
[root@nat ~]# iptables -t nat -L -n
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  10.1.249.158         0.0.0.0/0           to:192.168.2.3 
内部主机访问外部主机
[root@nei ~]# curl http://192.168.2.4/index.html
hello 

查看外部主机中web的访问日志
[root@wai ~]# tail /var/log/httpd/access_log
192.168.2.3 - - [13/Oct/2016:21:40:58 +0800]
192.168.2.3 - - [13/Oct/2016:21:40:59 +0800]
//主机,我们的内部主机IP为10.1.249.158
//而httpd的访问日志为192.168.2.3的访问
//说明成功实现了源地址转换



DNAT


技术分享


[root@nat ~]# iptables -t filter -F 
[root@nat ~]# iptables -t nat -F
[root@nat ~]# iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.4
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            10.1.249.125        tcp dpt:80 to:192.168.2.4

[root@nat ~]# netstat -tln | grep "\<80\>"  此时本机上并没有开放80端口

[root@wai ~]# curl http://10.1.249.125
hello  --> 此时我们访问为 nat 主机上的80端口  由上面可知,此服务器上并没有开放80,而是将请求送往 后端服务器



本文出自 “似水流年” 博客,请务必保留此出处http://sixijie123.blog.51cto.com/11880770/1879262

利用iptables实现SNAT及DNAT

标签:iptables   目标地址   源地址   

原文地址:http://sixijie123.blog.51cto.com/11880770/1879262

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!