设为首页 收藏本站
查看: 334|回复: 0

[经验分享] iptables SNAT/DNAT测试

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-16 08:42:38 | 显示全部楼层 |阅读模式
环境:CentOS 6.4(最小化安装)


SNAT测试:
wKioL1WmEnLQor5VAABxxrg6Fyg657.jpg

地址规划:
Node
IP address
GW
EndUser
192.168.101.241/24
192.168.101.1
iptables
eth1:192.168.101.1/24
eth0:192.168.100.242/24
192.168.100.1
EndUser:
1
2
3
4
5
6
7
[iyunv@EndUser ~]# tail -n 5 /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=static
IPADDR=192.168.101.241
NETMASK=255.255.255.0
GATEWAY=192.168.101.1
DNS1=114.114.114.114
[iyunv@EndUser ~]#



测试网关:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@EndUser ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.101.0   *               255.255.255.0   U     0      0        0 eth0
link-local      *               255.255.0.0     U     1002   0        0 eth0
default         192.168.101.1   0.0.0.0         UG    0      0        0 eth0
[iyunv@EndUser ~]# ping 192.168.101.1 -c 4
PING 192.168.101.1 (192.168.101.1) 56(84) bytes of data.
64 bytes from 192.168.101.1: icmp_seq=1 ttl=64 time=0.315 ms
64 bytes from 192.168.101.1: icmp_seq=2 ttl=64 time=0.340 ms
64 bytes from 192.168.101.1: icmp_seq=3 ttl=64 time=0.320 ms
64 bytes from 192.168.101.1: icmp_seq=4 ttl=64 time=0.317 ms
--- 192.168.101.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.315/0.323/0.340/0.010 ms
[iyunv@EndUser ~]# ping 192.168.100.242 -c 4
PING 192.168.100.242 (192.168.100.242) 56(84) bytes of data.
64 bytes from 192.168.100.242: icmp_seq=1 ttl=64 time=0.557 ms
64 bytes from 192.168.100.242: icmp_seq=2 ttl=64 time=0.339 ms
64 bytes from 192.168.100.242: icmp_seq=3 ttl=64 time=0.319 ms
64 bytes from 192.168.100.242: icmp_seq=4 ttl=64 time=0.400 ms
--- 192.168.100.242 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.319/0.403/0.557/0.096 ms
[iyunv@EndUser ~]#



iptables:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[iyunv@iptables ~]# tail -n 5 /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=static
IPADDR=192.168.100.242
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=114.114.114.114
[iyunv@iptables ~]# tail -n 4 /etc/sysconfig/network-scripts/ifcfg-eth1
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.101.1
NETMASK=255.255.255.0
[iyunv@iptables ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   *               255.255.255.0   U     0      0        0 eth0
192.168.101.0   *               255.255.255.0   U     0      0        0 eth1
link-local      *               255.255.0.0     U     1002   0        0 eth0
link-local      *               255.255.0.0     U     1003   0        0 eth1
default         192.168.100.1   0.0.0.0         UG    0      0        0 eth0
[iyunv@iptables ~]#
[iyunv@iptables ~]# grep forward /etc/sysctl.conf |grep -v ^#
net.ipv4.ip_forward = 1
[iyunv@iptables ~]# sysctl -p
[iyunv@iptables ~]# iptables -t nat -vL          //查看linux下NAT映射表



wKiom1WmEO2zpfTcAAJdrFxb4XI796.jpg
1
2
3
4
5
6
7
8
9
[iyunv@iptables ~]# grep "POSTROUTING" /etc/sysconfig/iptables|grep -v ^:
-A POSTROUTING -s 192.168.101.0/24 -o eth0 -j SNAT --to-source 192.168.100.242
[iyunv@iptables ~]#
[iyunv@iptables ~]# /etc/init.d/iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter nat      [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[iyunv@iptables ~]#



先测试防火墙能否上网:
1
2
3
4
5
6
7
8
9
10
[iyunv@iptables ~]# ping www.baidu.com -c 4
PING www.a.shifen.com (180.97.33.107) 56(84) bytes of data.
64 bytes from 180.97.33.107: icmp_seq=1 ttl=53 time=27.3 ms
64 bytes from 180.97.33.107: icmp_seq=2 ttl=53 time=26.5 ms
64 bytes from 180.97.33.107: icmp_seq=3 ttl=53 time=25.2 ms
64 bytes from 180.97.33.107: icmp_seq=4 ttl=53 time=24.9 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3031ms
rtt min/avg/max/mdev = 24.994/26.047/27.371/0.977 ms
[iyunv@iptables ~]#



测试EndUser:
wKioL1WmEyKyvYMQAAJT3Nlyx2I925.jpg
ping一个windows主机用wireshark抓包:
wKiom1WmEXbwTeetAAi4vSHJDYU338.jpg


DNAT测试:
wKioL1WmE3vTDQv1AACGPeWtyOA861.jpg

地址规划:
Node
IP Address
GW
WEB
192.168.101.241/24
192.168.101.1
iptables
eth0:192.168.100.242/24
eth1:192.168.101.1/24
192.168.100.243
192.168.101.241
EndUser
192.168.100.251/24
      -

WEB:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[iyunv@WEB ~]# tail -n 4 /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=static
IPADDR=192.168.101.241
NETMASK=255.255.255.0
GATEWAY=192.168.101.1
[iyunv@WEB ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.101.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.101.1   0.0.0.0         UG    0      0        0 eth0
[iyunv@WEB ~]#
[iyunv@WEB ~]# ping 192.168.100.242 -c 4
PING 192.168.100.242 (192.168.100.242) 56(84) bytes of data.
64 bytes from 192.168.100.242: icmp_seq=1 ttl=64 time=0.287 ms
64 bytes from 192.168.100.242: icmp_seq=2 ttl=64 time=0.346 ms
64 bytes from 192.168.100.242: icmp_seq=3 ttl=64 time=0.403 ms
64 bytes from 192.168.100.242: icmp_seq=4 ttl=64 time=0.409 ms
--- 192.168.100.242 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.287/0.361/0.409/0.051 ms
[iyunv@WEB ~]#



iptables:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[iyunv@iptables ~]# grep forward /etc/sysctl.conf |grep -v ^#
net.ipv4.ip_forward = 1
[iyunv@iptables ~]#
[iyunv@iptables ~]# tail -n 4 /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=static
IPADDR=192.168.100.242
NETMASK=255.255.255.0
GATEWAY=192.168.100.243
[iyunv@iptables ~]# tail -n 4 /etc/sysconfig/network-scripts/ifcfg-eth1
BOOTPROTO=static
IPADDR=192.168.101.1
NETMASK=255.255.255.0
GATEWAY=192.168.101.241
[iyunv@iptables ~]#
[iyunv@iptables ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.101.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         192.168.101.241 0.0.0.0         UG    0      0        0 eth1
[iyunv@iptables ~]#
[iyunv@iptables ~]# iptables -t nat -A PREROUTING -d 192.168.100.242 -p tcp -m tcp --dport 80 -i eth0 -j DNAT --to-destination 192.168.101.241



wKioL1WmE8iTxEI6AADg_JLBz00040.jpg
1
2
3
4
5
6
[iyunv@iptables ~]# /etc/init.d/iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter nat      [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[iyunv@iptables ~]#



访问:http://192.168.100.242/

wKioL1WmE_7B8dgUAACSJQ26tHM579.jpg
wKiom1WmEiXgLHEIAAZYyfwPm-I961.jpg



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-87069-1-1.html 上篇帖子: Linux下搭建SVN+版本冲突 下篇帖子: Linux中 grep/egrep的命令收集
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表