单臂路由的概念

单臂路由(router-on-a-stick)是指在路由器的一个接口上通过配置 子接口(或“逻辑接口”,并不存在真正物理接口)的方式,实现原来相互隔离的不同VLAN(虚拟局域网)之间的互联互通。由于从拓扑结构图上看,在交换机与路由器之间,数据从一条线路进去,又从一个线路出来,两条线路重合,故形象的称之为“ 单臂路由”(右图中黑色线路) [1]
在Cisco网络认证体系中,单臂路由是一个重要的学习知识点。通过单臂路由的学习,能够深入的了解VLAN(虚拟局域网)的划分、封装和通信原理,理解路由器子接口、ISL协议和802.1Q协议,是 CCNA考试中经常考察。

单臂路由的优缺点

VLAN能有效分割局域网,实现各网络区域之间的访问控制。但现实中,往往需要配置某些VLAN之间的互联互通。比如,你的公司划分为 领导层、销售部、财务部、人力部、科技部、审计部,并为不同部门配置了不同的VLAN,部门之间不能相互访问,有效保证了各部门的信息安全。但经常出现 领导层需要跨越VLAN访问其他各个部门,这个功能就由单臂路由来实现。
优点:实现不同vlan之间的通信,有助理解、学习VLAN原理和子接口概念。
缺点:容易成为网络单点故障,配置稍有复杂,现实意义不大。

单臂路由的实验手册

实验名称:单臂路由实验
目标:通过一个路由器进行多个VLAN互联
环境:1. 交换机为二层交换机,支持VLAN划分;2. 路由器只有1个Ethernet接口
实施:采用单臂路由,即在路由器上设置多个逻辑子接口,每个子接口对应于一个VLAN。由于物理路由接口只有一个,各子接口的数据在物理链路上传递要进行标记封装。Cisco设备支持ISL和 802.1q协议。华为设备只支持 802.1q。 

单臂路由的配置实例

实验设备:一台CISCO路由器(R1),一台二层交换机(S1),两台PC机( pc2和pc3)
实验拓扑

  实验拓扑

PC的配置如下:
pc2的IP:192.168.2.10 网关:192.168.2.1
pc3的IP:192.168.3.10 网关:192.168.3.1
交换机S1的配置如下:
Switch>enable
Switch#vlan database
Switch(vlan)#vlan 2 name test01
VLAN 2 added:
Name: test01
Switch(vlan)#vlan 3 name test02
VLAN 3 added:
Name: test02 →设置好vlan ,这里只简单设置两个。
Switch(vlan)#exit
APPLY completed.
Exiting....
Switch#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#interface fa0/2
Switch(config-if)#switchport access vlan 2
Switch(config-if)#exit
Switch(config)#interface fa0/3
Switch(config-if)#switchport access vlan 3
Switch(config-if)#exit
Switch(config)#interface fa0/1
Switch(config-if)#switchport trunk encapsulation dot1q (2960等 交换机只支持 802.1q协议,这里忽略)
Switch(config-if)#switchport mode trunk
Switch(config-if)#switchport trunk allowed vlan all
Switch(config-if)#exit
Switch(config)#end
%SYS-5-CONFIG_I: Configured from console by console
Switch#write memory
Building configuration...
[OK]
下面来重点,配置 单臂路由
Router>enable
Router#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z. .................进入全局配置模式
Router(config)#interface fa0/0 ................进入和 交换机连接的那个接口
Router(config-if)#no shutdown ................激活该端口
Router(config-if)#exit ……………返回到全局配置模式
Router(config)#interface fa0/0.1 ...........配置  子接口 这是配置 单臂路由的关键,这个接口是个  逻辑接口,并不是实际存在的物理接口,但是功能却和物理接口是一样的。
Router(config-subif)#encapsulation dot1q 2 .......为这个接口配置802.1Q协议,最后面的 2 是vlan 号,这也是关键部分
Router(config-subif)#ip address 192.168.2.1 255.255.255.0 .........为该接口划分ip地址。
Router(config-subif)#exit
Router(config)#interface fa0/0.2 .....同样,进入第2个子接口,进行配置
Router(config-subif)#encapsulation dot1q 3 .........配置802.1Q协议
Router(config-subif)#ip address 192.168.3.1 255.255.255.0 ......划分ip地址和 子网掩码
Router(config-subif)#end
%SYS-5-CONFIG_I: Configured from console by console ..........完成配置
下面是测试结果:
经过分别对两台机子互相ping的测试,可以发现能够ping通,说明实验成功。

