交换机是网络中的核心设备,其线路配置的正确性直接影响网络的稳定性与性能,交换机的线路配置命令主要涉及接口的基本参数设置、VLAN划分、链路聚合、安全策略等,不同品牌交换机的命令略有差异,但核心逻辑一致,以下以华为(Huawei)和思科(Cisco)两大主流品牌为例,详细解析交换机线路配置的关键命令及操作步骤。

接口基本配置
接口是交换机与终端设备(如PC、服务器、其他交换机)连接的通道,基本配置包括接口启用、速率/双工模式设置、IP地址配置(若为管理接口)等。
华为交换机接口基本配置
-
进入接口视图
命令:system-view
(进入系统视图)
interface GigabitEthernet 0/0/1
(进入千兆以太网接口0/0/1,具体接口号根据设备型号调整) -
启用/禁用接口
命令:undo shutdown
(启用接口,默认通常为开启状态)
shutdown
(禁用接口) -
设置接口速率与双工模式
命令:speed {10 | 100 | 1000 | auto}
(速率可选10Mbps、100Mbps、1000Mbps或自适应)
duplex {half | full | auto}
(双工模式可选半双工、全双工或自适应)
示例:speed 1000
,duplex full
(强制设置千兆全双工模式)(图片来源网络,侵删) -
配置管理IP地址
交换机需通过管理IP远程登录,通常使用VLANIF接口(虚拟接口)绑定VLAN实现。
命令:vlan 10
(创建VLAN 10)
interface Vlanif 10
(进入VLAN 10接口视图)
ip address 192.168.1.1 24
(配置IP地址与子网掩码)
quit
(返回系统视图)
interface GigabitEthernet 0/0/1
(进入物理接口)
port link-type access
(设置接口为接入链路模式,连接终端设备)
port default vlan 10
(将接口划入VLAN 10)
思科交换机接口基本配置
-
进入全局配置模式与接口视图
命令:enable
(从用户模式进入特权模式)
configure terminal
(进入全局配置模式)
interface GigabitEthernet0/1
(进入接口0/1,思科接口号格式为“插槽/端口”) -
启用/禁用接口
命令:no shutdown
(启用接口,默认关闭)
shutdown
(禁用接口) -
设置接口速率与双工模式
命令:speed auto
(自适应,默认)
speed 1000
,duplex full
(强制千兆全双工)(图片来源网络,侵删) -
配置管理IP地址
命令:interface vlan 1
(默认VLAN 1作为管理VLAN)
ip address 192.168.1.1 255.255.255.0
(配置IP与子网掩码)
no shutdown
(启用VLAN接口)
VLAN配置
VLAN(虚拟局域网)可将物理网络划分为多个逻辑子网,隔离广播域,提升网络安全性。
华为交换机VLAN配置
-
创建VLAN
命令:vlan 20
(创建VLAN 20,若VLAN存在则直接进入VLAN视图) -
命名VLAN(可选)
命令:description Sales
(为VLAN 20添加描述“销售部”) -
将接口划入VLAN
-
接入链路模式(Access):连接终端设备(如PC)的接口需设置为Access模式,且只能属于一个VLAN。
命令:interface GigabitEthernet 0/0/2
port link-type access
port default vlan 20
-
trunk链路模式:连接其他交换机的接口需设置为Trunk模式,可承载多个VLAN的流量(默认所有VLAN通过,可过滤)。
命令:interface GigabitEthernet 0/0/24
port link-type trunk
port trunk allow-pass vlan 20 30
(仅允许VLAN 20和30通过Trunk链路)
-
思科交换机VLAN配置
-
创建VLAN
命令:vlan 20
-
命名VLAN
命令:name Sales
-
将接口划入VLAN
-
Access模式:
命令:interface range fastethernet0/2-4
(批量配置接口0/2至0/4)
switchport mode access
switchport access vlan 20
-
Trunk模式:
命令:interface gigabitethernet0/24
switchport mode trunk
switchport trunk allowed vlan 20,30
(允许VLAN 20和30通过)
-
链路聚合配置
链路聚合(Eth-Trunk/Port-Channel)将多个物理接口捆绑为一个逻辑接口,提升带宽并实现冗余备份。
华为交换机Eth-Trunk配置
-
创建Eth-Trunk接口
命令:interface Eth-Trunk 1
(创建Eth-Trunk 1) -
将物理接口加入Eth-Trunk
命令:interface range GigabitEthernet 0/0/1 to GigabitEthernet 0/0/2
(批量选择接口)
eth-trunk 1
(将接口加入Eth-Trunk 1) -
设置Eth-Trunk模式
命令:mode manual load-balance
(手动模式,根据负载均衡算法转发流量)
mode lacp-static
(LACP静态模式,需对端设备支持LACP)
思科交换机Port-Channel配置
-
创建Port-Channel接口
命令:interface range gigabitethernet0/1-2
channel-group 1 mode active
(LACP主动模式,推荐使用LACP实现动态聚合) -
配置接口属性
命令:interface port-channel 1
switchport mode trunk
switchport trunk allowed vlan 20,30
接口安全配置
为防止未授权设备接入,可配置端口安全功能,限制接口下的MAC地址数量。
华为交换机端口安全
-
启用端口安全
命令:interface GigabitEthernet 0/0/1
port-security enable
-
设置最大MAC地址数
命令:port-security max-mac-num 2
(限制接口最多学习2个MAC地址) -
配置违规处理方式
命令:port-security violation protect
(保护模式:丢弃违规报文但不禁用接口)
(可选:restrict
:丢弃违规报文并记录日志;shutdown
:禁用接口)
思科交换机端口安全
-
启用端口安全
命令:interface fastethernet0/1
switchport port-security
-
设置最大MAC地址数
命令:switchport port-security maximum 2
-
配置违规处理方式
命令:switchport port-security violation protect
配置保存与验证
完成配置后,需保存配置并验证接口状态。
华为交换机
-
保存配置
命令:save
(保存到当前配置文件,重启后仍生效) -
验证命令
display interface brief
(查看所有接口状态,包括UP/DOWN、速率等)
display vlan
(查看VLAN配置信息)
display eth-trunk
(查看Eth-Trunk聚合状态)
思科交换机
-
保存配置
命令:end
(退出到特权模式)
copy running-config startup-config
(将当前运行配置保存到启动配置) -
验证命令
show ip interface brief
(查看接口IP与状态)
show vlan brief
(查看VLAN信息)
show etherchannel summary
(查看链路聚合状态)
不同品牌命令对比(表格)
功能 | 华为交换机命令 | 思科交换机命令 |
---|---|---|
进入系统视图 | system-view |
configure terminal |
启用接口 | undo shutdown |
no shutdown |
设置接口速率 | speed 1000 |
speed 1000 |
创建VLAN | vlan 20 |
vlan 20 |
接口划入VLAN(Access) | port link-type access port default vlan 20 |
switchport mode access switchport access vlan 20 |
Trunk模式配置 | port link-type trunk port trunk allow-pass vlan 20 |
switchport mode trunk switchport trunk allowed vlan 20 |
链路聚合创建 | interface Eth-Trunk 1 eth-trunk 1 |
interface range gi0/1-2 channel-group 1 mode active |
端口安全启用 | port-security enable |
switchport port-security |
保存配置 | save |
copy running-config startup-config |
相关问答FAQs
问题1:交换机接口显示“down”状态,可能的原因及排查步骤?
解答:接口“down”状态通常由物理连接问题或配置错误导致,排查步骤如下:
- 物理连接检查:确认网线两端是否牢固连接至接口,网线类型是否正确(如千兆接口需使用超五类或六类线),对端设备(如PC、交换机)接口是否正常工作。
- 接口配置检查:确认接口是否被手动禁用(华为
display current-configuration interface [接口名]
查看shutdown
配置;思科show running-config interface [接口名]
查看shutdown
命令)。 - VLAN与Trunk配置:若接口为Trunk模式,检查允许通过的VLAN列表是否正确;若为Access模式,确认是否划入正确的VLAN。
- 硬件故障:若以上均正常,可能是接口模块或设备硬件故障,需更换接口或设备。
问题2:如何配置交换机以实现不同VLAN之间的通信?
解答:VLAN间通信需通过三层设备(如三层交换机或路由器)实现,核心是配置VIF接口(虚拟接口)作为VLAN的网关,以华为三层交换机为例:
- 创建VLAN并划分接口:按上述步骤创建VLAN 10和VLAN 20,并将对应接口划入各VLAN(Access模式)。
- 启用VIF接口并配置IP:
interface Vlanif 10
,ip address 192.168.10.1 24
(VLAN 10网关)interface Vlanif 20
,ip address 192.168.20.1 24
(VLAN 20网关)
- 启用IP路由功能:
ip routing
(华为交换机默认关闭,需手动开启;思科交换机默认开启)。 - 验证连通性:在VLAN 10内的PC上 ping VLAN 20内的PC网关(192.168.20.1),若通则说明VIF路由正常,可跨VLAN通信。
若使用路由器实现,需将路由器与交换机连接的接口配置为Trunk模式,并在路由器子接口上配置IP地址(如interface GigabitEthernet0/0.1
,encapsulation dot1Q 10
,ip address 192.168.10.1 24
),其余步骤类似。