单接口NAT配置实例
条件:
1、IOS在12.1(5)T9及以上版本。更低版本未做验证。
2、至少具有两个或多个ISP提供的的global地址。
实现思路:
1、将ISP提供的地址作为secondary地址配置在以太接口上。该以太接口同时作为inside接口。作为内部主机的网关。
2、 创建一个loopback接口做为nat的outside.
3、使用route-map,强行将内部网出去的数据包及从外部返回的对应数据包路由到loopback接口。
实例:
拓朴如下图所示:
配置如下:
interface Loopback0
ip address 172.16.2.254 255.255.255.252
ip nat outside
!
//创建一个loopback接口,并作为NAT outside接口。
interface Ethernet0
ip address 192.168.0.1 255.255.255.248 secondary
ip address 172.16.1.254 255.255.255.0
ip Nat inside
ip policy route-map rm-nat
!
//在E0接口上配置172.16.1.254做为IP,同时将ISP提供的地址做为Secondary地址。e0做为inside接口。
在本接口上应用rm-nat这个route-map
ip nat pool pool1 192.168.0.2 192.168.0.3 prefix-length 29
ip nat inside source list 10 pool pool1 overload
//常规NAT配置
ip classless
ip route 0.0.0.0 0.0.0.0 192.168.0.6
ip route 172.16.1.0 255.255.255.0 Ethernet0
access-list 10 permit 172.16.1.0 0.0.0.255
//配置路由及NAT所需要的access-list
access-list 101 permit ip 172.16.1.0 0.0.0.255 any
access-list 101 permit ip any 192.168.0.0 0.0.0.7
//配置route-map所需要的acl。第一句匹配出去的包,第二句匹配返回的包。
route-map rm-nat permit 10
match ip address 101
set ip next-hop 172.16.2.254
//配置所需要的route-map,凡是满足access-list 101条件的包均被转发到172.16.2.254(loopback0接口)。
疑问一:
示例中有两条静态路由,ip route 172.16.1.0 255.255.255.0 Ethernet0 是什么用处?
疑问二:我按这样的意思配置以后,不能访问nat地支持同一网段的其他电脑,怎么回事?
以下是我的配置结果:
以下假设172.18.24.224-239都是外部地址,这样配之后的结果是不可以访问172.18.24.0/24的电脑。
ip access-list standard 1
permit 10.10.11.0 0.0.0.255
exit
ip access-list extended 1001
permit ip 10.10.11.0 0.0.0.255 any
permit ip any 172.18.24.224 0.0.0.15
exit
interface loopback0
ip address 10.10.10.254 255.255.255.252
ip nat outside
exit
interface fastethernet0
ip address 10.10.11.254 255.255.255.0
ip address 172.18.24.225 255.255.255.0 secondary
(或ip address 172.18.24.225 255.255.255.252 secondary,但不可以是ip address 172.18.24.225 255.255.255.240 secondary)
ip nat inside
ip policy route-map test
exit
ip nat pool pool1 172.18.24.224 172.18.24.239 netmask 255.255.255.0
ip nat inside source list 1 pool pool1 overload
ip route 0.0.0.0 0.0.0.0 172.18.24.254
route-map test permit 10
match ip address 1001
set ip next-hop 10.10.10.254
exit
将原来接电脑的网线接到2层交换机上,然后路有器、电脑在接到交换机上。除了172.18.24.0/24资源不能访问,其他一切正常。
请问这是什么原因呢?
ip route 172.18.24.0 255.255.255.0 172.18.24.225
我已经将set ip next-hop 172.16.2.254 改为set ip next-hop 172.16.2.253
现在配置如下:
Current configuration : 1205 bytes
!
version 12.2
no parser cache
no service single-slot-reload-enable
service timestamps debug uptime
service timestamps log uptime
no service password-encryption
!
hostname Router
!
logging rate-limit console 10 except errors
!
ip subnet-zero
!
!
!
no ip dhcp-client network-discovery
!
!
!
interface Loopback0
ip address 172.16.2.254 255.255.255.252
ip nat outside
!
interface Ethernet0/0
ip address xxx.72.132.50 255.255.255.248 secondary
ip address 172.16.1.254 255.255.255.0
ip nat inside
ip policy route-map rm-nat
full-duplex
!
ip nat pool pool1 xxx.72.132.50 xxx.72.132.51 netmask 255.255.255.248
ip nat inside source list 10 pool pool1 overload
//动态映射,让局网的机器上外网
ip nat inside source static 172.16.1.3 219.72.132.50
//静态映射,让外网访问xxx.72.132.50 这个IP时,映射到内局的一台IP为172.16.1.3 的WEB服务器上
ip classless
ip route 0.0.0.0 0.0.0.0 xxx.72.132.49
ip route 172.16.1.0 255.255.255.0 Ethernet0/0
ip http server
!
access-list 10 permit 172.16.1.0 0.0.0.255
access-list 101 permit ip 172.16.1.0 0.0.0.255 any
access-list 101 permit ip any 219.72.132.48 0.0.0.7
access-list 177 permit icmp any any
//这个list 177用来做debug的
route-map rm-nat permit 10
match ip address 101
set ip next-hop 172.16.2.253
!
!
目前我的拓扑结构如下:
http://bbs.chinaitlab.com/UploadFile/2004126162526959.jpg
路由表如下:
路由表如下
Router#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is xxx.72.132.49 to network 0.0.0.0
172.16.0.0/16 is variably subnetted, 2 subnets, 2 masks
C 172.16.2.252/30 is directly connected, Loopback0
C 172.16.1.0/24 is directly connected, Ethernet0/0
xxx.72.132.0/29 is subnetted, 1 subnets
C xxx.72.132.48 is directly connected, Ethernet0/0
S* 0.0.0.0/0 [1/0] via xx.72.132.49
Router#
然后我就在路由上启用命令:debug ip packet 177 detail
看看从公网(xxx.72.132.52/29)上ping路由的公网IP:xxx.72.132.50是否转换到内网的172.16.1.3这个地址,结果发现,数据包到达路由公网IP接口就直接返回了,并没有起动nat过程,具体数据显示为:
3d03h: IP: s=xxx.72.132.52 (Ethernet0/0), d=xxx.72.132.50 (Ethernet0/0), len 60,
rcvd 3
3d03h: ICMP type=8, code=0
3d03h: IP: s=xxx.72.132.50 (local), d=xxx72.132.52 (Ethernet0/0), len 60, sendi
ng
3d03h: ICMP type=0, code=0
3d03h: IP: s=xxx.72.132.52 (Ethernet0/0), d=xxx.72.132.50 (Ethernet0/0), len 60,
rcvd 3
3d03h: ICMP type=8, code=0
3d03h: IP: s=xxx.72.132.50 (local), d=xxx.72.132.52 (Ethernet0/0), len 60, sendi
ng
3d03h: ICMP type=0, code=0
3d03h: IP: s=xx.72.132.52 (Ethernet0/0), d=xxx.72.132.50 (Ethernet0/0), len 60,
rcvd 3
3d03h: ICMP type=8, code=0
3d03h: IP: s=xxx.72.132.50 (local), d=xxx.72.132.52 (Ethernet0/0), len 60, sendi
ng
3d03h: ICMP type=0, code=0
3d03h: IP: s=xx.72.132.52 (Ethernet0/0), d=xxx.72.132.50 (Ethernet0/0), len 60,
rcvd 3
3d03h: ICMP type=8, code=0
3d03h: IP: s=xxx.72.132.50 (local), d=xxx.72.132.52 (Ethernet0/0), len 60, sendi
ng
3d03h: ICMP type=0, code=0
然后,我再从内的172.16.1.3ping 网关,这次发现外出的数据包成功转换,但只能看到数据包转换为路由的公网IP之后就没有反映了,我估计和上面的公网IP ping 一样,内网数据包可以到达网关,但是从网关的返回包到了路由后没有做NAT转换为内网IP,所以就不通了。可能问题就在于此,可是百思不得其解的是为什么公网过来的数据包没做转换呢,而且也没做route-map。
内局ping 网关的数据显示为:
3d03h: IP: NAT enab = 1 trans = 0 flags = 80
3d03h: NAT: s=172.16.1.3->xxx.72.132.50, d=xxx.32.132.49 [732]
3d03h: IP: s=xxx.72.132.50 (Ethernet0/0), d=xxx.32.132.49 (Loopback0), g=172.16.
2.253, len 60, forward
3d03h: ICMP type=8, code=0
3d03h: IP: s=xxx.72.132.50 (Loopback0), d=xxx.32.132.49 (Ethernet0/0), g=xxx.72.
132.49, len 60, forward
3d03h: ICMP type=8, code=0
3d03h: IP: NAT enab = 1 trans = 0 flags = 80
3d03h: NAT: s=172.16.1.3->xxx.72.132.50, d=xxx.32.132.49 [733]
3d03h: IP: s=xxx.72.132.50 (Ethernet0/0), d=xxx.32.132.49 (Loopback0), g=172.16.
2.253, len 60, forward
3d03h: ICMP type=8, code=0
3d03h: IP: s=xxx.72.132.50 (Loopback0), d=xxx.32.132.49 (Ethernet0/0), g=xxx.72.
132.49, len 60, forward
3d03h: ICMP type=8, code=0
我的疑惑:
1、在内网172.16.1.3ping 网关是出现request timed out
2、我是将实验所用的设备都接在思科3550-48单独划分出来的一个VLAN中,此VLAN与其他VLAN没有任何关系,也没做VLAN间路由。那么返程的路由,是指哪里路由到哪里啊。如果外网数据已经到达我的路由,我认为先应该起动那条
route-map rm-nat permit 10
match ip address 101
set ip next-hop 172.16.2.253
然后匹配access-list 101中的
access-list 101 permit ip any 219.72.132.48 0.0.0.7
接着把数据包发送到lo 0端口,进行NAT转换成内网IP,由于这个内网IP是直连在路由端口上的,按理就能找到了。
现在问题就是这一系列动作都没有进行。
3、按思科的文档来说,这个case只需要具体NAT功能的路由即可(参见相关思科单接路由NAT配置文档 (http://www.cisco.com/en/US/tech/tk648/tk361/technologies_tech_note09186a0080094430.shtml)
),按照从网上流行的“单接路由NAT配置实例”文档来说要求用的是12.1(5)T9的IOS或更高版本,而我所用的是12.2(2)T4,是不是应该比前面所提的版本高呢?
改成ip route 172.16.2.0 255.255.255.0 Ethernet0/0看一下情况怎么样!
还有为什么要把172.16.2.0这一网段改到e0/0啊,它明明是在loop 0上的啊
问楼主,请问这句话什么意思:(或ip address 172.18.24.225 255.255.255.252 secondary,但不可以是ip address 172.18.24.225 255.255.255.240 secondary),为什么不能ip address 172.18.24.225 255.255.255.240 secondary啊
这个我也不知道为什么,实际中就是这样,要时掩码换成240,就出不去。另外我以上的配置,是可以访问绝大多数外部资源了(除了172。18。24。0段),nat也成功的。
cisco pdf 文档说不可以 set ip next-hop 10.10.10.254,而需set ip next-hop 10.10.10.253,楼主你认为呢
我想这个指任何一个都应该一样的效果,10.10.10.*路由器都会把它送到那个接口,因为路由器认为这个网段是连着那个接口的。
我想请问:你路由的默认网关是
ip route 0.0.0.0 0.0.0.0 172.18.24.254上,现在你说除了172。18。24。0段访问不了,也就是说你连你的默认网关都访问不到,你凭什么说是可以访问绝大多数外部资源啊。你连你到外部资源的网关都不通,也就是根本上不了外网。
172.18.24.254是网关,下面接了普通的交换机,本来客户端都是DHCP据获取地址的,默认网关就是172.18.24.254。然后我选取一些没在用的地址,作为NAT的外部地址使用,在同一网段的电脑,可以ping通路由器的172.18.24.225地址,只是ping不到在用的NAT172.18.24.239地址。
我想这是因为网关处没有路由的缘故,所以能访问网关以后的资源?
[ 关闭窗口 ]
