引言

常见的以太网设备包括Hub、交换机等。交换机工作在数据链路层,它有效地隔离了以太网中的冲突域,极大地提升了以太网的性能。

日前,Hub(集线器)设备很少见,它可能出现在小型办公室当中。

小型交换网络

image-20240204121103587

交换机工作在 OSI 七层模型中的数据链路层,用来转发数据帧。因此交换机,也被称为二层设备。

交换机的转发原理

转发行为

image-20240204121310882

交换机的转发行为涉及到泛洪(Flooding)、转发(Forwarding)和丢弃(Discarding)这三个主要方面,

泛洪(Flooding): 当交换机收到某个接口发送的一个数据帧,但它的 MAC地址表 中没有目标 MAC地址 的对应记录时,交换机会执行泛洪操作。这意味着它将数据帧发送到所有连接的端口,除了接收到该数据帧的端口之外的所有端口。这样确保了目标设备能够接收到数据帧,同时也用于学习新的MAC地址。

转发(Forwarding): 当交换机在 MAC地址表 中找到了 目标MAC地址 的对应记录时,它会直接将数据帧转发到与目标地址相关联的端口。这是一种精确的目标转发,可以大大减少网络中的冗余流量,提高网络效率。

丢弃(Discarding): 有时候,交换机可能会选择丢弃接收到的数据帧。这种情况可能发生在网络出现异常或者帧有错误的情况下。如果数据帧的 目标MAC地址 不在 MAC地址 表中,而交换机又无法通过泛洪找到目标设备,它可能会选择丢弃该数据帧,避免在网络中引入不必要的流量。

初始状态

image-20240204121647671

初始状态下,交换机的 MAC 地址表是空的。

学习 MAC 地址

image-20240204121736804

交换机将收到的数据帧的源 MAC地址 和对应接口记录到 MAC地址 表中。

转发数据帧

image-20240204121825155

当数据帧的 目的MAC地址不在 MAC表 中,或者 目的MAC地址 为广播地址时,交换机会泛洪该帧,也就是说将该数据帧发送到除发送接口(G0/0/1)外的所有接口。

目标主机回复

image-20240204121939610

交换机根据 MAC地址 表将目标主机的回复信息单播转发给源主机(主机A),并将发送方主机C的 MAC 地址和对应接口信息记录在交换机的 MAC地址表 中。

VLAN 技术

引言

​ 随着网络中计算机的数量越来越多,传统的以太网络开始面临冲突严重、广播泛滥以及安全性无法保障等各种问题。
VLAN(Virtual Local Area Network)即虚拟局域网,是将一个物理的局域网在逻辑上划分成多个广播域的技术。通过在交换机上配置VLAN,可以实现在同一个VLAN内的用户可以进行二层互访,而不同VLAN间的用户被二层隔离。这样既能够隔离广播域,又能够提升网络的安全性。

广播域

image-20240204125234113

VLAN 技术能隔离广播域。

在以上拓扑中,VLAN 20 是一个广播域,而 VLAN 10也是一个广播域。

VLAN 帧格式

image-20240204125545917 image-20240204125557039

​ 交换机设备通过 Tag 中的 VLAN ID,区分不同VLAN,VLAN 计算机在发送数据的时候,数据本身是没有 Tag 的,而从交换机接口发送出去之后,就带有了 Tag(打标签),但是在交换机与PC连接的接口,在接收数据的时候,会自动的去标签。

image-20240204133349071

假设有如上拓扑,我们在 VPC9 上 ping VPC15,然后分别在 S3 交换机的 e0/0、S3 交换机的e0/1、S1 交换机的 e0/0 和 S7 交换机的 e0/0 接口抓包,查看数据包封装:

S3 交换机的 e0/0 (与VPC9接入的交换机端口)

image-20240204133728415

VPC9 发送的 ICMP 数据包本身是没有打标签的。

S3 交换机的e0/1 (与VPC9接入的交换机数据出口)

image-20240204133943598

发现交换机转发数据的时候,将该数据包打了标签。

S1 交换机的 e0/0 (转发到其他交换机的交换机接口)

image-20240204134250591

S7 交换机的 e0/0 接口 (与VPC15直连的接口):

image-20240204134417023

交换机将数据转发到某个PC的时候,则会去标签,因此,VPC15 接收的则是去标签之后的,这就是 VLAN 技术。

VLAN 相关命令

VLAN 的创建

