Chinaunix首页 | 论坛 | 博客
  • 博客访问: 352374
  • 博文数量: 42
  • 博客积分: 1984
  • 博客等级: 上尉
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-30 14:40
文章分类

全部博文(42)

文章存档

2016年(11)

2014年(3)

2013年(7)

2012年(3)

2011年(8)

2010年(10)

分类:

2012-04-25 13:37:37

NAT包括多种不同的类型,并可用于不同目的:
静态NAT:按照一一对应的方式将每个内部IP地址转换为一个外部IP地址,这种方式经常用于企业网的内部设备能够被外部网络访问。
动态NAT:将一个内部IP地址转换为一组外部IP地址(地址池)中的一个IP地址。
超载(Overloading)NAT:动态NAT的一种实现形式,利用不同的端口号将多个内部IP地址转换为一个外部IP地址,也称为PAT、NAPT或端口复用NAT,是目前最常用的转换方式。
此外,NAT还可以用来解决地址重叠问题,并可以用于进行TCP负载均衡。
本文旨在学习静态NAT及动态NAT的配置。
 
 
实验环境
Dynamips模拟器
路由器
Cisco IOS Software, 3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(4)T, RELEASE SOFTWARE (fc1)
交换机
Cisco IOS Software, 2600 Software (C2691-ADVSECURITYK9-M), Version 12.4(11)T2, RELEASE SOFTWARE (fc4)
 
 
拓扑结构
如下图所示, 四台路由器,一台交换机。
其中,R3、R4别模拟pc1、svr1,作为内网终端。
R2是内网路由器,R3是公网上的路由器。
交换机SW1在这里仅使用了二层功能,连好接口就行了,不需要作任何配置。
 

 
操作过程如下:
 
基本配置
 
Router(config)#host pc1
pc1 (config)#int f1/1
pc1 (config-if)#no switchport 
pc1 (config-if)#ip add 192.168.12.11 255.255.255.0
pc1 (config-if)#no shut
pc1 (config-if)#exit
pc1 (config)#no ip routing   !关闭路由功能,使其模拟客户端
pc1 (config)#ip default-gateway 192.168.12.2
pc1 (config)#
--------------------------------
Router(config)#host svr1
svr1(config)#int f1/1 
svr1(config-if)#no switchport
svr1(config-if)#ip add 192.168.12.22 255.255.255.0
svr1(config-if)#no shut
svr1(config-if)#exit
svr1(config)#no ip routing !关闭路由功能,使其模拟服务器
svr1(config)#ip default-gateway 192.168.12.2
svr1(config)#
svr1(config)#enable password cisco   !设置登录密码及可被远程访问
svr1(config)#line vty 0 4
svr1(config-line)#password ciscovty
svr1(config-line)#login 
svr1(config-line)#exit
svr1(config)#
--------------------------------
Router(config)#host SW1
SW1(config)#
--------------------------------
Router(config)#host R2
R2(config)#int f1/1
R2(config-if)#no switchport 
R2(config-if)#ip add 192.168.12.2 255.255.255.0
R2(config-if)#no shut
R2(config-if)#exit
R2(config)#int s3/1
R2(config-if)#ip add 23.1.1.2 255.255.255.252
R2(config-if)#clock rate 64000
R2(config-if)#no shut
R2(config-if)#exit
R2(config)#ip route 0.0.0.0 0.0.0.0 s3/1  !默认路由
R2(config)#
--------------------------------
Router(config)#host R1
R1(config)#int s3/1
R1(config-if)#ip add 23.1.1.1 255.255.255.252
R1(config-if)#no shut
R1(config-if)#exit
R1(config)#
-----------------------------------------------------------------------------
 
 
场景一:配置动态NAT
 
在R2上进行操作:
R2(config)#
R2(config)#int f1/1
R2(config-if)#ip nat inside
R2(config-if)#exit
R2(config)#int s3/1
R2(config-if)#ip nat outside
R2(config-if)#exit
R2(config)#ip nat pool dynamic-p1 23.1.1.2 23.1.1.2 netmask 255.255.255.252 !创建地址池
R2(config)#access-list 1 permit 192.168.12.0 0.0.0.255 !创建访问控制列表
R2(config)#ip nat inside source list 1 pool dynamic-p1 !应用地址池
 
