华为交换机作为企业网络的核心设备,其命令配置是网络管理的基础,掌握常用命令可有效提升网络部署、运维效率,以下从基础配置、VLAN管理、接口设置、路由协议、安全策略、链路聚合及系统维护等方面汇总常用命令,并辅以说明。
基础配置命令
基础配置是交换机管理的起点,主要包括模式切换、设备命名及密码设置等。
命令 | 作用 | 示例 |
---|---|---|
< |
用户模式提示符 | |
> |
特权模式提示符 | < system-view > |
[ ] |
系统视图提示符 | < system-view > [ ] |
system-view |
进入系统视图(全局配置模式) | [ ] system-view |
sysname |
设置设备名称 | [ ] sysname Switch-A |
user-interface console 0 |
进入Console用户界面 | [ ] user-interface console 0 |
set authentication password cipher 密码 |
设置Console登录密码 | [ Switch-A-ui-console0] set authentication password cipher Huawei@123 |
user-interface vty 0 4 |
进入VTY(远程登录)用户界面 | [ ] user-interface vty 0 4 |
set authentication password cipher 密码 |
设置VTY登录密码 | [ Switch-A-ui-vty0-4] set authentication password cipher Huawei@123 |
user privilege level level |
设置用户权限级别 | [ Switch-A-ui-vty0-4] user privilege level 3 |
quit |
退出当前视图 | [ Switch-A] quit |
return |
直接返回用户模式 | [ Switch-A] return |
save |
保存当前配置 | < save > |
VLAN配置命令
VLAN(虚拟局域网)可隔离广播域,提升网络安全性,常用命令包括VLAN创建、端口划分及查看。
命令 | 作用 | 示例 |
---|---|---|
vlan batch VLAN_ID |
批量创建VLAN(单个VLAN用vlan VLAN_ID ) |
[ Switch-A] vlan batch 10 20 |
undo vlan VLAN_ID |
删除指定VLAN | [ Switch-A] undo vlan 10 |
interface interface-type interface-number |
进入接口视图(如GigabitEthernet 0/0/1 ) |
[ Switch-A] interface GigabitEthernet 0/0/1 |
port link-type access |
设置接口为Access模式(连接终端设备) | [ Switch-A-GigabitEthernet0/0/1] port link-type access |
port default vlan VLAN_ID |
将Access端口加入指定VLAN | [ Switch-A-GigabitEthernet0/0/1] port default vlan 10 |
port link-type trunk |
设置接口为Trunk模式(连接交换机,允许多VLAN通过) | [ Switch-A-GigabitEthernet0/0/2] port link-type trunk |
port trunk allow-pass vlan VLAN_ID |
配置Trunk端口允许通过的VLAN(默认允许VLAN 1) | [ Switch-A-GigabitEthernet0/0/2] port trunk allow-pass vlan 10 20 |
display vlan |
查看VLAN配置信息 | [ Switch-A] display vlan |
display port vlan |
查看端口VLAN成员关系 | [ Switch-A] display port vlan |
接口配置命令
接口配置涉及物理参数、IP地址及状态管理,是网络通信的基础。
命令 | 作用 | 示例 |
---|---|---|
undo shutdown |
开启接口(默认开启) | [ Switch-A-GigabitEthernet0/0/1] undo shutdown |
shutdown |
关闭接口 | [ Switch-A-GigabitEthernet0/0/1] shutdown |
speed {10 | 100 | 1000 | auto} |
设置接口速率(auto为自适应) | [ Switch-A-GigabitEthernet0/0/1] speed 1000 |
duplex {full | half | auto} |
设置接口双工模式(full为全双工) | [ Switch-A-GigabitEthernet0/0/1] duplex full |
description 接口描述 |
设置接口描述信息(便于管理) | [ Switch-A-GigabitEthernet0/0/1] description To-Server |
interface Vlanif VLAN_ID |
进入VLAN接口视图(三层交换机配置SVI) | [ Switch-A] interface Vlanif 10 |
ip address IP_ADDRESS SUBNET_MASK |
配置VLAN接口IP地址(网关地址) | [ Switch-A-Vlanif10] ip address 192.168.10.1 24 |
undo ip address |
删除VLAN接口IP地址 | [ Switch-A-Vlanif10] undo ip address |
display ip interface brief |
查看接口IP地址及状态 | [ Switch-A] display ip interface brief |
路由配置命令
三层交换机需配置路由协议以实现跨网段通信,常用静态路由与OSPF。
命令 | 作用 | 示例 |
---|---|---|
ip route-static DESTINATION_MASK NEXT_HOP_ADDRESS |
配置静态路由 | [ Switch-A] ip route-static 192.168.30.0 24 192.168.20.1 |
ip route-static DESTINATION_MASK NEXT_HOP_ADDRESS preference VALUE |
配置静态路由优先级(默认60,值越小优先级越高) | [ Switch-A] ip route-static 192.168.30.0 24 192.168.20.2 preference 70 |
ospf 1 |
启动OSPF进程(进程号1,本地有效) | [ Switch-A] ospf 1 |
router-id ROUTER_ID |
配置OSPF Router ID(需唯一,通常用设备Loopback地址) | [ Switch-A-ospf-1] router-id 1.1.1.1 |
area AREA_ID |
进入OSPF区域视图(骨干区域为0) | [ Switch-A-ospf-1] area 0 |
network IP_ADDRESS SUBNET_MASK AREA_ID |
宣告网段参与OSPF(需与接口IP网段匹配) | [ Switch-A-ospf-1-area-0.0.0.0] network 192.168.10.0 0.0.0.255 |
display ip routing-table |
查看路由表 | [ Switch-A] display ip routing-table |
display ospf peer |
查看OSPF邻居关系状态(Full表示邻接成功) | [ Switch-A] display ospf peer |
安全配置命令
安全策略可防止未授权访问及网络攻击,常用端口安全、ACL及DHCP Snooping。
命令 | 作用 | 示例 |
---|---|---|
port-security max-mac-address NUMBER |
设置端口最大MAC地址数(默认为1) | [ Switch-A-GigabitEthernet0/0/1] port-security max-mac-address 2 |
port-security sticky |
开启端口安全MAC地址学习(绑定MAC) | [ Switch-A-GigabitEthernet0/0/1] port-security sticky |
port-security violation {restrict | shutdown} |
设置安全违规动作(restrict丢弃违规报文并告警;shutdown关闭端口) | [ Switch-A-GigabitEthernet0/0/1] port-security violation shutdown |
acl number ACL_NUMBER |
创建ACL(基本ACL:2000-2999;高级ACL:3000-3999) | [ Switch-A] acl 3000 |
rule [rule-id] {permit | deny} [source IP_ADDRESS SUBNET_MASK] |
配置ACL规则(rule-id可选,默认按顺序匹配) | [ Switch-A-acl-adv-3000] rule 5 permit source 192.168.10.0 0.0.0.255 |
traffic classifier NAME operator {and | or} |
创建流分类(operator:and为逻辑与,or为逻辑或) | [ Switch-A] traffic classifier c1 operator or |
if-match acl ACL_NUMBER |
为流分类匹配ACL规则 | [ Switch-A-classifier-c1] if-match acl 3000 |
traffic behavior NAME |
创建流行为 | [ Switch-A] traffic behavior b1 |
deny |
设置流行为为拒绝流量 | [ Switch-A-behavior-b1] deny |
traffic policy NAME |
创建流策略 | [ Switch-A] traffic policy p1 |
classifier NAME behavior NAME |
将流分类与流行为绑定到流策略 | [ Switch-A-trafficpolicy-p1] classifier c1 behavior b1 |
interface interface-type interface-number |
进入接口视图应用流策略 | [ Switch-A] interface GigabitEthernet 0/0/1 |
traffic-policy NAME inbound |
在接口 inbound 方向应用流策略 | [ Switch-A-GigabitEthernet0/0/1] traffic-policy p1 inbound |
dhcp snooping enable |
全局开启DHCP Snooping(信任DHCP服务器报文) | [ Switch-A] dhcp snooping enable |
dhcp snooping trusted |
设置接口为DHCP Snooping信任端口(通常连接DHCP服务器) | [ Switch-A-GigabitEthernet0/0/2] dhcp snooping trusted |
链路聚合与STP配置
链路聚合(Eth-Trunk)可提升带宽及冗余,生成树协议(STP)可防止环路。
命令 | 作用 | 示例 |
---|---|---|
interface Eth-Trunk TRUNK_ID |
创建并进入Eth-Trunk接口视图 | [ Switch-A] interface Eth-Trunk 1 |
mode lacp-static |
设置Eth-Trunk模式为LACP静态聚合(可选static 静态负载均衡) |
[ Switch-A-Eth-Trunk1] mode lacp-static |
eth-trunk TRUNK_ID |
将接口加入Eth-Trunk | [ Switch-A-GigabitEthernet0/0/3] eth-trunk 1 |
display eth-trunk TRUNK_ID |
查看Eth-Trunk聚合状态 | [ Switch-A] display eth-trunk 1 |
stp mode rstp |
设置STP模式为RSTP(快速生成树,默认为STP) | [ Switch-A] stp mode rstp |
stp root primary |
将交换机配置为根桥(优先级0,最低) | [ Switch-A] stp root primary |
stp root secondary |
将交换机配置为备份根桥(优先级4096) | [ Switch-A] stp root secondary |
display stp brief |
查看STP端口角色及状态(Root根端口、Designated指定端口) | [ Switch-A] display stp brief |
系统维护命令
系统维护命令用于查看设备状态、日志及文件管理,便于故障排查。
命令 | 作用 | 示例 |
---|---|---|
display version |
查看设备版本信息(软件版本、硬件信息、运行时间) | [ Switch-A] display version |
display device |
查看设备硬件状态(风扇、电源、温度) | [ Switch-A] display device |
display current-configuration |
查看当前运行配置(未保存的配置) | [ Switch-A] display current-configuration |
display saved-configuration |
查看已保存配置(下次启动生效的配置) | [ Switch-A] display saved-configuration |
reset saved-configuration |
删除已保存配置(恢复出厂默认) | < reset saved-configuration > |
reboot |
重启设备 | < reboot > |
logbuffer informational |
设置日志缓冲区级别(informational及以上日志) | [ Switch-A] logbuffer informational |
display logbuffer |
查看系统日志(记录设备操作及错误信息) | [ Switch-A] display logbuffer |
相关问答FAQs
Q1:华为交换机如何将接口从Access模式改为Trunk模式?
A:操作步骤如下:
- 进入系统视图:
system-view
- 进入目标接口视图:
interface GigabitEthernet 0/0/1
- 删除原有VLAN配置(若接口已加入VLAN):
undo port default vlan
- 修改接口类型为Trunk:
port link-type trunk
- 配置允许通过的VLAN(如允许VLAN 10、20):
port trunk allow-pass vlan 10 20
- 保存配置:
save
Q2:华为交换机配置静态路由后无法通信,可能的原因及排查方法?
A:可能原因及排查步骤如下:
- 路由配置错误:检查目标网络、子网掩码、下一跳地址是否正确。
- 排查:使用
display ip routing-table
查看路由表,确认是否存在目标路由及下一跳是否正确。
- 排查:使用
- 下一跳接口状态异常:下一跳接口可能被关闭或未配置IP。
- 排查:使用
display ip interface brief
检查下一跳接口状态(需为UP
),若关闭则执行undo shutdown
。
- 排查:使用
- ACL拦截:接口可能配置了ACL拒绝流量通过。
- 排查:使用
display traffic-policy applied-record
查看接口应用的流策略,检查ACL规则是否误拦截。
- 排查:使用
- 路由未生效:配置后未保存或重启设备。
- 排查:执行
save
保存配置,若仍不生效可尝试reboot
重启设备。
- 排查:执行