1
Switch(config)#vlan 20,30,40,50-60         # 创建 vlan 20、30、40、50到60

VLAN 的查看

1
SW3#show vlan brief  # 查看所有VLAN

image-20240204151655641

默认所有接口都在 VLAN1当中。

VLAN 的命名

1
2
SW3(config)#vlan 101   
SW3(config-vlan)#name LAN101 # 命名

image-20240204151749492

链路类型

image-20240204142540243

引言

对于交换机而言,拥有入向要打标签出向去标签行为的接口被称为 access 接口。

默认情况下,access 接口仅允许一个 VLAN 通过 ,如果要允许多个,就需要使用 trunk,因此,交换机和交换机 之间走 trunk 链路,PC 机和交换机之间走 access 链路。

PC 机在发送数据的时候是没有标签的,故而接收的数据也不能有标签,因此交换机会对入 access 接口的数据进行去标签。

trunk 链路并不会去标签,而是原封不动的进行转发,如果 trunk 链路收到的数据没有 VLAN ID,则会打上 Native VLAN ,思科路由器默认为1。

默认情况下,所有 VLAN 都在 VLAN 1当中。

image-20240204142255949

trunk

  • 用于连接两个交换机之间或连接交换机与路由器等上层设备。
  • Trunk 端口可以同时传输多个 VLAN 的数据,数据帧会带有 VLAN 标签,以标识属于哪个 VLAN。
  • 允许在单个物理链路上传输多个 VLAN 的数据,这有助于实现 VLAN 的跨交换机通信。
  • trunk 默认允许,所有 VLAN 通过,可通过命令配置

配置命令:

1
2
3
4
5
SW3(config)#int e0/1
SW3(config-if)#switchport trunk encapsulation dot1q # 新版不用配
SW3(config-if)#switchport mode trunk
# 默认允许所有VLAN通过,以下配置及允许VLAN20和VLAN30通过
SW3(config-if)#switchport trunk allowed vlan 20,30

更多命令:

image-20240204144016419

查看 trunk 接口:

1
SW3#show interface trunk

image-20240204153446577

access

  • 用于连接终端设备,如计算机、打印机等。
  • Access 端口只属于一个VLAN。
  • 交换机通过 Access 端口将设 备连接到特定的 VLAN,从而将设备划分到相应的广播域。

**配置命令: **

1
2
3
4
5
6
SW3#conf t
SW3(config)#vlan 30
SW3(config-vlan)#exit
SW3(config)#int e0/0
SW3(config-if)#switchport mode access
SW3(config-if)#switchport access vlan 30

VLAN 配置实例

image-20240204160729613

以下配置都已在配置好IP、主机名的前提下进行:

创建并加入 VLAN

SW1
1
2
3
4
5
6
7
8
9
10
11
12
SW1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#
*Feb 4 15:56:58.372: %SYS-5-CONFIG_I: Configured from console by console
SW1(config)#vlan 10,20
SW1(config-vlan)#exit
SW1(config)#int e0/0
SW1(config-if)#switchport mode access
SW1(config-if)#switchport access vlan 10
SW1(config-if)#int e0/1
SW1(config-if)#switchport mode access
SW1(config-if)#switchport access vlan 20
SW2
1
2
3
4
5
6
7
8
9
SW2#conf t  
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vlan 10,20,30
SW2(config-vlan)#exit
SW2(config)#int e0/0
SW2(config-if)#switchport mode access
SW2(config-if)#switchport access vlan 30
SW2(config-if)#exit
SW2(config)#
SW3
1
2
3
4
5
6
7
8
9
10
11
12
13
SW3#
SW3#conf t
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vlan 10,20,30
SW3(config-vlan)#int e0/0
SW3(config-if)#switchport mode access
SW3(config-if)#switchport access vlan 10
SW3(config-if)#int e0/1
SW3(config-if)#switchport mode access
SW3(config-if)#switchport access vlan 20
SW3(config-if)#int e0/2
SW3(config-if)#switchport mode access
SW3(config-if)#switchport access vlan 30

配置 trunk

