NAT 技术使得一个私有网络可以通过Internet 注册IP 连接到外部世界,位于Inside 网络和Outside 网络中的NAT 路由器在发送数据包之前,负责把内部IP 地址翻译成外部合法IP 地址。NAT 将每个局域网节点的IP 地址转换成一个合法IP 地址,反之亦然。它也可以应用到防火墙技术里,把个别IP 地址隐藏起来不被外界发现,对内部网络设备起到保护的作用,同时,它还帮助网络可以超越地址的限制,合理地安排网络中的公有Internet 地址和私有IP 地址的使用。
静态NAT
静态NAT 中,内部网络中的每个主机都被永久映射成外部网络中的某个合法的地址。静态地址转换将内部本地地址与内部合法地址进行一对一的转换,且需要指定和哪个合法地址进行转换。如果内部网络有E-mail 服务器或FTP 服务器等可以为外部用户提供的服务,这些服务器的IP 地址必须采用静态地址转换,以便外部用户可以使用这些服务。
实验网络拓扑图:
Router>
Router>enable
Router#config terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#hostname R1
R1(config)#int f0/1
R1(config-if)#ip address 192.168.1.1 255.255.255.0
R1(config-if)#no shut
R1(config-if)#int s0/0/0
R1(config-if)#ip address 202.96.1.1 255.255.255.0
R1(config-if)#clock rate 128000
R1(config-if)#no shut
R1(config-if)#exit
R1(config)#route rip
R1(config-router)#version 2
R1(config-router)#no auto-summary
R1(config-router)#network 202.96.1.0
R1(config-router)#exit
R1(config)#ip nat inside source static 192.168.1.3 202.96.1.3
//将内部网络地址192.168.1.3转换为合法IP地址202.96
R1(config)#ip nat inside source static 192.168.1.4 202.96.1.4
R1(config)#int f0/1
R1(config-if)#ip nat inside
R1(config-if)#int s0/0/0
R1(config-if)#ip nat outside
Router>enable
Router#config terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#hostname R2
R2(config)#int s0/0/0
R2(config-if)#ip address 202.96.1.2 255.255.255.0
R2(config-if)#clock rate 128000
R2(config-if)#no shut
R2(config)#int loopback 0
R2(config-if)#ip address 2.2.2.2 255.255.255.0
R1(config-if)#exit
R1(config)#route rip
R1(config-router)#version 2
R1(config-router)#no auto-summary
R1(config-router)#network 202.96.1.0
在PC1上使用PING 命令PING 2.2.2.2
R1#debug ip nat //查看NAT转换过程
R1#
NAT: s=192.168.1.4->202.96.1.4, d=2.2.2.2 [26]
NAT*: s=2.2.2.2, d=202.96.1.4->192.168.1.4 [44]
NAT: s=192.168.1.4->202.96.1.4, d=2.2.2.2 [27]
NAT*: s=2.2.2.2, d=202.96.1.4->192.168.1.4 [45]
R1#show ip nat translations //查看静态转换表
Pro Inside global Inside local Outside local Outside global
--- 202.96.1.3 192.168.1.3 --- ---
--- 202.96.1.4 192.168.1.4 --- ---
动态NAT
动态NAT 首先要定义合法地址池,然后采用动态分配的方法映射到内部网络。动态NAT是动态一对一的映射。
R1>
R1>enable
R1#config terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#no ip nat
R1(config)#no ip nat inside source static 192.168.1.3 202.96.1.3 //删除静态nat
ipnat_remove_static_cfg: id 3, flag A
R1(config)#no ip nat inside source static 192.168.1.4 202.96.1.4
ipnat_remove_static_cfg: id 4, flag A
R1(config)#
R1(config)#ip nat pool nat 202.96.1.3 202.96.1.100 netmask 255.255.255.0 //配置动态NAT;指明地址缓冲池的名称为nat,IP地址范围为202.96.1.3~202.96.1.100,子网掩码为255.255.255.0。需要注意 的是,即使掩码为255.255.255.0,也会由起始IP地址和终 止IP地址对IP地址池进行限制。
或ip nat pool nat 202.96.1.3 202.96.1.100 prefix-length 24//改的是子网掩码的位数
R1(config)#ip nat inside source list 1 pool nat
//将由access-list指定的内部本地地址与指定的内部合法地址池进行地址转换
ipnat_add_dynamic_cfg: id 1, flag 5, range 0
id 1, flags 0, domain 0, lookup 0, aclnum 1 ,
aclname 1 , mapname idb 0
R1(config)#access-list 1 permit 192.168.1.0 0.0.0.255
//允许访问Internet的网段为192.168.1.0~192.168.1.255,反掩码为0.0.0.255。需要
注意的是,在这里采用的是反掩码,而非子网掩码。
R1(config)#interface f0/1
R1(config-if)#ip nat inside
R1(config-if)#int s0/0/0
R1(config-if)#ip nat outside
R1#debug IP NAT
IP NAT debugging is on
R1#
NAT: s=192.168.1.3->202.96.1.3, d=2.2.2.2 [13]
NAT*: s=2.2.2.2, d=202.96.1.3->192.168.1.3 [320]
NAT: s=192.168.1.3->202.96.1.3, d=2.2.2.2 [14]
NAT*: s=2.2.2.2, d=202.96.1.3->192.168.1.3 [321]
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 202.96.1.3:13 192.168.1.3:13 2.2.2.2:13 2.2.2.2:13
icmp 202.96.1.3:14 192.168.1.3:14 2.2.2.2:14 2.2.2.2:14
icmp 202.96.1.3:15 192.168.1.3:15 2.2.2.2:15 2.2.2.2:15
icmp 202.96.1.3:16 192.168.1.3:16 2.2.2.2:16 2.2.2.2:16
Port端口多路复用
PAT 则是把内部地址映射到外部网络的IP 地址的不同端口上,从而可以实现多对一的映射。PAT 对于节省IP 地址是最为有效的。
R1(config)#int f0/1
R1(config-if)#ip nat inside
R1(config-if)#int s0/0/0
R1(config-if)#ip nat outside
R1(config-if)#exit
R1(config)#ip nat pool natport 202.96.1.3 202.96.1.100 netmask 255.255.255.0
//指明地址缓冲池的名称为natport,IP地址范围为202.96.1.3—202.96.1.100 子网掩码为255.255.255.0
R1(config)#ip nat inside source list 1 pool natport overload
//以端口复用方式,将访问列表1中的私有IP地址转换为natport IP地址池中定义的合法IP地址。overload是复用动态地址转换的关键词
R1(config)#access-list 1 permit 192.168.1.0 0.0.0.255
R1(config)#end
R1#debug ip nat
IP NAT debugging is on
R1#
NAT: s=192.168.1.3->202.96.1.3, d=2.2.2.2 [9]
NAT*: s=2.2.2.2, d=202.96.1.3->192.168.1.3 [36]
NAT: s=192.168.1.3->202.96.1.3, d=2.2.2.2 [10]
NAT*: s=2.2.2.2, d=202.96.1.3->192.168.1.3 [37]
动态NAT 的过期时间是86400 秒,PAT 的过期时间是60 秒,通过命令“show ip nat
translations verbose”可以查看。也可以通过下面的命令来修改超时时间:
R1(config)#ip nat translation timeout timeout //此模拟器无法修改
参数timeout 的范围是0-2147483。
如果主机的数量不是很多, 可以直接使用outside 接口地址配置PAT,不必定义地址池,
命令如下:
R1(config)#ip nat inside source list 1 interface s0/0/0 overload
本文出自 “让梦飞翔” 博客