编辑本段华为单臂路由

需求:在局域网中,通过 交换机上配置VLAN可以减少主机通信 广播域的范围,当VLAN之间有部分主机需要通信,但交换机不支持 三层交换时,可以采用一台 支持802.1Q的 路由器实现VLAN的互通.这需要在以太口上建立 子接口,分配IP地址作为该VLAN的 网关,同时启动802.1Q.
组网: 路由器E0端口与 交换机的上行trunk端口(第24端口)相连,交换机下行口划分3个VLAN,带若干 主机.

1.路由器的配置

[Router]
[Router]inter e0
[Router-Ethernet0]ip add 10.0.0.1 255.255.255.0
[Router-Ethernet0]inter e0.1 //定义 子接口E0.1
[Router-Ethernet0.1]ip add 172.16.1.1 255.255.255.0
[Router-Ethernet0.1]vlan-type dot1q vid 1 //指定 以太网 子接口属于VLAN1,此命令应用在 以太网 子接口上。只有配置了该命令之后, 以太网子接口才会根据配置的VLAN ID 号在 以太网帧头中嵌入VLAN 标签,与该网口相连的 交换机接口才能正确处理接收到的帧。
[Router-Ethernet0.1]inter e0.2 //定义 子接口E0.2
[Router-Ethernet0.2]ip add 172.16.2.1 255.255.255.0
[Router-Ethernet0.2]vlan-type dot1q vid 2 //指定以太网 子接口属于VLAN2
[Router-Ethernet0.2]inter e0.3 //定义 子接口E0.3
[Router-Ethernet0.3]ip add 172.16.3.1 255.255.255.0
[Router-Ethernet0.3]vlan-type dot1q vid 3 //指定以太网 子接口属于VLAN3
[Router-Ethernet0.3]inter e0
[Router-Ethernet0]undo shut
% Interface Ethernet0 is up
[Router-Ethernet0] //用网线将E0端口连到S3026第24端口
%19:46:32: Interface Ethernet0 changed state to UP
%19:46:32: Line protocol ip on interface Ethernet0, changed state to UP
%19:46:32: Line protocol ip on interface Ethernet0.1, changed state to UP
%19:46:32: Line protocol ip on interface Ethernet0.2, changed state to UP
%19:46:32: Line protocol ip on interface Ethernet0.3, changed state to UP

2.交换机的配置