SW1
1
2
3
4
5
SW1(config)#
SW1(config)#int e0/2
SW1(config-if)#switchport trunk encapsulation dot1q
SW1(config-if)#switchport mode trunk
SW1(config-if)#switchport trunk allowed vlan 10,20
SW2
1
2
3
4
5
6
7
8
9
10
SW2(config)#
SW2(config)#int e0/1
SW2(config-if)#switchport trunk encapsulation dot1q
SW2(config-if)#switchport mode trunk
SW2(config-if)#switchport trunk allowed vlan 10,20
SW2(config-if)#
SW2(config-if)#int e0/3
SW2(config-if)#switchport trunk encapsulation dot1q
SW2(config-if)#switchport mode trunk
SW2(config-if)#switchport trunk allowed vlan 10,20,30
SW3
1
2
3
4
5
6
SW3#conf t
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#int e0/3
SW3(config-if)#switchport trunk encapsulation dot1q
SW3(config-if)#switchport mode trunk
SW3(config-if)#switchport trunk allowed vlan 10,20,30

网络连通性测试

image-20240204161705880

image-20240204161736766

image-20240204161834153

VTP 协议

引言

​ ** VTP(VLAN Trunking Protocol)是思科私有的协议**。它是一种用于在交换网络中自动管理VLAN信息的协议,允许管理员在一个交换网络中配置VLAN信息,然后通过VTP自动传播这些信息到其他支持VTP的交换机上。VTP的主要目的是简化VLAN的管理,确保网络中所有的交换机都具有相同的VLAN配置。

角色

VTP Server(VTP 服务器):VTP网络中的交换机可以配置为VTP服务器。VTP服务器负责管理VLAN数据库,包括VLAN的创建、删除和重命名。服务器会把这些VLAN信息通过VTP协议传播到其他在同一VTP域中的交换机上。对VLAN数据库的更改只能由VTP服务器进行。

VTP Client(VTP 客户端):VTP客户端是受VTP服务器管理的交换机。它们从VTP服务器接收VLAN数据库信息,并将其应用于本地配置。VTP客户端无法更改VLAN数据库,只能接收并应用来自VTP服务器的更改。

VTP Transparent(VTP 透明 NA 阶段了解):VTP透明模式的交换机既不会主动传播VLAN数据库信息,也不会同步接收到的VLAN数据库信息。它们保留本地的VLAN数据库配置,但不参与VLAN数据库的传播。透明模式的交换机可以手动配置VLAN信息,而不受VTP的影响。

应用场景

image-20240205115850090

设有如上网络拓扑,不光SW2、SW3、SW4上需要存在 VLAN10 和 VLAN20,而 SW1 上也需要存在 VLAN10 和 VLAN 20。不然 SW1 是无法正确转发的。

因此我们需要在每个交换机上都创建 VLAN 10 和 VLAN 20,如果网络规模较大,我们的工作量也随之变大。

于是,我们能不能在 SW1 上配置 VTP Server,其他的交换机都是 VTP Client,然后 VLAN 从 Server 上同步呢?

以下操作均在配置好 trunk 链路下进行:

SW1:

1
2
3
4
5
6
7
SW1#
SW1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vtp mode server
Device mode already VTP Server for VLANS.
SW1(config)#vtp domain vtp01
Changing VTP domain name from NULL to vtp01

SW2:

1
2
3
4
5
6
SW2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
SW2(config)#vtp mode client
Setting device to VTP Client mode for VLANS.
SW2(config)#vtp domain vtp01
Domain name already set to vtp01.

SW3:

1
2
3
4
5
6
7
8
9
SW3#conf t
Enter configuration commands, one per line. End with CNTL/Z.
SW3(config)#vtp
*Feb 5 12:08:23.846: %SYS-5-CONFIG_I: Configured from console by console
SW3(config)#vtp mode client
Setting device to VTP Client mode for VLANS.
SW3(config)#vtp domain vtp01
Domain name already set to vtp01.
SW3(config)#

SW4:

1
2
3
4
5
6
7
8
9
SW4#
SW4#conf t
Enter configuration commands, one per line. End with CNTL/Z.
SW4(config)#we
*Feb 5 12:08:35.694: %SYS-5-CONFIG_I: Configured from console by console
SW4(config)#vtp mode client
Setting device to VTP Client mode for VLANS.
SW4(config)#vtp domain vtp01
Domain name already set to vtp01.

接着来到 SW1 上创建VLAN:

1
2
3
SW1(config)#
SW1(config)#vlan 10,20
SW1(config-vlan)#exit

其余 交换机(VTP 客户端)自动同步该VLAN:

image-20240205121212480

image-20240205121238977

image-20240205121301011

接着我们只需要手动将接口加入到响应VLAN即可。VTP 只会同步 VLAN 的创建,并不会同步 VLAN 接口、允许哪些 VLAN 通行等信息。

相关命令

