NAT技术详解及配置实例

NAT作为一种减轻IPv4地址空间耗尽速度的方法,最早出现在Cisco IOS 11.2版本中。  

为什么要使用 NAT
1 内网中主机过多,没有足够的合法 IP 地址可用。
2 ISP 发生变化时,使用 NAT 技术避免了 IP 地址的重新编址。
3 当两个合并的网络中出现了重复地址的时候。
4 利用 NAT 来解决 TCP 的负载均衡问题。
5 隐藏内部网络,增强安全性。
 
NAT 就是将内网中使用的私有地址转换成可在 Internet 上进行路由的合法地址的技术。
私有地址范围:
10.0.0.0 ~ 10.255.255.255
172.16.0.0 ~ 172..31.255.255
192.168.0.0 ~ 192.168.255.255
 
NAT 技术主要分为 NAT PAT
NAT 是从内部本地地址到内部全局地址的一对一转换。
PAT 是从多个内部本地地址到内部全局地址的多对一转换。通过端口号确定其多个内部主机的唯一性。
 
NAT 术语
Inside network :需要翻译成外部地址的内部网络。
Outside network :使用合法地址进行通信的外部网络。
Local address :内部网络使用的地址。
Global address :外部网络使用的地址。
Inside local address :内部本地地址。数据在内部网络使用的地址,一般为 private ip address
Inside global address :内部全局地址。数据为了到达外部网络,用来代表 inside local address 的地址,一般为 ISP 提供的合法地址。
Outside local address :外部本地地址,不必是合法地址。当外部网络数据到达内部网络,外部网络中的主机 IP 地址与内部网络中的主机处在同一网段时,为防止内部主机误认外部主机与自己在同一网段而广播 ARP 请求,造成无法通信,将外部主机的地址转换成外部本地地址之后再与内部主机进行通信。
Outside global address :数据在外部网络使用的地址,是个合法地址。
Simple translation entry :把一个 IP 地址映射到另外一个地址上的一对一的翻译方式。
Extended translation entry :把 IP 地址和端口( port )的组合翻译成另外一个地址和端口的组合。
Static address translation :静态地址翻译,把一个 local 对应到一个 global 上去。
Dynamic address translation :动态翻译, local global 池( pool )建立动态对应关系。
Port address translation PAT ):通过使用地址和端口的结合来达到多个 local 对应一个 global 的状态。端口号用来确定每个 local 的唯一性。这样的技术也叫超载( overloading )。
 
NAT 的优缺点
优点:
1 极大的节省了合法的 IP 地址。
2 能够处理地址重复情况,避免了地址的重新编号,增加了编址的灵活性。
3 隐藏了内部网络地址,增强了安全性。
4 可以使多个使用 TCP 负载特性的服务器之间实现基本的数据包负载均衡。
缺点:
1 由于 NAT 要在边界路由器上进行地址的转换,增大了传输的延迟。
2 由于 NAT 改动了 IP 地址,失去了跟踪端到端 IP 流量的能力。当出现恶意流量时,会使故障排除和流量跟踪变的更加棘手。
3 不支持一些特定的应用程序。如早期版本的 MSN
4 增大了资源开销。处理 NAT 进程增加了 CPU 的负荷,并需要更多内存来存储 NAT 表项。
 
配置 NAT
inside & outside
每个包含了 NAT 进程的接口必须被指定为内部接口或外部接口,但不能被同时指定为这两个接口。在路由器上,必须至少有一个接口被配置成内部接口,一个接口被配置成外部接口。这样路由器就知道怎样在接口上处理入站和出站流量了。
D-Lab(config)#int e0    // 进入接口模式
D-Lab(config-if)#ip nat inside   // 指定 inside 接口 出站
D-Lab(config-if)#int s0
D-Lab(config-if)#ip nat outside // 指定 outside 接口 入站
 
静态 NAT
inside|outside local inside|outside global 的一对一映射
D-Lab(config)#ip nat {inside|outside} source static < inside local> < inside global>
这样就在 NAT 表中创建了一个永久表项。
配置示例
D-Lab#config t
D-Lab(config)#int e0
D-Lab(config-if)#ip nat inside
D-Lab(config-if)#int s0
D-Lab(config-if)#ip nat outside
D-Lab(config)#ip nat inside source static 192.168.1.1 210.83.202.1
D-Lab(config)#ip nat outside source static 210.83.202.2 192.168.2.3
 
静态 PAT
D-Lab(config)#ip nat {inside|outside} source static < protocol> < inside local> < port> < inside global> < port>
配置示例:
D-Lab(config)#ip nat inside source static tcp 10.1.1.1 80 214.118.21.121 80
 