<Quidway>
<Quidway>sys
Enter system view , return user view with Ctrl+Z.
[Quidway]vlan 1
[Quidway-vlan1]vlan 2
[Quidway-vlan2]port ethernet 0/17 to eth 0/19 eth 0/22 //将第17至 19端口,和第 22端口加入VLAN2
[Quidway-vlan2]vlan 3
[Quidway-vlan3]port eth 0/21 //将第21端口加入VLAN3
[Quidway-vlan3]inter e0/24
[Quidway-Ethernet0/24]port link-type trunk //将第24端口设为trunk口
[Quidway-Ethernet0/24]port trunk permit vlan all //允许所有VLAN流量通过
Please wait........................................... Done.
[Quidway-Ethernet0/24]dis port trunk //检验TRUNK口配置
Now, the following trunking ports exist:
Ethernet0/24
[Quidway-Ethernet0/24]dis vlan 2 //检验VLAN2的配置
VLAN ID: 2
VLAN Type: static
Route Interface: not configured
Description: VLAN 0002
Tagged Ports:
Ethernet0/24
Untagged Ports:
Ethernet0/17 Ethernet0/18 Ethernet0/19 Ethernet0/22
[Quidway-Ethernet0/24]dis vlan 3 //检验VLAN3的配置
VLAN ID: 3
VLAN Type: static
Route Interface: not configured
Description: VLAN 0003
Tagged Ports:
Ethernet0/24
Untagged Ports:
Ethernet0/21
3.在 工作站上检查网络是否连通。此工作站连接S3026第21端口,属于VLAN2。
C:\Documents and Settings\Administrator>ipconfig
windows 2000 IP Configuration
Ethernet adapter  本地连接:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 172.16.2.22
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 172.16.2.1
C:\Documents and Settings\Administrator>ping 172.16.3.1
Pinging 172.16.3.1 with 32 bytes of data:
Reply from 172.16.3.1: bytes=32 time<10ms TTL=255
Reply from 172.16.3.1: bytes=32 time<10ms TTL=255
Reply from 172.16.3.1: bytes=32 time<10ms TTL=255
Reply from 172.16.3.1: bytes=32 time<10ms TTL=255
Ping statistics for 172.16.3.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
4.在 路由器上查看 路由表。可以发现,由于172.16各 网段都是 直连路由,故不需启用 路由协议静态路由即能实现VLAN之间的通讯。
[Router]display ip routing-table
Routing Tables:
Destination/Mask Proto Pref Metric Nexthop Interface
10.0.0.0/24 Direct 0 0 10.0.0.1 Ethernet0
10.0.0.1/32 Direct 0 0  127.0.0.1LoopBack0
127.0.0.0/8 Direct 0 0  127.0.0.1 LoopBack0
127.0.0.1/32 Direct 0 0  127.0.0.1 LoopBack0
172.16.1.0/24 Direct 0 0 172.16.1.1 Ethernet0.1
172.16.1.1/32 Direct 0 0  127.0.0.1 LoopBack0
172.16.2.0/24 Direct 0 0 172.16.2.1 Ethernet0.2
172.16.2.1/32 Direct 0 0  127.0.0.1 LoopBack0
172.16.3.0/24 Direct 0 0 172.16.3.1 Ethernet0.3
172.16.3.1/32 Direct 0 0 127.0.0.1 LoopBack0

编辑本段vlan下的设置

在一个vlan下,可以通过设置路由接口的secondary ip实现在一个 物理网络上两个 具有不同 网段IP计算机的联通。

1.设置Ip地址