image-20240205121947632

VLAN 间路由

引言

​ 部署了 VLAN 的传统交换机不能实现不同 VLAN 间的二层报文转发,因此必须引入路由技术来实现不同 VLAN 间的通信。VLAN 路由可以通过 二层交换机配合路由器 来实现,也可以通过 三层交换机来实现

VLAN 的局限性

image-20240205122609845

VLAN 在分割广播域的同时也限制了不同 VLAN 间的主机进行二层通信的能力。

不合适的 VLAN 路由

image-20240205124246891

二层交换机上配置 VLAN,每一个 VLAN 使用一条独占的物理链路连接到路由器的一个接口上。

但是 我们之前说过,路由器的接口有限,而 VLAN 的个数有时候远远超过路由器的接口数,故而我们就需要增加路由器的数量,从而增加成本。

具体实验:

image-20240205124455964

交换机配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 创建 VLAN 并将 PC 加入到相应 VLAN 
Switch#
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#vlan 10,20
Switch(config-vlan)#exit
Switch(config)#int e0/0
Switch(config-if)#sw ac vlan 10
Switch(config-if)#int e0/1
Switch(config-if)#switchport ac vlan 20

# 将与路由器相连的接口分别加入到相应 VLAN
Switch(config)#int e0/2
Switch(config-if)#switchport acc vlan 10
Switch(config-if)#int e0/3
Switch(config-if)#switchport ac vlan 20

路由器配置:

1
2
3
4
5
6
Router(config)#int e0/2 
Router(config-if)#ip add 192.168.10.254 255.255.255.0
Router(config-if)#no shutdown
Router(config-if)#int e0/3
Router(config-if)#ip add 192.168.20.254 255.255.255.0
Router(config-if)#no shutdown

单臂路由

image-20240205125438164

将交换机和路由器之间的链路配置为 Trunk 链路,并且在路由器上创建子接口以支持 VLAN 路由。

这里可能有个疑问,路由器的接口不是无法接收带有 VLAN ID 的数据包吗,是的,但是路由器的子接口是可以接收带有 VLAN ID 的数据包的,因此可以将该链路设置为 Trunk

具体实验:

image-20240205131633514

Switch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 将 PC 加入到 相应 VLAN
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#vlan 10,20
Switch(config-vlan)#exit
Switch(config)#int e0/0
Switch(config-if)#switchport access vlan 10
Switch(config-if)#int e0/1
Switch(config-if)#switchport access vlan 20

# 将与路由器相连的接口配置为 trunk
Switch(config-if)#int e0/2
Switch(config-if)#switchport trunk encapsulation dot1q
Switch(config-if)#switchport mode trunk

Router:

1
2
3
4
5
6
7
8
9
10
11
12
# 进入到子接口并为其服务的VLAN ID、接口的IP地址
Router#conf t
Router(config)#interface ethernet 0/2.10
Router(config-subif)#encapsulation dot1Q 10
Router(config-subif)#ip add 192.168.10.254 255.255.255.0
Router(config-subif)#interface ethernet 0/2.20
Router(config-subif)#encapsulation dot1Q 20
Router(config-subif)#ip add 192.168.20.254 255.255.255.0

# 开启子接口需要将物理接口 up
Router(config-subif)#int e0/2
Router(config-if)#no shutdown

三层交换

image-20240205140321803

为每个 VLAN 创建一个SVI接口(layer3) 作为网关

具体实验:

image-20240205141016089

Switch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 创建 VLAN 并将 PC 加入到相应 VLAN 
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#vlan 20,30
Switch(config-vlan)#exit
Switch(config)#int e0/0
Switch(config-if)#switchport access vlan 20
Switch(config-if)#int e0/1
Switch(config-if)#switchport access vlan 20
Switch(config-if)#int e0/2
Switch(config-if)#switchport access vlan 30
Switch(config-if)#int e0/3
Switch(config-if)#switchport access vlan 30
Switch(config-if)#end

# 进入到 VLAN 接口 并设置 IP 地址 作为网关
Switch#conf t
Switch(config)#int vlan 20
Switch(config-if)#ip add 192.168.20.254 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#int vlan 30
Switch(config-if)#ip add 192.168.30.254 255.255.255.0
Switch(config-if)#no shutdown

DHCP 服务

引言

