单臂路由实现VLAN间通信

Title
单臂路由实现VLAN间通信
Date
May 14, 2023
不同VLAN之间隔离广播域,不能直接在二层上通信,想要在VLAN间通信,需要通过路由器或者三层交换机。
单臂路由(router-on-a-stick)是指在路由器的一个接口上通过配置子接口(或“逻辑接口”,并不存在真正物理接口)的方式,实现原来相互隔离的不同VLA之间互联互通。

实验拓扑图:

notion image

实验环境:

Cisco Packet Tracer模拟器

实验说明:

PC0,PC1,PC2属于不同的网段和VlAN,通过SW1相连到路由器R1上,通过在R1上实现单臂路由功能来实现三台PC互通。

参考配置:

PC0:
notion image
PC1:
notion image
PC2:
notion image
SW配置:
Switch(config)#vlan 2 //创建vlan2 Switch(config)#vlan 3 //创建vlan3 Switch(config)#vlan 4 //创建vlan4 Switch(config)#int f0/1 Switch(config-if)#switchport mode access //指定端口为access模式 Switch(config-if)#switchport access vlan2 //指定接口为VLAN2 Switch(config-if)#int f0/2 Switch(config-if)#switchport mode access Switch(config-if)#switchport access vlan3 //指定接口为VLAN3 Switch(config-if)#int f0/3 Switch(config-if)#switchport mode access Switch(config-if)#switchport access vlan4 //指定接口为VLAN4 Switch(config-if)#int f0/4 Switch(config-if)#switchport mode trunk //与路由器相连的端口设置为trunk模式 Switch(config-if)#switchport trunk allowed vlan all //允许所有vlan帧通过,可不加,默认设置 Switch#show int f0/1 switchport //查看f0/1的端口的信息(vlan信息)
R1配置:
R1(config)#int g0/0 R1(config-if)#no shutdown //开启端口 R1(config-if)#int g0/0.1 R1(config-subif)#encapsulation Dot1Q 2 //进入子接口模式,设置vlan帧格式为Dot1Q VLAN ID为2,要注意和IP对应的vlan ID 保持一致 R1(config-subif)#ip add 10.1.1.254 255.255.255.0 //设置为PC0对应的网关地址,PC0 VLAN ID 为 2, 要注意和g0/0.1 接口的VLAN ID 一致才行 R1(config-if)#int g0/0.2 R1(config-subif)#encapsulation Dot1Q 3 R1(config-subif)#ip add 10.1.2.254 255.255.255.0 R1(config-if)#int g0/0.3 R1(config-subif)#encapsulation Dot1Q 4 R1(config-subif)#ip add 10.1.3.254 255.255.255.0
设置完之后,三台PC就可以互通了,不要忘记设置PC的网关。
PC0和PC1与PC2通信:
notion image
扩展:
因为连接PC要手动设置IP地址和网关,所以我在SW上启用了DHCP服务,又添加了一个VLAN5:
SW配置:
Switch(config)#service dhcp //开启dhcp服务 Switch(config)#ip dhcp pool lion //创建名字lion dhcp ip 地址池 Switch(dhcp-config)#network 10.1.5.0 255.255.255.0 //dhcp 网段地址 Switch(dhcp-config)#default-router 10.1.5.1 //默认网关地址,设置为10.1.5.1 Switch(dhcp-config)#domain-name lion.com //设置域名 Switch(dhcp-config)#dns-server 8.8.8.8 //设置dns Switch(config)#ip dhcp excluded-address 10.1.5.1 //保留dhcp地址 Switch(config)#int vlan 5 #创建vlan 5接口 Switch(config-if)#ip add 10.1.5.1 255.255.255.0 //设置为dhcp网关 Switch(config-if)#int f0/5 //f0/5设置为access口,VLAN ID 为5 Switch(config-if)#switchport mode access Switch(config-if)#switchport access vlan 5
PC3只要连接上f0/5口,ip改为dhcp就可以自动获取ip地址:
notion image

三层交换机实现VLAN间通信:

使用三层交换机也可以实现VLAN间通信,相比路由器来说更方便

实验拓扑:

只需要把R1和Switch替换成一个三层交换机即可,其他的PC配置不变:
notion image
 
参考配置:
SW:
三层交换机开启路由功能:ip routing
三层交换机二层端口开启三层:no switchport(可以添加ip地址)
Switch(config)#ip routing #开启数据路由 Switch(config-if)#int f0/1 Switch(config-if)#switchport //作为交换口,no switchport 作为三层口,可以添加ip Switch(config-if)#switchport mode access Switch(config-if)#switchport access vlan 2 //设置vlan id 为 2 Switch(config-if)#int f0/2 Switch(config-if)#switchport mode access //设置端口为access Switch(config-if)#switchport access vlan 3 //设置vlan id 为 3,如vlan3不存在,会先创建 % Access VLAN does not exist. Creating vlan 3 Switch(config-if)#int f0/3 Switch(config-if)#switchport mode access Switch(config-if)#switchport access vlan 4 % Access VLAN does not exist. Creating vlan 4 Switch(config)#int vlan 2 //创建vlan 2接口,并添加ip地址 Switch(config-if)# %LINK-5-CHANGED: Interface Vlan2, changed state to up %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan2, changed state to up Switch(config-if)#ip add 10.1.1.254 255.255.255.0 Switch(config-if)#int vlan 3 //创建vlan 3接口,并添加ip地址 %LINK-5-CHANGED: Interface Vlan3, changed state to up %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan3, changed state to up Switch(config-if)#ip add 10.1.2.254 255.255.255.0 Switch(config-if)#int vlan 4 //创建vlan 4接口,并添加ip地址 Switch(config-if)# %LINK-5-CHANGED: Interface Vlan4, changed state to up %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan4, changed state to up Switch(config-if)#ip add 10.1.3.254 255.255.255.0
结果:
配置完成可以互相访问:
notion image
 
Built with Potion.so