(注:因为本例只有一个外部全局ip地址,所以在创建地址池的时候,开始IP地址和结束IP地址都写的是23.1.1.2,如果是现实中有多个外部全局IP地址,那就要分别指定开始IP地址和结束IP地址了。)
动态地址转换是在从内部全局地址池中动态地选择一个未被使用的地址。在本实验中,如上述配置所示,由于内部全局地址只有23.1.1.2一个,而内部本地地址有两个,因此哪一台终端首先连接外网,那它就一直可以访问外网,其他的终端不可以。
 
Ping测试如下:
 
在pc1上ping R1
pc1#ping 23.1.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 23.1.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 12/32/96 ms
--------------------------------
在svr1上ping R1
svr1#ping 23.1.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 23.1.1.1, timeout is 2 seconds:
U.U.U
Success rate is 0 percent (0/5)
--------------------------------
在R2上查看NAT运行的状态
R2#show ip nat translations
Pro Inside global      Inside local       Outside local      Outside global
icmp 23.1.1.2:6        192.168.12.11:6    23.1.1.1:6         23.1.1.1:6
--- 23.1.1.2           192.168.12.11      ---                ---
--------------------------------
使用debug命令对NAT的转换操作进行调试:
R2#debug ip nat
IP NAT debugging is on
 
然后在pc1和svr1上再分别ping R1,R2上提示如下:
R2#             
*Mar  1 00:25:32.999: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [40]
*Mar  1 00:25:33.035: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [40]
*Mar  1 00:25:33.063: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [41]
*Mar  1 00:25:33.099: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [41]
*Mar  1 00:25:33.111: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [42]
*Mar  1 00:25:33.119: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [42]
*Mar  1 00:25:33.123: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [43]
*Mar  1 00:25:33.147: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [43]
*Mar  1 00:25:33.151: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [44]
R2#
*Mar  1 00:25:33.171: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [44]
R2#
*Mar  1 00:25:35.923: NAT: translation failed (A), dropping packet s=192.168.12.22 d=23.1.1.1
*Mar  1 00:25:35.971: NAT: translation failed (A), dropping packet s=192.168.12.22 d=23.1.1.1
R2#
*Mar  1 00:25:37.959: NAT: translation failed (A), dropping packet s=192.168.12.22 d=23.1.1.1
*Mar  1 00:25:38.007: NAT: translation failed (A), dropping packet s=192.168.12.22 d=23.1.1.1
R2#
*Mar  1 00:25:40.011: NAT: translation failed (A), dropping packet s=192.168.12.22 d=23.1.1.1
R2#
*Mar  1 00:25:53.795: NAT: expiring 23.1.1.2 (192.168.12.11) icmp 7 (7)
R2#
*Mar  1 00:26:33.219: NAT: expiring 23.1.1.2 (192.168.12.11) icmp 8 (8)
R2#
--------------------------------------
清空R2上的所有转换条目
R2#clear ip nat translation * 
--------------------------------
此时,若svr1首先ping R1,则svr1可以一直连接外网,而pc1则不能,与上述情况相反。
 
调试完成后,在R2上关闭操作调试功能
R2#no debug ip nat
IP NAT debugging is off
-----------------------------------------------------------------------------
 
 
场景二:配置动态NAPT
 
在R2上进行操作:
R2(config)#
R2(config)#int f1/1
R2(config-if)#ip nat inside
R2(config-if)#exit
R2(config)#int s3/1
R2(config-if)#ip nat outside
R2(config-if)#exit
R2(config)#ip nat pool dynamic-p1 23.1.1.2 23.1.1.2 netmask 255.255.255.252
R2(config)#access-list 1 permit 192.168.12.0 0.0.0.255
R2(config)#ip nat inside source list 1 pool dynamic-p1 overload
 
其实就是将场景一中的ip nat inside source list 1 pool dynamic-p1删除,
改为执行ip nat inside source list 1 pool dynamic-p1 overload命令。
 
在R2上查看当前的转换条目
R2#show ip nat translations 
 
此时,条目数为零。
(鉴于场景一是在同一平台上进行操作,可能存在已有的转换条目,这里执行以下命令清空,
R2#clear ip nat translation *
如果R2有较长时间(多少秒?)没有执行NAT操作,也会自行清空之前的转换条目)
--------------------------------
在R2上开启操作调试功能
R2#debug ip nat
IP NAT debugging is on
 
