网络交换机是构建局域网(LAN)的核心设备,负责在数据链路层转发帧。其配置方法因品牌(如Cisco、华为、H3C、TP-Link等)和型号而异,但基本原理相通。本文将系统介绍交换机的详细配置方法,涵盖基础访问、VLAN、链路聚合、生成树、端口安全、管理IP等关键环节。
Switch> enable
Switch# configure terminal
Switch(config)# hostname SW1
SW1(config)# enable secret cisco123 # 设置特权密码
SW1(config)# line console 0
SW1(config-line)# password console123
SW1(config-line)# login
SW1(config-line)# exit
SW1(config)# line vty 0 4
SW1(config-line)# password telnet123
SW1(config-line)# login
SW1(config-line)# transport input ssh # 推荐使用SSH
SW1(config-line)# exit
<Huawei> system-view
[Huawei] sysname SW1
[SW1] user-interface console 0
[SW1-ui-console0] authentication-mode password
[SW1-ui-console0] set authentication password cipher console123
[SW1-ui-console0] quit
[SW1] user-interface vty 0 4
[SW1-ui-vty0-4] authentication-mode aaa
[SW1-ui-vty0-4] protocol inbound ssh
[SW1-ui-vty0-4] quit
[SW1] aaa
[SW1-aaa] local-user admin password cipher admin123
[SW1-aaa] local-user admin privilege level 15
[SW1-aaa] local-user admin service-type ssh
VLAN用于隔离广播域,提升安全性和性能。
### 创建VLAN并将端口加入
Cisco:`
SW1(config)# vlan 10
SW1(config-vlan)# name Sales
SW1(config-vlan)# exit
SW1(config)# interface f0/1
SW1(config-if)# switchport mode access
SW1(config-if)# switchport access vlan 10
SW1(config-if)# exit`
华为:`
[SW1] vlan 10
[SW1-vlan10] description Sales
[SW1-vlan10] quit
[SW1] interface GigabitEthernet0/0/1
[SW1-GigabitEthernet0/0/1] port link-type access
[SW1-GigabitEthernet0/0/1] port default vlan 10
[SW1-GigabitEthernet0/0/1] quit`
Cisco:`
SW1(config)# interface f0/24
SW1(config-if)# switchport mode trunk
SW1(config-if)# switchport trunk allowed vlan 10,20`
华为:`
[SW1] interface GigabitEthernet0/0/24
[SW1-GigabitEthernet0/0/24] port link-type trunk
[SW1-GigabitEthernet0/0/24] port trunk allow-pass vlan 10 20`
为交换机配置管理IP,便于远程登录。
Cisco:`
SW1(config)# interface vlan 1
SW1(config-if)# ip address 192.168.1.100 255.255.255.0
SW1(config-if)# no shutdown
SW1(config-if)# exit
SW1(config)# ip default-gateway 192.168.1.1`
华为:`
[SW1] interface Vlanif 1
[SW1-Vlanif1] ip address 192.168.1.100 24
[SW1-Vlanif1] quit
[SW1] ip route-static 0.0.0.0 0 192.168.1.1`
增加带宽并提供冗余。
Cisco:`
SW1(config)# interface range f0/1-2
SW1(config-if-range)# channel-group 1 mode active
SW1(config-if-range)# exit
SW1(config)# interface port-channel 1
SW1(config-if)# switchport mode trunk`
华为:`
[SW1] interface Eth-Trunk 1
[SW1-Eth-Trunk1] mode lacp-static
[SW1-Eth-Trunk1] port link-type trunk
[SW1-Eth-Trunk1] port trunk allow-pass vlan 10 20
[SW1-Eth-Trunk1] quit
[SW1] interface GigabitEthernet0/0/1
[SW1-GigabitEthernet0/0/1] eth-trunk 1
[SW1-GigabitEthernet0/0/1] quit
[SW1] interface GigabitEthernet0/0/2
[SW1-GigabitEthernet0/0/2] eth-trunk 1`
防止二层环路。
Cisco(快速PVST+):`
SW1(config)# spanning-tree mode rapid-pvst
SW1(config)# spanning-tree vlan 10 root primary`
华为(RSTP/MSTP):`
[SW1] stp mode rstp
[SW1] stp root primary`
限制端口接入的设备数量,防止MAC泛洪。
Cisco:`
SW1(config)# interface f0/1
SW1(config-if)# switchport port-security
SW1(config-if)# switchport port-security maximum 2
SW1(config-if)# switchport port-security violation restrict
SW1(config-if)# switchport port-security mac-address sticky`
华为:`
[SW1] interface GigabitEthernet0/0/1
[SW1-GigabitEthernet0/0/1] port-security enable
[SW1-GigabitEthernet0/0/1] port-security max-mac-num 2
[SW1-GigabitEthernet0/0/1] port-security protect-action restrict`
Cisco:`
SW1# copy running-config startup-config
SW1# copy running-config tftp:`
华为:`
[SW1] save`
交换机配置需根据网络需求灵活调整。建议先在实验环境验证,再应用到生产网络。掌握VLAN、Trunk、链路聚合、STP和端口安全是核心技能。不同品牌命令有差异,但逻辑一致。定期备份配置,并关注安全加固(如禁用未用端口、使用SSH)。通过本文的步骤,您可以完成大多数中小型网络的交换机配置。
如若转载,请注明出处:http://www.whdkwc.com/product/39.html
更新时间:2026-09-19 16:48:38
PRODUCT