不同VLAN之间隔离广播域,不能直接在二层上通信,想要在VLAN间通信,需要通过路由器或者三层交换机。
单臂路由(router-on-a-stick)是指在路由器的一个接口上通过配置子接口(或“逻辑接口”,并不存在真正物理接口)的方式,实现原来相互隔离的不同VLA之间互联互通。
实验拓扑图:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F05%2F01%2F8BUxGZJNuaHdVO1.png?table=block&id=b8ac288b-5a73-41ff-9918-cdd398e037da&cache=v2)
实验环境:
Cisco Packet Tracer模拟器
实验说明:
PC0,PC1,PC2属于不同的网段和VlAN,通过SW1相连到路由器R1上,通过在R1上实现单臂路由功能来实现三台PC互通。
参考配置:
PC0:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F05%2F01%2Famrhz5GZcEjsv62.png?table=block&id=a89573ab-129f-4cf2-9ad4-3ed6b7c4e831&cache=v2)
PC1:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F05%2F01%2FN4MBdk581ncivFK.png?table=block&id=795481d1-1c3f-4b3d-80d1-e21bd7c23b6c&cache=v2)
PC2:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F05%2F01%2FEe2kLxPfopFZUBm.png?table=block&id=ec88d383-2e3e-4586-ab5c-e7f8848b4a09&cache=v2)
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](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F05%2F01%2FbUyjYwRHSrhs5td.png?table=block&id=c05fe50e-0a9c-4edc-bc61-58487b775a5b&cache=v2)
扩展:
因为连接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](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F05%2F01%2FZHvGniQOg5z9x6K.png?table=block&id=926e2c7d-a914-43f3-a7c5-39fd329557e5&cache=v2)
三层交换机实现VLAN间通信:
使用三层交换机也可以实现VLAN间通信,相比路由器来说更方便
实验拓扑:
只需要把R1和Switch替换成一个三层交换机即可,其他的PC配置不变:
![notion image](https://www.notion.so/image/https%3A%2F%2Fs2.loli.net%2F2022%2F05%2F01%2F82ayYrBvLzHZcXU.png?table=block&id=febc6fec-43a4-4a61-80e8-877102ccef5c&cache=v2)
参考配置:
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](https://www.notion.so/image/https%3A%2F%2Fi.imgur.com%2FMXUnRbt.png?table=block&id=6b23a4a7-1191-47b2-9057-635635cb313e&cache=v2)