解决思科交换机无法访问局域网外设备
问题背景
有时,我们需要远程连接来管理一台思科交换机,例如使用SSH协议。然而交换机运作在链路层,这就需要交换机有一个网络层地址,来接纳基于IP协议的远程访问请求。于是,我们依靠设置一个带有IP地址的交换机虚拟接口(Switch Virtual Interface,SVI),来使它拥有一个网络层接口。
在我们的例子中,我们的交换机位于网络192.168.1.0/24
中。我们想要为其设置IP地址192.168.1.253
。
Switch>enable
Switch#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#interface Vlan 1
*Mar 23 02:46:56.686: %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan1, changed state to down
Switch(config-if)#ip addres 192.168.1.253 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#
*Mar 23 02:47:16.454: %LINK-3-UPDOWN: Interface Vlan1, changed state to up
*Mar 23 02:47:17.454: %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan1, changed state to up
然而,这似乎还并不够。SSH请求可能来自另一个局域网(LAN)。如果交换机没有默认网关(Default Gateway),它将不知道如何响应收到的数据包。因此,我们为它设置其所在局域网的路由器,192.168.1.254
,作为默认网关。
Switch(config)#ip default-gateway 192.168.1.254
Switch(config)#do show running-config | include default-gateway
ip default-gateway 192.168.1.254
Switch(config)#
问题现象
然而,即使设置了默认网关,当另一局域网,10.0.0.0/30
中的IP地址10.0.0.2
尝试ping192.168.1.253
即交换机时,交换机并不响应。
故障排查
我们先再次检查了交换机上的配置,确认无误。然而,在我们的情况里,有一点与众不同,就是该交换机是一个三层交换机(Cisco IOSvL2 15.2)。
在三层交换机上,如果路由启用,那么交换机就会忽略默认网关设置,转而使用自己的路由表决定是否有到达某个目的地的可用路由(route)。
尽管如此,路由应该在思科三层交换机上默认处于禁用状态。但以防万一,我们仍然应该检查。
Switch>enable
Switch#show running-config | section ip
ip cef
no ipv6 cef
ip address 192.168.1.253 255.255.255.0
ip default-gateway 192.168.1.254
ip forward-protocol nd
ip http server
ip http secure-server
ip ssh server algorithm encryption aes128-ctr aes192-ctr aes256-ctr
ip ssh client algorithm encryption aes128-ctr aes192-ctr aes256-ctr
Switch#
我们确实没有看到ip routing
设置。然而,路由真的没有启用吗?为了保险起见,我们检查是否能访问路由表。
Switch#show ip route
Codes: L - local, C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is not set
192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.1.0/24 is directly connected, Vlan1
L 192.168.1.253/32 is directly connected, Vlan1
Switch#
我们惊奇地发现,竟然可以访问路由表!也就是说,路由处于启用状态。诡异的是,我们只有在使用no ip routing
禁用路由时,它才会在运行配置(running-config
)中显示出来。
Switch(config)#no ip routing
Switch(config)#do show running-config | section ip
no ip routing
no ip cef
no ipv6 cef
ip address 192.168.1.253 255.255.255.0
no ip route-cache
ip default-gateway 192.168.1.254
ip forward-protocol nd
ip http server
ip http secure-server
ip ssh server algorithm encryption aes128-ctr aes192-ctr aes256-ctr
ip ssh client algorithm encryption aes128-ctr aes192-ctr aes256-ctr
Switch(config)#
解决方法
方法一:禁用路由
使用no ip routing
禁用路由后,交换机将会根据默认网关(ip default-gateway [IP地址]
)设置来决定默认网关,而忽略路由表。
Switch(config)#no ip routing
Switch(config)#do show ip route
Default gateway is 192.168.1.254
Host Gateway Last Use Total Uses Interface
ICMP redirect cache is empty
Switch(config)#
此时,10.0.0.2/30
已能ping通。
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.253, timeout is 2 seconds:
.!!!!
Success rate is 80 percent (4/5), round-trip min/avg/max = 2/2/3 ms
方法二:设置默认路由(Default Route)
如果你不想关掉路由功能,也可以靠设置默认路由来让交换机认识到默认网关。但在我们的情况内,我们并不想让该交换机拥有路由功能,所以选用了方法一。
Switch(config)#ip route 0.0.0.0 0.0.0.0 192.168.1.254
Switch(config)#do show ip route
Codes: L - local, C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is 192.168.1.254 to network 0.0.0.0
S* 0.0.0.0/0 [1/0] via 192.168.1.254
192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.1.0/24 is directly connected, Vlan1
L 192.168.1.253/32 is directly connected, Vlan1
Switch(config)#