接续上一节的实验,本节将正式进入NAT的配置设定环节。
本节内容是在上节内容的基础上进行的。
拓扑-NAT实施前
至此,大家应该已经可以看到路由器端(也就是逻辑上的ISP维护端)已全部打通;
但是,这个时候我们会发下另一个问题:
PC1或者PC3(PC2因为在R1上未配置到192.168.20.2/24网段的路由与PC1和PC3不同,这里暂不讨论);最多只能访问到接入路由器的出口分别是R1的f0/1和R3的f0/1。
实际上我们再现实的企业网中在内网(PC1和PC3所在的网络)机器是可以ping通ISP的路由的(部分);要实现这一点,我们有2中方法:
1.在R2(代表ISP内部所有路由器)上增加到企业内网所有网段(PC1-PC3所在的192.168.10.0/24、192.168.20.0/24、192.168.30.0/24)的路由条目。
2.在ISP接入出口(R2)上对内网网段做NAT。
显然,方法1在现实环境中实现起来根本不现实;且涉及到2家不同的企业极有可能使用的是相同的内网网段那么在ISP路由中就无法完成内网网段的路由表的维护(除非全球的企业内网都使用独一无二不重复的网段,那这样IPv4设计的ABC类地址将失去意义,且IPv4地址不足的问题也就没办法解决);
此时,NAT技术的出现就可以大大减轻这样的状况。
接下来,我们就按照不在R2上增加内网路由的形式实现PC1到R2甚至PC3的通讯。
我们从实际出发,首先需要保证PC-1和PC-3能正常访问internet(R2);这里我们就需要用到NAT中的动态NAT或者端口复用技术(主要是因为实际环境中内网设备众多),NAT技术的区别前文已经提到:
Ø静态NAT:唯一的私有IP------映射------唯一的公网IP(映射关系确定,永久保存)
Ø动态NAT:多个私有IP------映射------多个公网IP(映射关系不确定,向外访问时建立映射关系)
Ø端口复用:局域网中多个或全部私有IP------映射------1个公网IP(映射关系不确定,向外访问时建立
映射关系)
经验总结:
Ø静态转换(static translation)
将内部网络的私有IP地址转换为公有合法IP地址。IP地址的对应关系是一对一的,而且是不变的。
Ø动态转换(dynamic translation)
指将内部私有IP转换为公网IP地址时,IP的对应关系是不确定的。也就是说只要指定哪些内部地址可以进行NAT转换,以及哪些可以的合法的IP地址可
以作为外部地址,就可以进行动态转换了。也可以使用多个合法地址集。
Ø端口复用(port address translation,)PAT
改变外出数据包的源IP地址和源端口并进行端口转换,即端口地址转换采用端口多路复用的方式。内部网络的所有主机均可共享一个合法外部IP地址实现互联网的访问。最大程度上节约IP地址资源。
R1设置动态NAT
我们假设拓扑的左边和右边分别代表两家公司的网络:
我们先在R1上为左边内网(A公司)设置动态NAT;(多——>多)
需要映射的内网地址:192.168.1.0/24和192.168.10.5-10/24(拒绝192.168.10.11/24)映射的公网地址:1.1.1.5-6/24
然后在R3上为右边内网(B公司)设置端口复用:(多——>一)
需要映射的内网地址:all(192.168.2.0/24+192.168.30.0/24)映射的公网地址:2.1.1.5/24
R1:
##R1上为左边内网(A公司)设置动态NAT
R1#show ip nat translations
R1#
##定义访问控制列表,内部本地地址范围
R1#conf t
R1(config)#access-list 1 permit ?
Hostname or A.B.C.D Address to match
any Any source host
host A single host address
R1(config)#access-list 1 permit 192.168.1.0 ?
A.B.C.D Wildcard bits
log Log matches against this entry
<cr>
R1(config)#access-list 1 permit 192.168.1.0 0.0.0.255
R1(config)#access-list 1 permit host 192.168.10.5
R1(config)#access-list 1 permit host 192.168.10.6
R1(config)#access-list 1 permit host 192.168.10.7
R1(config)#access-list 1 permit 192.168.10.8 0.0.0.0
R1(config)#access-list 1 permit 192.168.10.9 0.0.0.0
R1(config)#access-list 1 permit 192.168.10.10 0.0.0.0
R1(config)#access-list 1 deny 192.168.10.11 0.0.0.0 #被下一句包含不是必要的
R1(config)#access-list 1 deny any
R1(config)#
##定义内部全局地址池
R1(config)#ip nat pool A_test 1.1.1.5 1.1.1.6 netmask 255.255.255.0
##建立映射关系(在inside端将原地址在List 1中的IP转换为Pool A_test的IP)
R1(config)#ip nat inside source list 1 pool A_test
R1(config)#
##区分inside和outside
R1(config)#inter f0/0
R1(config-if)#ip nat inside #定义内部网络接口
R1(config-if)#exit
R1(config)#inter f0/1
R1(config-if)#ip nat outside #定义外部网络接口
R1(config-if)#end
R1#
#保存配置
R1#write
Building configuration...
[OK]
R1#
R1配置完成后的效果:
基础信息的检验和查询
##R1上显示当前存在的NAT转换信息
R1#show ip nat translations
R1#
##查看NAT的统计信息
R1#show ip nat statistics
Total active translations: 0 (0 static, 0 dynamic; 0 extended)
Outside interfaces:
FastEthernet0/1
Inside interfaces:
FastEthernet0/0
Hits: 0 Misses: 0
CEF Translated packets: 0, CEF Punted packets: 0
Expired translations: 0
Dynamic mappings:
-- Inside Source
[Id: 1] access-list 1 pool A_test refcount 0
pool A_test: netmask 255.255.255.0
start 1.1.1.5 end 1.1.1.6
type generic, total addresses 6, allocated 0 (0%), misses 0
Appl doors: 0
Normal doors: 0
Queued Packets: 0
R1#
##显示当前存在的NAT转换的详细信息
R1#show ip nat translations verbose
R1#
##跟踪NAT操作,显示出每个被转换的数据包
R1#debug ip nat
IP NAT debugging is on
R1#
Clear ip nat translations *:删除NAT映射表中的所有内容.
##在PC-1上ping R2
PC-1> ping 1.1.1.2
1.1.1.2 icmp_seq=1 timeout
84 bytes from 1.1.1.2 icmp_seq=2 ttl=253 time=75.199 ms
84 bytes from 1.1.1.2 icmp_seq=3 ttl=253 time=75.137 ms
84 bytes from 1.1.1.2 icmp_seq=4 ttl=253 time=76.375 ms
84 bytes from 1.1.1.2 icmp_seq=5 ttl=253 time=76.691 ms
PC-1>
##在R1上收到转换详情
R1#
*Mar 1 02:29:31.463: NAT*: s=192.168.10.10->1.1.1.5, d=1.1.1.2 [14351]
R1#
*Mar 1 02:29:32.855: NAT*: s=192.168.10.10->1.1.1.5, d=1.1.1.2 [14352]
*Mar 1 02:29:32.927: NAT*: s=1.1.1.2, d=1.1.1.5->192.168.10.10 [14352]
*Mar 1 02:29:33.723: NAT*: s=192.168.10.10->1.1.1.5, d=1.1.1.2 [14353]
*Mar 1 02:29:33.791: NAT*: s=1.1.1.2, d=1.1.1.5->192.168.10.10 [14353]
R1#
*Mar 1 02:29:34.759: NAT*: s=192.168.10.10->1.1.1.5, d=1.1.1.2 [14354]
*Mar 1 02:29:34.823: NAT*: s=1.1.1.2, d=1.1.1.5->192.168.10.10 [14354]
*Mar 1 02:29:35.627: NAT*: s=192.168.10.10->1.1.1.5, d=1.1.1.2 [14355]
*Mar 1 02:29:35.691: NAT*: s=1.1.1.2, d=1.1.1.5->192.168.10.10 [14355]
R1#
##上面的查看信息都检查一下
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
--- 1.1.1.5 192.168.10.10 --- ---
R1#show ip nat translations verbose
Pro Inside global Inside local Outside local Outside global
--- 1.1.1.5 192.168.10.10 --- ---
create 00:01:40, use 00:01:36 timeout:86400000, left 23:58:23, Map-Id(In): 1,
flags:
none, use_count: 0, entry-id: 1, lc_entries: 0
R1#
R1#show ip nat statistics
Total active translations: 1 (0 static, 1 dynamic; 0 extended)
Outside interfaces:
FastEthernet0/1
Inside interfaces:
FastEthernet0/0
Hits: 4 Misses: 5
CEF Translated packets: 9, CEF Punted packets: 0
Expired translations: 5
Dynamic mappings:
-- Inside Source
[Id: 1] access-list 1 pool A_test refcount 1
pool A_test: netmask 255.255.255.0
start 1.1.1.5 end 1.1.1.6
type generic, total addresses 6, allocated 1 (16%), misses 0
Appl doors: 0
Normal doors: 0
Queued Packets: 0
R1#
##再从SW1上ping R2测试
SW1#ping 1.1.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.2, timeout is 2 seconds:
.!!!!
Success rate is 80 percent (4/5), round-trip min/avg/max = 48/82/96 ms
SW1#
##R1也对这次"internet"访问做了NAT转换
R1#
*Mar 1 02:37:49.719: NAT*: s=192.168.1.1->1.1.1.6, d=1.1.1.2 [15]
R1#
*Mar 1 02:37:51.799: NAT*: s=192.168.1.1->1.1.1.6, d=1.1.1.2 [16]
*Mar 1 02:37:51.867: NAT*: s=1.1.1.2, d=1.1.1.6->192.168.1.1 [16]
*Mar 1 02:37:51.959: NAT*: s=192.168.1.1->1.1.1.6, d=1.1.1.2 [17]
*Mar 1 02:37:52.019: NAT*: s=1.1.1.2, d=1.1.1.6->192.168.1.1 [17]
*Mar 1 02:37:52.087: NAT*: s=192.168.1.1->1.1.1.6, d=1.1.1.2 [18]
*Mar 1 02:37:52.147: NAT*: s=1.1.1.2, d=1.1.1.6->192.168.1.1 [18]
*Mar 1 02:37:52.211: NAT*: s=192.168.1.1->1.1.1.6, d=1.1.1.2 [19]
*Mar 1 02:37:52.271: NAT*: s=1.1.1.2, d=1.1.1.6->192.168.1.1 [19]
R1#
##检查转换详情
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
--- 1.1.1.6 192.168.1.1 --- ---
--- 1.1.1.5 192.168.10.10 --- ---
R1#show ip nat translations verbose
Pro Inside global Inside local Outside local Outside global
--- 1.1.1.6 192.168.1.1 --- ---
create 00:01:12, use 00:01:12 timeout:86400000, left 23:58:47, Map-Id(In): 1,
flags:
none, use_count: 0, entry-id: 7, lc_entries: 0
--- 1.1.1.5 192.168.10.10 --- ---
create 00:09:31, use 00:09:26 timeout:86400000, left 23:50:33, Map-Id(In): 1,
flags:
none, use_count: 0, entry-id: 1, lc_entries: 0
R1#show ip nat statistics
Total active translations: 2 (0 static, 2 dynamic; 0 extended)
Outside interfaces:
FastEthernet0/1
Inside interfaces:
FastEthernet0/0
Hits: 12 Misses: 6
CEF Translated packets: 18, CEF Punted packets: 0
Expired translations: 6
Dynamic mappings:
-- Inside Source
[Id: 1] access-list 1 pool A_test refcount 2
pool A_test: netmask 255.255.255.0
start 1.1.1.5 end 1.1.1.6
type generic, total addresses 6, allocated 2 (33%), misses 0
Appl doors: 0
Normal doors: 0
Queued Packets: 0
R1#
##关闭nat debug
R1#no debug ip nat
IP NAT debugging is off
R1#
此时,在R2上做一些测试
结论:
在R2上ping已映射和未映射的地址;
已映射的可以ping通;未映射的无法ping通。
R2#ping 1.1.1.5
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 92/116/124 ms
R2#ping 1.1.1.6
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.6, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 88/92/100 ms
R2#ping 1.1.1.7
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.7, timeout is 2 seconds:
.....
Success rate is 0 percent (0/5)
R2#
ping测试后再R1上检查NAT映射表
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.6:1 192.168.1.1:1 1.1.1.2:1 1.1.1.2:1
--- 1.1.1.6 192.168.1.1 --- ---
icmp 1.1.1.5:0 192.168.10.10:0 1.1.1.2:0 1.1.1.2:0
--- 1.1.1.5 192.168.10.10 --- ---
R1#show ip nat translations verbose
Pro Inside global Inside local Outside local Outside global
--- 1.1.1.6 192.168.1.1 --- ---
create 00:28:19, use 00:02:29 timeout:86400000, left 23:57:30, Map-Id(In): 1,
flags:
none, use_count: 0, entry-id: 7, lc_entries: 0
--- 1.1.1.5 192.168.10.10 --- ---
create 00:36:37, use 00:02:31 timeout:86400000, left 23:57:28, Map-Id(In): 1,
flags:
none, use_count: 0, entry-id: 1, lc_entries: 0
R1#show ip nat statistics
Total active translations: 2 (0 static, 2 dynamic; 0 extended)
Outside interfaces:
FastEthernet0/1
Inside interfaces:
FastEthernet0/0
Hits: 50 Misses: 18
CEF Translated packets: 56, CEF Punted packets: 0
Expired translations: 22
Dynamic mappings:
-- Inside Source
[Id: 1] access-list 1 pool A_test refcount 2
pool A_test: netmask 255.255.255.0
start 1.1.1.5 end 1.1.1.10
type generic, total addresses 6, allocated 2 (33%), misses 0
Appl doors: 0
Normal doors: 0
Queued Packets: 0
R1#
缺陷
这样配置动态NAT有个缺陷:
当pool中的地址用完之后;例如本例中,我定义的pool有两个地址
——1.1.1.5
——1.1.1.6
我分别利用PC-1和SW1 ping R2;占用掉所有pool资源
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.6:7 192.168.1.1:7 1.1.1.2:7 1.1.1.2:7
--- 1.1.1.6 192.168.1.1 --- ---
icmp 1.1.1.5:9802 192.168.10.10:9802 1.1.1.2:9802 1.1.1.2:9802
icmp 1.1.1.5:10058 192.168.10.10:10058 1.1.1.2:10058 1.1.1.2:10058
icmp 1.1.1.5:10314 192.168.10.10:10314 1.1.1.2:10314 1.1.1.2:10314
icmp 1.1.1.5:10570 192.168.10.10:10570 1.1.1.2:10570 1.1.1.2:10570
icmp 1.1.1.5:10826 192.168.10.10:10826 1.1.1.2:10826 1.1.1.2:10826
--- 1.1.1.5 192.168.10.10 --- ---
然后再利用R1的F0/0端口ping R2就会出现转换失败无法ping通的问题
R1#ping 1.1.1.2 source 192.168.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.2, timeout is 2 seconds:
Packet sent with a source address of 192.168.1.2
.....
Success rate is 0 percent (0/5)
R1#
这个时候我们就需要利用动态NAT+端口复用
R1上为A公司使用动态NAT+端口复用:
(只要在原来的动态NAT建立动态映射关系的语句后面加上overload即代表加上
了端口复用;当然设置前需要先no掉原先的映射关系)
加上overload参数将会从第一个地址开始翻译复用;另外这里的地址池并不一定
要和outside端口的地址在同一网段,只要有相应的路由即可。
R1#conf t
R1(config)#ip nat inside source list 1 pool A_test overload
%Dynamic mapping in use, cannot change
R1(config)#no ip nat inside source list 1 pool A_test
Dynamic mapping in use, do you want to delete all entries? [no]: y
R1(config)#ip nat inside source list 1 pool A_test overload
R1(config)#end
R1#write
Building configuration...
[OK]
R1#
##接下来我们还是先利用PC-1和SW1占用完pool资源
R1#show ip nat translations
R1#
PC-1> ping 1.1.1.2
84 bytes from 1.1.1.2 icmp_seq=1 ttl=253 time=63.547 ms
84 bytes from 1.1.1.2 icmp_seq=2 ttl=253 time=74.766 ms
84 bytes from 1.1.1.2 icmp_seq=3 ttl=253 time=74.656 ms
84 bytes from 1.1.1.2 icmp_seq=4 ttl=253 time=76.141 ms
84 bytes from 1.1.1.2 icmp_seq=5 ttl=253 time=84.774 ms
PC-1>
SW1#ping 1.1.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 64/85/100 ms
SW1#
##再利用R1的F0/0端口ping R2
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:10 192.168.1.1:10 1.1.1.2:10 1.1.1.2:10
icmp 1.1.1.5:24654 192.168.10.10:24654 1.1.1.2:24654 1.1.1.2:24654
icmp 1.1.1.5:24910 192.168.10.10:24910 1.1.1.2:24910 1.1.1.2:24910
icmp 1.1.1.5:25166 192.168.10.10:25166 1.1.1.2:25166 1.1.1.2:25166
icmp 1.1.1.5:25678 192.168.10.10:25678 1.1.1.2:25678 1.1.1.2:25678
icmp 1.1.1.5:25934 192.168.10.10:25934 1.1.1.2:25934 1.1.1.2:25934
R1#
R1#ping 1.1.1.2 source 192.168.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.2, timeout is 2 seconds:
Packet sent with a source address of 192.168.1.2
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 20/27/48 ms
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:10 192.168.1.1:10 1.1.1.2:10 1.1.1.2:10
icmp 1.1.1.5:9 192.168.1.2:9 1.1.1.2:9 1.1.1.2:9
icmp 1.1.1.5:24654 192.168.10.10:24654 1.1.1.2:24654 1.1.1.2:24654
icmp 1.1.1.5:24910 192.168.10.10:24910 1.1.1.2:24910 1.1.1.2:24910
icmp 1.1.1.5:25166 192.168.10.10:25166 1.1.1.2:25166 1.1.1.2:25166
icmp 1.1.1.5:25678 192.168.10.10:25678 1.1.1.2:25678 1.1.1.2:25678
icmp 1.1.1.5:25934 192.168.10.10:25934 1.1.1.2:25934 1.1.1.2:25934
R1#
þ可以看到R1的F0/0端口已经可以和R2通讯了;且NAT的映射关系不再是内网地址
和外网地址一一对应的关系了;而是变成不同的内网地址+端口会被翻译成相同
或不同的Pool地址+ 端口。
并且似乎每次通讯都会生成新的映射关系。
R1#ping 1.1.1.2 source 192.168.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.2, timeout is 2 seconds:
Packet sent with a source address of 192.168.1.2
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 64/67/76 ms
R1#ping 1.1.1.2 source 192.168.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.2, timeout is 2 seconds:
Packet sent with a source address of 192.168.1.2
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 64/70/80 ms
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:10 192.168.1.2:10 1.1.1.2:10 1.1.1.2:10
icmp 1.1.1.5:11 192.168.1.2:11 1.1.1.2:11 1.1.1.2:11
R1#ping 1.1.1.2 source 192.168.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.2, timeout is 2 seconds:
Packet sent with a source address of 192.168.1.2
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 60/61/64 ms
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:10 192.168.1.2:10 1.1.1.2:10 1.1.1.2:10
icmp 1.1.1.5:11 192.168.1.2:11 1.1.1.2:11 1.1.1.2:11
icmp 1.1.1.5:12 192.168.1.2:12 1.1.1.2:12 1.1.1.2:12
R1#
关于NAT地址映射表条目的超时时间问题:
在Cisco里,NAT按大类分有三种类型:
- 静态NAT
- 动态NAT(内网IP与Pool IP一一对应,且先到先得,用完即止,后来需要用pool ip的将被告知无法转换)
- 端口复用NAT(即PAT或者动态NAT+端口复用)
其中;静态NAT是一对一的IP地址映射,是永久不变的;一旦配置完成,映射永久存在于转换表中,永不超时。而动态NAT与端口复用NAT是匹配配置的第一个数据包来触发生成,动态建立的。
在这两种NAT中,由于终端一般是用户,也就是不是对外提供服务的一方;所以。在用户空闲的情况下(没有访问外网需求),如果长期占有公网IP地址,势必造成IP资源的浪费。并且,NAT条目的存在也会占用网络设备的资源,NAT映射表条目的超时时间就是为了解决这些问题的。
在动态NAT里,可以调整NAT的超时时间(默认是24小时),调整的命令如下:
ip nat #60代表60秒
在端口复用NAT(也就是带overload参数的NAT)里,因为有了端口上的转换所以可以有更多控制,比如tcp/udp等等;命令如下:
ip nat #60代表60秒;默认5分钟
ip nat #60代表60秒;默认1分钟
ip nat #60代表60秒;TCP默认24小时,当同一会话有RST或者FIN的时候,默认再保留1分钟
ip nat #60代表60秒;默认1分钟;对上一条的补充
ip nat #60代表60秒;默认1分钟
ip nat #60代表60秒;默认1分钟
……
接下来,我们做另一个测试:
(1)我在PC-1上ping R2;这会在R1上生成映射条目(为了方式条目超时我们将icmp超时时间设置为3600s);
PC-1> ping 1.1.1.2
84 bytes from 1.1.1.2 icmp_seq=1 ttl=253 time=61.395 ms
84 bytes from 1.1.1.2 icmp_seq=2 ttl=253 time=60.952 ms
84 bytes from 1.1.1.2 icmp_seq=3 ttl=253 time=76.436 ms
84 bytes from 1.1.1.2 icmp_seq=4 ttl=253 time=76.031 ms
84 bytes from 1.1.1.2 icmp_seq=5 ttl=253 time=75.481 ms
PC-1>
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:22107 192.168.10.10:22107 1.1.1.2:22107 1.1.1.2:22107
icmp 1.1.1.5:22363 192.168.10.10:22363 1.1.1.2:22363 1.1.1.2:22363
icmp 1.1.1.5:22619 192.168.10.10:22619 1.1.1.2:22619 1.1.1.2:22619
icmp 1.1.1.5:22875 192.168.10.10:22875 1.1.1.2:22875 1.1.1.2:22875
icmp 1.1.1.5:23131 192.168.10.10:23131 1.1.1.2:23131 1.1.1.2:23131
R1#
(2)这个时候在R2上ping 映射出来的pool地址; 记录通不通?(答案:通)
R2#ping 1.1.1.5
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 60/63/68 ms
R2#
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:22107 192.168.10.10:22107 1.1.1.2:22107 1.1.1.2:22107
icmp 1.1.1.5:22363 192.168.10.10:22363 1.1.1.2:22363 1.1.1.2:22363
icmp 1.1.1.5:22619 192.168.10.10:22619 1.1.1.2:22619 1.1.1.2:22619
icmp 1.1.1.5:22875 192.168.10.10:22875 1.1.1.2:22875 1.1.1.2:22875
icmp 1.1.1.5:23131 192.168.10.10:23131 1.1.1.2:23131 1.1.1.2:23131
R1#
(3)然后将PC-1关机,再次在R2上ping 映射出来的pool地址;记录通不通?(通;但是这个icmp的回包是谁回应的呢?)
R2#ping 1.1.1.5
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 64/68/72 ms
R2#
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:47965 192.168.10.10:47965 1.1.1.2:47965 1.1.1.2:47965
icmp 1.1.1.5:48221 192.168.10.10:48221 1.1.1.2:48221 1.1.1.2:48221
icmp 1.1.1.5:48477 192.168.10.10:48477 1.1.1.2:48477 1.1.1.2:48477
icmp 1.1.1.5:48989 192.168.10.10:48989 1.1.1.2:48989 1.1.1.2:48989
icmp 1.1.1.5:49245 192.168.10.10:49245 1.1.1.2:49245 1.1.1.2:49245
R1#
这样的话在设置好NAT后尤其是静态NAT,就不能通过ping pool地址测试真实服务器的在线状态了。
(4)然后将PC-1关机并shutdown R1的inside端口,再次在R2上ping 映射出来的pool地址;
记录通不通?(通;这个包一定是F0/1回应的,清空R1的Translation表就不通了,开启inside端口)
R1#conf t
R1(config)#inter f0/0
R1(config-if)#shutdown
R1(config-if)#end
R1#show ip nat translations
Pro Inside global Inside local Outside local Outside global
icmp 1.1.1.5:47965 192.168.10.10:47965 1.1.1.2:47965 1.1.1.2:47965
icmp 1.1.1.5:48221 192.168.10.10:48221 1.1.1.2:48221 1.1.1.2:48221
icmp 1.1.1.5:48477 192.168.10.10:48477 1.1.1.2:48477 1.1.1.2:48477
icmp 1.1.1.5:48989 192.168.10.10:48989 1.1.1.2:48989 1.1.1.2:48989
icmp 1.1.1.5:49245 192.168.10.10:49245 1.1.1.2:49245 1.1.1.2:49245
R1#
R2#ping 1.1.1.5
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.5, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 40/57/64 ms
R2#
这里可以根据global和local地址是否相同来判定左边(出去,入口)和右边(进来,出口)是否经过NAT转换;
很明显我这里左边转换过,右边没有经过转换。
静态NAT
还是这拓扑,我们在A公司已经实现了“动态NAT+端口复用”;接下来我们在B公司的R3上实现“静态NAT”。
我们先在R1上为左边内网(A公司)设置动态NAT;(已完成;实际做成了动态NAT+端口复用)
- 需要映射的内网地址:192.168.1.0/24和192.168.10.5-10/24(拒绝192.168.10.11/24)
- 映射的公网地址:1.1.1.5-6/24
然后在R3上为右边内网(B公司)设置端口复用:(即将做;静态NAT)
- 需要映射的内网地址:192.168.30.11/24
- 映射的公网地址:2.1.1.5/24
在B公司内网做些基础的设定
##SW2上将端口f0/2划分给vlan 30
SW2#conf t
SW2(config)#inter f0/2
SW2(config-if)#switchport mode access
SW2(config-if)#switchport access vlan 30
SW2(config-if)#no shut
SW2(config-if)#end
SW2#
SW2#write
Building configuration...
[OK]
SW2#
##Server001 f0/0设置IP和网关
Server001#conf t
Server001(config)#no ip routing
Server001(config)#
Server001(config)#inter f0/0
Server001(config-if)#ip address 192.168.30.11 255.255.255.0
Server001(config-if)#no shut
Server001(config-if)#exit
Server001(config)#ip default-gateway 192.168.30.254
Server001(config)#end
Server001#
##Server001 启用telnet
Server001#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Server001(config)#username admin password cisco
Server001(config)#enable sec cisco
Server001(config)#line vty 0 14
Server001(config-line)#login local
Server001(config-line)#end
Server001#wr
Building configuration...
[OK]
Server001#
确保在R3上能正常访问Server001
##在R3上telnet server001
R3#telnet 192.168.30.11
Trying 192.168.30.11 ... Open
User Access Verification
Username: admin
Password:
Server001>en
Password:
Server001#
R3上为B公司设置静态NAT
配置
R3#conf t
R3(config)#ip nat inside source static tcp 192.168.30.11 23 2.1.1.5 8080
R3(config)#inter f0/0
R3(config-if)#no ip nat inside
R3(config-if)#exit
R3(config)#inter f0/1
R3(config-if)#no ip nat outside
R3(config)#end
R3#wr
Building configuration...
[OK]
R3#
验证
##查看映射表
R3#show ip nat translations
Pro Inside global Inside local Outside local Outside global
tcp 2.1.1.5:8080 192.168.30.11:23 --- ---
R3#
##在R2上telnet 映射地址和端口
R2#telnet 2.1.1.5
Trying 2.1.1.5 ...
% Connection refused by remote host
R2#telnet 2.1.1.5 8080
Trying 2.1.1.5, 8080 ... Open
User Access Verification
Username: admin
Password:
Server001>
##在R1上telnet 映射地址和端口
R1#telnet 2.1.1.5 8080
Trying 2.1.1.5, 8080 ... Open
User Access Verification
Username: admin
Password:
Server001>exit
[Connection to 2.1.1.5 closed by foreign host]
R1#
##在SW1上telnet 映射地址和端口
SW1#telnet 2.1.1.5 8080
Trying 2.1.1.5, 8080 ... Open
User Access Verification
Username: admin
Password:
Server001>exit
[Connection to 2.1.1.5 closed by foreign host]
SW1#
现在关于NAT的实验就完成了。
挖个坑,这里大家来考虑下NAT地址池满的问题;后续如果有时间我们在延伸篇里做详细讲解。