然后在pc1和svr1上分别ping R1,两者都是可以ping通的。
R2上提示如下:
R2#
*Mar  1 00:51:02.647: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [60]
*Mar  1 00:51:02.715: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [60]
*Mar  1 00:51:02.743: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [61]
*Mar  1 00:51:02.755: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [61]
*Mar  1 00:51:02.767: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [62]
*Mar  1 00:51:02.775: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [62]
*Mar  1 00:51:02.783: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [63]
*Mar  1 00:51:02.819: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [63]
*Mar  1 00:51:02.827: NAT*: s=192.168.12.11->23.1.1.2, d=23.1.1.1 [64]
R2#
*Mar  1 00:51:02.835: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.11 [64]
R2#
*Mar  1 00:51:06.051: NAT*: s=192.168.12.22->23.1.1.2, d=23.1.1.1 [65]
*Mar  1 00:51:06.103: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.22 [65]
*Mar  1 00:51:06.127: NAT*: s=192.168.12.22->23.1.1.2, d=23.1.1.1 [66]
*Mar  1 00:51:06.135: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.22 [66]
*Mar  1 00:51:06.155: NAT*: s=192.168.12.22->23.1.1.2, d=23.1.1.1 [67]
*Mar  1 00:51:06.159: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.22 [67]
*Mar  1 00:51:06.167: NAT*: s=192.168.12.22->23.1.1.2, d=23.1.1.1 [68]
*Mar  1 00:51:06.175: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.22 [68]
*Mar  1 00:51:06.179: NAT*: s=192.168.12.22->23.1.1.2, d=23.1.1.1 [69]
R2#
*Mar  1 00:51:06.187: NAT*: s=23.1.1.1, d=23.1.1.2->192.168.12.22 [69]
 
再在R2上查看当前的转换条目
R2#show ip nat translations 
Pro Inside global      Inside local       Outside local      Outside global
icmp 23.1.1.2:11       192.168.12.11:11   23.1.1.1:11        23.1.1.1:11
icmp 23.1.1.2:12       192.168.12.22:12   23.1.1.1:12        23.1.1.1:12
-----------------------------------------------------------------------------
 
 
场景三 配置静态NAT
 
现在要求内网中的服务器svr1能够被外部网络访问,即对外开放——本实验中,要求svr1可以被R1 远程访问。
 
在场景二的基础上,进行以下操作。
 
R2(config)#
R2(config)#ip nat pool static-p2 192.168.12.22 192.168.12.22 netmask 255.255.255.0
R2(config)#access-list 2 permit host 23.1.1.2
R2(config)#ip nat inside destination list 2 pool static-p2
% Pool static-p2 is not a rotary-type pool, unexpected behavior may result.
R2(config)#ip nat inside source static tcp 192.168.12.22 23 23.1.1.2 23
R2(config)#end
R2#
(这里的Pool static-p2 is not a rotary-type pool警告意味着什么,不甚清楚)
--------------------------------
在R2上查看当前的转换条目
R2#show ip nat translations 
Pro Inside global      Inside local       Outside local      Outside global
tcp 23.1.1.2:23        192.168.12.22:23   23.1.1.1:18327     23.1.1.1:18327
tcp 23.1.1.2:23        192.168.12.22:23   ---                ---
 
此时,应该能看两条固定的条目,使用clear ip nat translation * 命令无法清空。
(鉴于场景二上进行了操作,可能存在已有的转换条目,这里执行以下命令清空,
R2#clear ip nat translation *)
 
在R1上远程访问svr1,
R1#telnet 23.1.1.2     
Trying 23.1.1.2 ... Open
 
 
User Access Verification
 
Password: 
svr1>en
Password: 
svr1#
[Connection to 23.1.1.2 closed by foreign host]
R1#
--------------------------------
 
上述信息表明,R1在telnet R2的外网IP时,实际上访问的是svr1服务器,这就是配置静态NAT的结果。
 
再在pc1和svr1上分别ping R1,两者都是可以ping通的。
 
此时再在R2上查看当前的转换条目,发现多了icmp相关的条目,如果R2有较长没时间没有执行NAT操作,会自行清空icmp相关的条目。
R2#show ip nat translations 
Pro Inside global      Inside local       Outside local      Outside global
icmp 23.1.1.2:0        192.168.12.11:14   23.1.1.1:14        23.1.1.1:0
icmp 23.1.1.2:14       192.168.12.22:14   23.1.1.1:14        23.1.1.1:14
tcp 23.1.1.2:23        192.168.12.22:23   23.1.1.1:18327     23.1.1.1:18327
tcp 23.1.1.2:23        192.168.12.22:23   ---                ---
 
也可以通过debug命令对NAT的转换操作进行调试以查看详细的转换过程。
 
 

 

本文出自 “一树清劲” 博客,请务必保留此出处http://sunshyfangtian.blog.51cto.com/1405751/832460


阅读(1044) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~