计算机网络实验 配置基于802.1Q trunk的vlan间路由

计算机网络实验 配置基于802.1Q trunk的vlan间路由

一、技术文档

二、实验要点

  ​​802.1Q​​是一款VLAN标记协议,可以在以太网帧中插入VLAN标识信息,使得交换机能够识别和区分不同VLAN的流量。这样一来就可以允许一条物理链路中传输多个VLAN的流量。也能实现跨VLAN路由,因为路由器能够通过子接口解析802.1Q标签。

三、命令实现

Part 1 基础硬件设置配置

Step 1 按拓扑图连接网络

Step 2 配置电脑

Step 3 根据需要初始化并重新加载路由器和交换机

Step 4 配置交换机

执行以下命令,以S1为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

Switch>
Switch>enable
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.

Switch(config)#no ip domain-lookup
Switch(config)#service password-encryption
Switch(config)#enable secret class

Switch(config)#banner motd # Unauthorized access is strictly prohibited. #

Switch(config)#line con 0
Switch(config-line)#password cisco
Switch(config-line)#login
Switch(config-line)#logging synchronous

Switch(config-line)#line vty 0 15
Switch(config-line)#password cisco
Switch(config-line)#login
Switch(config-line)#exit

Switch(config)#
Switch(config)#hostname S1

S1(config)#interface vlan 1
S1(config-if)#ip address 192.168.1.11 255.255.255.0
S1(config-if)#no shut

S1(config-if)#
%LINK-5-CHANGED: Interface Vlan1, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan1, changed state to up

S1(config-if)#ip default-gateway 192.168.1.1

<!-- 先禁用交换机上所有端口(提示信息已省略) -->
S1(config)#interface range f0/1 - 24
S1(config-if-range)#shutdown

<!-- 再打开所有要使用到的端口(提示信息已省略) -->

S1(config)#interface f0/1
S1(config-if)#no shut

S1(config-if)#interface f0/6
S1(config-if)#no shut

S1(config-if)#interface f0/5
S1(config-if)#no shut

S1(config-if)#end

S1#wr
Building configuration...
[OK]
S1#

Step 5 配置路由器

执行以下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Router>enable
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.

Router(config)#no ip domain-lookup
Router(config)#hostname R1
R1(config)#service password-encryption
R1(config)#enable secret class
R1(config)#banner motd # Unauthorized access is strictly prohibited. #
R1(config)#

R1(config)#line con 0
R1(config-line)#password cisco
R1(config-line)#login
R1(config-line)#logging synchronous

R1(config-line)#line vty 0 4
R1(config-line)#password cisco
R1(config-line)#login
R1(config-line)#exit

<!-- 配置回环接口 -->
R1(config)#interface loopback 0

R1(config-if)#
%LINK-5-CHANGED: Interface Loopback0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback0, changed state to up

R1(config-if)#ip address 209.165.200.225 255.255.255.224
R1(config-if)#end
R1#
%SYS-5-CONFIG_I: Configured from console by console

R1#wr
Building configuration...
[OK]
R1#

Part 2 配置交换机vlan和trunk

在交换机中执行以下命令,以S1为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
S1>enable
Password:
S1#conf t
Enter configuration commands, one per line. End with CNTL/Z.

<!-- 创建VLAN并命名 -->
S1(config)#vlan 10
S1(config-vlan)#name Students
S1(config-vlan)#vlan 20
S1(config-vlan)#name Faculty
S1(config-vlan)#exit

<!-- 配置Trunk接口 -->
<!-- Trunk 接口允许传输多个 VLAN 的流量 -->
<!-- 通常用于连接 ​​其他交换机、路由器或服务器​​(如跨交换机的VLAN 扩展)。 -->
S1(config)#interface f0/1
S1(config-if)#switchport trunk native vlan 1 <!-- 配置Trunk端口的Native VLAN,该VLAN的流量在链路上​​不携带802.1Q标签 -->
S1(config-if)#switchport trunk allowed vlan 1,10,20 <!-- 限制该 Trunk端口​​仅允许VLAN 1、10 和20的流量通过-->
S1(config-if)#switchport mode trunk

S1(config-if)#interface f0/5
S1(config-if)#switchport trunk native vlan 1
S1(config-if)#switchport trunk allowed vlan 1,10,20
S1(config-if)#switch mode trunk

<!-- 配置Access接口 -->
<!-- Access 接口仅属于一个 VLAN,通常用于连接​​终端设备​​(如电脑、打印机)。 -->
S1(config-if)#interface f0/6
S1(config-if)#switchport mode access
S1(config-if)#switchport access vlan 10
<!-- 为该接口配置安全设置 -->
S1(config-if)#switchport port-security
S1(config-if)#switchport port-security maximum 2
S1(config-if)#switchport port-security mac-address sticky

<!-- S1(config-if)#switchport port-security mac-address sticky 00E0.A3A2.8124 -->

S1(config-if)#end
S1#
%SYS-5-CONFIG_I: Configured from console by console

S1#wr
Building configuration...
[OK]
S1#

Part 3 配置基于trunk的vlan间路由

在R1上执行以下命令以完成单臂路由配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- 创建子接口并绑定VLAN -->
R1(config)# interface g0/1.1
R1(config-subif)# encapsulation dot1Q 1 <!-- 绑定到VLAN 1 -->
R1(config-subif)# ip address 192.168.1.1 255.255.255.0

R1(config-subif)# interface g0/1.10
R1(config-subif)# encapsulation dot1Q 10 <!-- 绑定到VLAN 10 -->
R1(config-subif)# ip address 192.168.10.1 255.255.255.0

R1(config-subif)# interface g0/1.20
R1(config-subif)# encapsulation dot1Q 20 <!-- 绑定到VLAN 20 -->
R1(config-subif)# ip address 192.168.20.1 255.255.255.0
R1(config-subif)# exit

<!-- 如果有一个子端口分配有Native VLAN的需求的话需要多加一个属性 -->
<!-- 例如:encapsulation dot1Q 99 native -->

<!-- 激活物理接口 g0/1,使其能够收发流量 -->
R1(config)# interface g0/1
R1(config-if)# no shutdown

计算机网络实验 配置基于802.1Q trunk的vlan间路由
http://example.com/2025/05/22/note56/
作者
谢斐
发布于
2025年5月22日
许可协议