交换机命令行界面(CLI)是网络管理员管理交换机的主要方式,通过输入特定指令可实现设备配置、状态监控、故障排查等功能,掌握交换机命令的使用需先理解其操作模式,不同模式下可执行的命令权限不同,常见的操作模式包括用户模式、特权模式、全局配置模式、接口配置模式等,每种模式有独特的提示符和可用命令集。

基础操作模式及常用命令
用户模式(User Mode)
用户模式是进入交换机后的初始模式,提示符通常为Switch>,仅能执行有限的查看命令,无法进行配置。
常用命令:
- 查看所有可用命令及简要说明。
show version:查看交换机版本、硬件信息、启动时间等。show running-config:查看当前运行的配置(需在特权模式下执行)。show mac-address-table:查看MAC地址表。
示例:
Switch> show version
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE4, RELEASE SOFTWARE (fc1) ...
特权模式(Privileged Mode)
特权模式在用户模式下通过enable命令进入,提示符为Switch#,可执行查看、保存配置、重启设备等高级操作。
进入/退出命令:
- 进入:
enable(可简写为en)。 - 退出:
disable(返回用户模式)。
常用命令:
show interfaces:查看所有接口的详细状态(如up/down、流量统计)。show vlan brief:查看VLAN摘要信息(VLAN ID、名称、包含接口)。write或copy running-config startup-config:保存当前配置到启动配置(避免重启丢失)。reload:重启交换机。
示例:
Switch> enable
Switch# show interfaces fastethernet 0/1
FastEthernet0/1 is up, line protocol is up
Hardware is Fast Ethernet, address is 001a.2b3c.4d5e (bia 001a.2b3c.4d5e)
...
全局配置模式(Global Configuration Mode)
全局配置模式在特权模式下通过configure terminal(简写conf t)进入,提示符为Switch(config)#,可对交换机的整体参数进行配置(如主机名、密码、VLAN等)。
进入/退出命令:

- 进入:
configure terminal。 - 退出:
end(直接返回特权模式)或Ctrl+Z。
常用命令:
hostname Switch1:设置交换机主机名。enable secret cisco:设置特权模式加密密码(安全级别高于enable password)。line console 0:进入控制台线路配置模式(用于设置Console登录密码)。line vty 0 15:进入虚拟终端线路配置模式(用于设置Telnet/SSH登录密码)。
示例:
Switch# configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)# hostname SW-Core
SW-Core(config)# enable secret Admin@123
接口配置模式(Interface Configuration Mode)
接口配置模式在全局配置模式下通过interface命令进入,提示符为SW-Core(config-if)#,用于配置具体接口参数(如速率、双工模式、VLAN划分)。
进入/退出命令:
- 进入:
interface fastethernet 0/1(或interface gigabitethernet 1/0/1,根据接口类型)。 - 退出:
exit(返回全局配置模式)或end(返回特权模式)。
常用命令:
description Link to Router-Fa0/0:设置接口描述信息(便于管理)。speed 100:设置接口速率(10/100/1000,根据接口类型支持)。duplex full:设置双工模式(half/full/auto)。no shutdown:开启接口(默认关闭状态,需手动激活)。switchport mode access:将接口设置为接入模式(连接终端设备)。switchport access vlan 10:将接入模式接口加入VLAN 10。
示例:
SW-Core(config)# interface gigabitethernet 1/0/1
SW-Core(config-if)# description Connect to Sales-PC
SW-Core(config-if)# switchport mode access
SW-Core(config-if)# switchport access vlan 10
SW-Core(config-if)# no shutdown
VLAN配置模式(VLAN Configuration Mode)
VLAN配置模式在全局配置模式下通过vlan命令进入,提示符为SW-Core(config-vlan)#,用于创建、删除VLAN及设置VLAN名称。
常用命令:

vlan 20:创建VLAN 20(若存在则进入其配置模式)。name Marketing:设置VLAN名称。no vlan 20:删除VLAN 20(需先从接口中移除)。
示例:
SW-Core(config)# vlan 20
SW-Core(config-vlan)# name Marketing
操作模式与命令总结表
| 操作模式 | 提示符 | 进入方式 | 常用命令示例 |
|---|---|---|---|
| 用户模式 | Switch> |
登录交换机后默认进入 | show version, |
| 特权模式 | Switch# |
用户模式下输入enable |
show interfaces, write, reload |
| 全局配置模式 | Switch(config)# |
特权模式下输入conf t |
hostname SW-Core, enable secret |
| 接口配置模式 | Switch(config-if)# |
全局配置模式下输入interface x/x/x |
switchport mode access, no shutdown |
| VLAN配置模式 | Switch(config-vlan)# |
全局配置模式下输入vlan x |
name Sales, no vlan x |
配置示例:划分VLAN并接入接口
假设需将交换机的Fa0/1~Fa0/5接口划入VLAN 10(Sales),Fa0/6~Fa0/10划入VLAN 20(Marketing),步骤如下:
- 进入特权模式并保存配置:
Switch> enable Switch# configure terminal Switch(config)# hostname SW-Sales SW-Sales(config)# vlan 10 SW-Sales(config-vlan)# name Sales SW-Sales(config-vlan)# exit SW-Sales(config)# vlan 20 SW-Sales(config-vlan)# name Marketing SW-Sales(config-vlan)# exit - 配置接口并加入VLAN:
SW-Sales(config)# interface range fastethernet 0/1-5 // 批量配置接口 SW-Sales(config-if-range)# switchport mode access SW-Sales(config-if-range)# switchport access vlan 10 SW-Sales(config-if-range)# no shutdown SW-Sales(config-if-range)# exit SW-Sales(config)# interface range fastethernet 0/6-10 SW-Sales(config-if-range)# switchport mode access SW-Sales(config-if-range)# switchport access vlan 20 SW-Sales(config-if-range)# no shutdown - 保存配置并验证:
SW-Sales(config)# end SW-Sales# write Building configuration... [OK] SW-Sales# show vlan brief VLAN Name Status Ports ---- -------------------------------- --------- ------------------------------- 1 default active Fa0/11, Fa0/12, Fa0/13, Fa0/14, Fa0/15 10 Sales active Fa0/1, Fa0/2, Fa0/3, Fa0/4, Fa0/5 20 Marketing active Fa0/6, Fa0/7, Fa0/8, Fa0/9, Fa0/10 ...
相关问答FAQs
Q1:交换机命令输入错误时如何修改?
A:若命令输入错误未按回车,可通过Ctrl+C取消当前命令,重新输入;若已按回车但提示符未改变(如仍在全局配置模式),可通过Ctrl+C中断当前操作,或使用退格键(Backspace)删除错误字符后重新输入。
SW-Sales(config)# hostnmae SW-Core // 输入错误(应为hostname)
^
% Invalid command at '^' marker.
SW-Sales(config)# hostname SW-Core // 修正后重新输入
Q2:如何查看交换机已配置的VLAN信息?
A:在特权模式下输入show vlan brief命令,可查看所有VLAN的ID、名称、状态及包含的接口,若需查看VLAN的详细配置(如VLAN描述),可使用show vlan id [VLAN_ID],例如查看VLAN 10的详情:
SW-Sales# show vlan id 10
VLAN 10 Sales
Status: active
Ports:
Fa0/1, Fa0/2, Fa0/3, Fa0/4, Fa0/5
原创文章,发布者:酷番叔,转转请注明出处:https://cloud.kd.cn/ask/21674.html