[root#PCA root]# ipconfig eth0 10.65.1.1 netmask 255.255.0.0
[root#PCB root]# ipconfig eth0 10.66.1.1 netmask 255.255.0.0
[root#PCA root]# ping 10.66.1.1 (不通)
[root#PCB root]# ping 10.65.1.1 (不通)
PCA与PCB是不通的,因为它们在不同的网络段,一个是10.65.0.0/16网络,一个
是10.66.0.0/16网络,即netid不同,而不同网络的主机间访问必须通过路由实现。

2. 设置ip地址

roa(config)#int f0/0
roa(config-if)#ip address 10.65.1.2 255.255.0.0
roa(config-if)#no shut
roa(config-if)#ip address 10.66.1.2 255.255.0.0 secondary
roa#sh run
在显示的信息当中,在FastEthernet0/0的位置,应该能够看到前面设置的两个IP
地址。

3. 可通性

[root#PCA root]# ping 10.66.1.1 (不通)
[root#PCA root]# ping 10.66.1.2 (通)
[root#PCB root]# ping 10.65.1.1 (不通)
[root#PCB root]# ping 10.65.1.3 (通)
为什么PCA和PCB还是不通呢?哦,还没有设置计算机的 网关
[root#PCA root]# route add default gw 10.65.1.2
[root#PCB root]# route add default gw 10.66.1.2
[root#PCA root]# ping 10.66.1.1 (通了)
[root#PCB root]# ping 10.65.1.1 (通了)

4. 去掉联接线

[root#PCA root]# ping 10.66.1.1 (不通了)
[root#PCB root]# ping 10.65.1.1 (不通了)
由此可以看出,PCA到PCB的发送的 数据包是经过 路由器的,从路由器f0/0入,再
从f0/0出,所以称之为 单臂路由
这种情况PCA和PCB在同一个 广播域中,对 网络带宽不利。如果划分VLAN可以隔离
广播域。通过 子接口可以实现对不同VLAN的路由。

5. 交换机的情况

双击交换机,进入交换机的 终端模式:
switch>en
password:
switch#conf t
switch(config)#int vlan 1
switch(config-if)#ip address 10.65.1.8 255.255.0.0
switch(config-if)#exit
switch(config)#ip default-gateway 10.65.1.2
switch(config)#end
switch#ping 10.65.1.1 (通)
switch#ping 10.66.1.1 (通)
断开 交换机路由器的联线,再执行从交换机ping 命令:
switch#ping 10.65.1.1 (通)
switch#ping 10.66.1.1 (不通)
这说明不同网络段IP的联通是要通过 路由器的。

编辑本段故障排查

有时候配置完成后并不能正常访问对端的PC,按照以下排错过程可以排除问题所在。
1、首先通过命令ipconfig /all 来检查客户端的地址及网关是否正常。结果如下: PC1>ipconfig
IP Address………………….:  192.168.1.10
Subnet Mask…………………: 255.255.255.0
Default Gateway……………..:  192.168.1.1
PC2>ipconfig
IP Address………………….:  192.168.2.10
Subnet Mask…………………: 255.255.255.0
Default Gateway……………..:  192.168.2.1
确认后,先PING各自的网关
PC1>ping 192.168.1.1
正在 Ping 192.168.1.1 具有 32 字节的数据:
来自 192.168.1.1 的回复:  字节=32 时间=2ms TTL=255
来自 192.168.1.1 的回复:  字节=32 时间=2ms TTL=255
来自 192.168.1.1 的回复: 字节=32 时间=2ms TTL=255
来自 192.168.1.1 的回复: 字节=32 时间=3ms TTL=255
192.168.1.1 的 Ping 统计信息:
数据包已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 2ms,最长 = 3ms,平均 = 2ms
2、第一步操作若不通,需要检查交换机的VLAN 配置和到路由器之间的Trunk链路状态。如下:
Switch#show vlan brief //查看 交换机VLAN信息
VLAN Name Status Ports
—- ——————————– ——— ——————————-
1 default active Fa0/3, Fa0/4, Fa0/5, Fa0/6
Fa0/7, Fa0/8, Fa0/9, Fa0/10
Fa0/11, Fa0/12, Fa0/13, Fa0/14
Fa0/15, Fa0/16, Fa0/17, Fa0/18
Fa0/19, Fa0/20, Fa0/21, Fa0/22
Fa0/23, Gig1/1, Gig1/2
10RED active  Fa0/1 //确认该接口是与PC1连接,且在VLAN 10中
20 BLACK active Fa0/2 //确认该接口是与PC2连接,且在VLAN 20中
1002 fddi-default active
1003 token-ring-default active
1004 fddinet-default active
1005 trnet-default active
Switch# show int trunk
Port Mode Encapsulation Status Native vlan
Fa0/24 on  802.1q trunking 1 // 交换机路由器连接的Fa0/24是封装模式为802.1q的Trunk状态
Port Vlans allowed on trunk
Fa0/24 1-1005
Port Vlans allowed and active in management domain
Fa0/24 1,10,20 //确认F0/24口允许VLAN10和VLAN20的数据通过
Port Vlans in spanning tree forwarding state and not pruned
Fa0/24 1,10,20
3、最后检查路由器上的配置。如下:
Router#show ip int br
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES unset  up up//确认物理接口状态为up,否则进入该接口执行no shutdown
FastEthernet0/0.1 192.168.1.1 YES manual up up //检查网关地址是否配置真确
FastEthernet0/0.2 192.168.2.1 YES manual up up
Router#show int | in is up|Vlan ID //通过include命令来塞选show int的结果,便于快速核对配置信息
FastEthernet0/0 is up, line protocol is up
FastEthernet0/0.1is up, line protocol is up //确认对应的字接口 封装到对应VLAN ID中
Encapsulation 802.1Q Virtual LAN,  Vlan ID 10
FastEthernet0/0.2 is up, line protocol is up
Encapsulation 802.1Q Virtual LAN,  Vlan ID 20
至此,VLAN间 单臂路由 故障可以完成排查。