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

iptables (三)

时间:2018-03-20 22:55:44      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:iptables   NAT   SNAT   DNAT   端口转发   

NAT

network address translation 网络地址转换

功能实现在 PREROUTING,INPUT,OUTPUT,POSTROUTING

请求报文:修改源/目标IP,定义如何修改
响应报文:修改源/目标IP,根据跟踪机制自动实现

SNAT

source NAT 源网络地址转换。让本地网络中的主机通过某一特定地址访问外部网络,实现地址伪装

功能实现在 POSTROUTING, INPUT

请求报文:修改源IP

DNAT

destination NAT 目标网络地址转换。把本地网络中的主机上的某服务开放给外部网络访问(发布服务和端口映射),但隐藏真实IP

功能实现在 PREROUTING , OUTPUT

请求报文:修改目标IP

PNAT

port nat

端口和IP都进行修改

命令语法

SNAT

--to-source [ipaddr[-ipaddr]][:port[-port]]   如:192.168.7.1-192.168.7.5
--random   --随机
--to-source 将源地址转换的外网IP地址
-d 目标网络
-s 指定将哪些内网网段进行映射转换
-o ethX 指定哪个网卡接口上做NAT地址转换,通常用在出口网卡
-j MASQUERADE 动态拨号使用

# iptables -t nat -I POSTROUTING -s 192.168.7.0/24 ! -d 192.168.7.0/24 -j SNAT --to-source 172.18.44.202    > 固定公网IP
# iptables -t nat -R POSTROUTING 1 -s 192.168.7.0/24 -o eth1 -j MASQUERADE                                  > 动态公网IP

DNAT


--to-destination [ipaddr[-ipaddr]][:port[-port]]

iptables -t nat -A PREROUTING -d 公网IP -p tcp|udp --dport PORT -j DNAT --to-destination 内网IP[:端口]

# iptables -t nat -A PREROUTING -d 172.18.44.202 -p tcp --dport 80 -j DNAT --to-destination 192.168.7.203:80
实验环境:VMware Workstation Pro 14(试用版)
系统平台:
CentOS Linux release 7.4.1708 (Core)       内核  3.10.0-693.el7.x86_64

实验环境:VMware Workstation Pro 14(试用版)
系统平台:
CentOS release 6.9 (Final)             内核  2.6.32-696.el6.x86_64
iptables v1.4.7

网络拓扑图

技术分享图片

iptables实现共享上网(SNAT)

有固定公网IP

在192.168.7.202开启路由转发

# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf 
# sysctl -p
net.ipv4.ip_forward = 1

iptables 默认设置如下:
技术分享图片

在192.168.7.201检测

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.7.202   0.0.0.0         UG    0      0        0 eth0

访问内网正常
# curl 7-web-3
7-web-2
IP:192.168.7.203

# ping 192.168.7.202

64 bytes from 192.168.7.202: icmp_seq=1 ttl=64 time=1.06 ms
64 bytes from 192.168.7.202: icmp_seq=2 ttl=64 time=0.461 ms

访问外网被拒绝
# ping www.baidu.com
From 6-web-2.hunk.tech (192.168.7.202) icmp_seq=1 Destination Host Prohibited
From 6-web-2.hunk.tech (192.168.7.202) icmp_seq=2 Destination Host Prohibited

在 192.168.7.202配置

# iptables -F FORWARD
# service iptables save
# iptables -t nat -I POSTROUTING -s 192.168.7.0/24 ! -d 192.168.7.0/24 -j SNAT --to-source 172.18.44.202
# 这一条命令等同上一条,注意是出口的网卡iptables -t nat -I POSTROUTING 1 -s 192.168.7.0/24 -o eth1 -j SNAT --to-source 172.18.44.202
# iptables -t nat -vnL
Chain POSTROUTING (policy ACCEPT 1 packets, 120 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    4   312 SNAT       all  --  *      *       192.168.7.0/24      !192.168.7.0/24      to:172.18.44.202 

在192.168.7.201检测

# ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=55 time=37.5 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=55 time=29.3 ms
可以访问外网了

无固定公网IP

1.方法1:将多个公网IP地址都分配给NAT
# iptables -t nat -R POSTROUTING 1 -s 192.168.7.0/24 ! -d 192.168.7.0/24 -j SNAT --to-source 172.18.44.201-172.18.44.202

2.方法2:使用-j MASQUERADE
# iptables -t nat -R POSTROUTING 1 -s 192.168.7.0/24 -o eth1 -j MASQUERADE
# iptables -t nat -vnL

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      eth1    192.168.7.0/24       0.0.0.0/0

发布内网服务器(DNAT)

在公网上的IP是无法得到内网服务器的服务的

# curl 172.18.44.202
curl: (7) Failed connect to 172.18.44.202:80; No route to host

在 192.168.7.202配置

# iptables -t nat -A PREROUTING -d 172.18.44.202 -p tcp --dport 80 -j DNAT --to-destination 192.168.7.203:80

在公网的IP上访问

# curl http://172.18.44.202
7-web-2
IP:192.168.7.203

REDIRECT 端口转发

通过改变目标IP和端口,将接受的包转发至本机不同端口

功能实现在 PREROUTING OUTPUT 自定义链

--to-ports port[-port]
# iptables -t nat -A PREROUTING -d 192.168.7.203 -p tcp --dport 223 -j REDIRECT --to-ports 80
访问本机的223端口将会被重定向到80端口

iptables (三)

标签:iptables   NAT   SNAT   DNAT   端口转发   

原文地址:http://blog.51cto.com/191226139/2089189

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