​ 在大型企业网络中,会有大量的主机或设备需要获取IP地址等网络参数。如果采用手工配置,工作量大且不好管理,如果有用户擅自修改网络参数,还有可能会造成IP地址冲突等问题。使用动态主机配置协议DHCP(Dynamic Host Configuration Protocol)来分配IP地址等网络参数,可以减少管理员的工作量,避免用户手工配置网络参数时造成的地址冲突。

DHCP 的配置可以通过路由器、三层交换机、Windows 、Linux 进行配置。

DHCP 报文类型

报文类型 含义
DHCP DISCOVER 客户端用来寻找DHCP服务器。
DHCP OFFER DHCP服务器用来响应DHCP DISCOVER报文,此报文携带了各种配置信息。
DHCP REQUEST 客户端请求配置确认,或者续借租期。
DHCP ACK 服务器对REQUEST报文的确认响应。
DHCP NAK 服务器对REQUEST报文的拒绝响应。
DHCP RELEASE 客户端要释放地址时用来通知服务器。

地址池

image-20240205143017828

DHCP 工作原理

image-20240205142558348

DHCP 租期更新

image-20240205142628822

lIP租约期限到达50%时,DHCP客户端会请求更新IP地址租约。

DHCP 重绑定

image-20240205142708438

DHCP客户端在租约期限到达87.5%时,还没收到服务器响应,会申请重绑定IP。

IP 地址释放

image-20240205142811964

如果IP租约到期前都没有收到服务器响应,客户端停止使用此IP地址。

如果DHCP客户端不再使用分配的IP地址,也可以主动向DHCP服务器发送DHCP RELEASE报文,释放该IP地址。

配置 DHCP 示例

实例1

image-20240205163221690

1
2
3
4
5
6
7
8
9
10
11
12
13
Switch#en
Switch#conf t
Switch(config)#service dhcp # 开启DHCP
Switch(config)#ip dhcp pool pool_1
Switch(dhcp-config)#network 192.168.1.0 255.255.255.0 # 设置分配的网络
Switch(dhcp-config)#default-route 192.168.1.254 # 设置默认路由
Switch(dhcp-config)#dns-server 114.114.114.114 8.8.8.8 # 设置DNS
Switch(dhcp-config)#lease 7 3 30 # 设置租期
Switch(dhcp-config)#exit
Switch(config)#ip dhcp excluded-address 192.168.192.168.1.254 # 排除地址
Switch(config)#interface vlan 1 # 进入 VLAN
Switch(config-if)#ip add 192.168.1.1 255.255.255.0 # 为VLAN配置IP
Switch(config-if)#no shutdown # 开启接口

实例2

image-20240205164437304

注:以下操作均在将对应PC划分到对应VLAN下进行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Switch(config)#  
Switch(config)#service dhcp
Switch(config)#ip dhcp pool vlan20
Switch(dhcp-config)#network 192.168.20.0 255.255.255.0
Switch(dhcp-config)#default-route 192.168.20.254
Switch(dhcp-config)#dns-server 114.114.114.114
Switch(dhcp-config)#exit
Switch(config)#ip dhcp excluded-address 192.168.20.254
Switch(config)#int vlan 20
Switch(config-if)#ip add 192.168.20.254 255.255.255.0
Switch(config-if)#ip helper-address 192.168.20.254
Switch(config-if)#no shutdown

Switch(config-if)#ip dhcp pool vlan30
Switch(dhcp-config)#network 192.168.30.0 255.255.255.0
Switch(dhcp-config)#default-route 192.168.30.254
Switch(dhcp-config)#dns-server 114.114.114.114
Switch(dhcp-config)#exit
Switch(config)#ip dhcp excluded-address 192.168.30.254
Switch(config)#int vlan 30
Switch(config-if)#ip add 192.168.30.254 255.255.255.0
Switch(config-if)#ip helper-address 192.168.30.254
Switch(config-if)#no shutdown

CDP 协议

Cisco Discovery Protocol 是一个专有实用程序,它提供直接连接的交换机、路由器和其他Cisco设备的摘要。思科私有协议,默认开启。

image-20240205165452428 image-20240205165628188
1
2
3
4
5
6
7
8
9
10
11
12
Switch#show cdp   
Global CDP information:
Sending CDP packets every 60 seconds
Sending a holdtime value of 180 seconds
Sending CDPv2 advertisements is enabled
Switch#show cdp neighbors
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote, C - CVTA, M - Two-port Mac Relay

Device ID Local Intrfce Holdtme Capability Platform Port ID
Router Eth 0/2 154 R B Linux Uni Eth 0/2