动态 NAT
动态 NAT 用来将 inside local 快速映射到 inside global 上。这些 inside local 放在可用的 IP 地址池中。同样,必须给参与 NAT 进程的路由器接口分配 IP 地址。
当内部网络主机要与 Internet 上的主机进行通信时, NAT 边界路由器会从标记为 “NAT 内部 的接口接收数据包。
D-Lab(config)#ip nat pool <pool-name> <start-ip> <end-ip> netmask xxxx
D-Lab(config)#ip nat pool < pool-name> <start-ip> <end-ip> prefix-length xx     // 掩码长度
例:
D-Lab(config)#ip nat pool dyn-nat-pool 211.10.121.1 211.10.121.254 netmask 255.255.255.0
D-Lab(config)#ip nat pool dyn-nat-pool 211.10.121.1 211.10.121.254 prefix-length 24
配置示例:
D-Lab(config)#inter e0 
D-Lab(config-if)#ip add 192.168.1.1 255.255.255.0     
D-Lab(config-if)#ip nat inside     // 指定内部接口
D-Lab(config-if)#inter s0
D-Lab(config-if)#ip add 210.83.202.1 255.255.255.192
D-Lab(config-if)#ip nat outside    // 指定外部接口
D-Lab(config-if)#exit
D-Lab(config)#access-list 1 permit 192.168.1.0 0.0.0.255      // 标记 inside local 范围
D-Lab(config)#ip nat pool in-out 210.83.202.2 210.83.202.8 prefix 24     // 设置 inside global 地址池 
!
D-Lab(config)#ip nat inside source list 1 pool in-out     // 启用 inside 源地址翻译,把标记的 inside local 范围与 inside global 池关联起来
 
动态 PAT (多对一映射)
路由器为每个转换表项添加第四层协议和端口信息
D-Lab(config)#ip nat inside source list 1 pool in-out overload
如果不知道出站 IP 地址,可在命令中指定出站接口
D-Lab(config)#ip nat inside source list 1 e0 overload
// 如果接口 shut 或者接口没有设置 IP 地址的话 ,NAT 不会生效
配置实例:
D-Lab(config)#inter e0
D-Lab(config-if)#ip add 192.168.1.1 255.255.255.0
D-Lab(config-if)#ip nat inside
D-Lab(config)#inter s0
D-Lab(config-if)#ip add 210.83.202.1 255.255.255.192
D-Lab(config-if)#ip nat outside
!
!
!
D-Lab(config)#ip nat pool test 210.83.202.2 210.83.202.8 prefix 24
!
D-Lab(config)#ip nat inside source list 9 pool test overload
D-Lab(config)#access-list 9 permit 192.168.1.0 0.0.0.255
 
为重复地址配置 NAT
D-Lab(config)#ip nat pool in-out 211.121.1.1 211.121.1.254 prefix-length 24    // 设置 local global
D-Lab(config)#ip nat pool out-in 10.1.2.1 10.1.2.254 prefix-length 24   // 设置 outside local
D-Lab(config)#ip nat inside source list 1 pool in-out     // 启用 inside 源地址翻译,把标记的 inside local 范围与 inside global 池关联起来
D-Lab(config)#ip nat inside source list 1 pool out-in    // 将在标记的 inside local 范围里的 outside global 转换成 outside local 池里的地址
D-Lab(config)#int fa0/0
D-Lab(config-if)#ip nat inside    // 指定出站接口
D-Lab(config-if)#int s1/1
D-Lab(config-if)#ip nat outside    // 指定入站接口
D-Lab(config-if)#exit
D-Lab(config)#access-list 1 permit 10.1.1.0 0.0.0.255    // 标记 inside local 地址范围
 
更灵活的地址池的配置:
ip nat pool <name> { netmask <mask> | prefix-length <length> } [ type { rotary } ]
这样可以允许定义不连续地址池 , 接下来定义地址空间:
address <start> <end>
例:
D-Lab(config)#ip nat pool D-lab prefix-length 24
D-Lab(config-ipnat-pool)#address 11.69.73.12 11.69.73.14
D-Lab(config-ipnat-pool)#address 11.69.73.18 11.69.73.244
这样就定义了一个 11.69.73.12 ~ 11.69.73.14 11.69.73.18~11.69.73.244 的地址池
 
NAT 关联 routemap 的配置:
ip nat inside source route-map <name> pool <name>
例:
ip nat pool d-1 173.26.232.1 173.26.232.254 prefix-length 24
ip nat pool d-2 24.10.12.1 24.10.12.254 prefix-length 24
ip nat inside source route-map d1-map pool d-1
ip nat inside source route-map d2-map pool d-2
!
interface Serial0/0
ip nat outside
!
interface Serial0/1
ip nat outside
!
route-map d1-map permit 10
match ip address 1
match interface Serial0/0
!
route-map d2-map permit 10
match ip address 1
match interface Serial0/1
 
配置 NAT 超时设置:
ip nat translation timeout <seconds>    // 特权模式下
 
 
NAT 配置的验证和故障排除
sh ip nat translations    // 查看生效的 NAT 表项
sh ip nat translations verbose    // 查看生效的 NAT 表项的详细信息
sh ip nat statistics    // 显示 NAT 的统计数字和配置信息
 
clear ip nat translation *    // 清除所有 NAT 表项
clear ip nat translation inside <global-ip>    // 清除指定的内部 NAT 表项 :
clear ip nat translation <protocol> <global-ip> <local-ip> <global-port> <local-port>    // 清除指定的 NAT 表项
 
debug ip nat [ <list> ] [ detailed ]    // 调试 NAT 进程
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值