设备配置必备 Cisco 命令速查表
简介
Cisco IOS(Internetwork Operating System)为管理员提供管理和配置 Cisco 设备的手段。尽管 Cisco 的命令结构是专有的,但它很好地体现了网络设备管理中可适用于各类厂商的基础概念。在 Aruba、Juniper 和 Broadcom 等其他主要网络公司的系统中,也能找到类似的命令行界面(CLI)和配置原则,使网络专业人员在进行一定的适配后,能够将技能应用到不同平台之上。
本文提供了一份速查表,可作为管理和配置 Cisco 网络设备的快速命令指南。文中附有截图,用于逐步展示部分命令的实际执行过程;截图是在通过控制台连接到路由器或交换机的情况下获取的。
快速参考:必备 Cisco 命令
基础模式和导航
Command | Purpose |
|---|---|
|
enable |
Enter privileged EXEC mode |
|
configure terminal |
Enter global configuration mode |
|
show running-config |
View the device's active configuration |
接口与 VLAN 配置
Command | Purpose |
|---|---|
|
interface gigabitethernet 0/1 |
Select an interface to configure |
|
vlan 10 |
Create or select VLAN 10 |
|
switchport mode access |
Set a port to access mode |
路由基础
Command | Purpose |
|---|---|
|
ip route 0.0.0.0 0.0.0.0 <next-hop> |
Add a default static route |
|
router ospf 1 |
Start an OSPF routing process |
|
show ip route |
View the routing table |
故障排除
Command | Purpose |
|---|---|
|
show ip interface brief |
Check interface status and IP addresses at a glance |
|
ping <address> |
Test connectivity to a device |
|
show version |
Check IOS version and uptime |
使用这些命令进行配置更改?Netwrix Auditor for Network Devices 会添加 网络配置更改跟踪,因此您始终知道是谁在何时更改了什么。
命令行界面(CLI)结构概览
命令模式
Cisco IOS 命令行界面(CLI)采用分层结构,并包含以下命令模式:
- User EXEC 模式 — 这是登录到 Cisco 设备时的默认模式。它提供有限的访问权限,只允许执行基本监控命令,并限制进行配置更改的能力。该模式可通过以 > 结尾的提示符来识别。
- Privileged EXEC 模式 — 该提升权限的模式可访问所有 show 命令以及系统级操作。它提供对系统的完整可视性,但不提供配置访问权限。它还使用户能够执行设备诊断和文件管理任务。该模式由以 # 结尾的提示符表示。
- 全局配置模式(Global Configuration mode) — 这种高度特权模式允许你进行全系统范围的配置更改,并可访问用于特定配置的各种子模式,例如接口设置。在该模式下,提示符包含 (config)#。
在命令模式之间切换
当你首次登录到 Cisco 路由器或交换机时,你将处于 User EXEC 模式。使用以下命令切换到更高权限的模式:
- enable — 用于访问 Privileged Exec 模式
- config terminal(或简写 config t)— 用于访问全局配置模式(Global Configuration mode)
以下命令在下面的截图中显示:
全局配置(Global Configuration) 模式还可以进一步细分为若干子模式,以便您配置不同的组件。以下是这些子模式以及用于访问它们的命令:
|
Interface Configuration mode |
interface [interface-type] [interface-number] |
|
Subinterface Configuration mode |
interface [interface-type] [interface-number].[subinterface-number] |
|
Router Configuration mode |
router [routing-protocol] |
|
Line Configuration mode |
line [line-type] [line-number] |
既然您已经知道如何进入更高特权模式的命令,下面是逐步返回退出这些模式的命令。
- exit — 用于在配置模式的层级结构中返回上一层。下面的截图显示管理员返回到 Privileged Exec 模式:
- end — 用于从任意配置模式中立即返回到 Privileged EXEC 模式。当您需要快速退出嵌套的配置级别时,这非常有用。
显示系统配置
登录到 Cisco 设备后,你可能想查看其基本系统设置。这可以通过输入 show running-config 来轻松完成,或者只需 show run 即可。该命令会显示设备内存中存储的活动配置。
请注意下面的截图:我们首先在 User Exec 模式下尝试了该命令,但由于权限不足而失败。截图只展示了 running config 的一部分。
与配置相关的其他命令包括以下内容:
|
show startup-config |
Displays the saved configuration stored in the device's NVRAM, which will be loaded when the device starts up |
|
copy running-config startup-config |
Replaces the startup config with the active config when the Cisco network device initializes |
|
copy startup-config running-config |
Merges the startup config with the currently active config in RAM |
|
write erase erase startup-config |
Deletes the startup config |
快捷方式和命令历史记录
下面是一些用于快速导航的其他命令:
- Ctrl+Z — 随时使用此键组合,可立即退出任何配置模式并返回到 Privileged EXEC mode
- Tab 键 — 使用此键可自动补全部分命令,或显示可能的补全选项。
- 上/下箭头键 — 使用这些键可在先前的命令之间循环浏览。
- show history — 使用此命令显示命令历史缓冲区。
设备设置与基础配置
重命名设备
先前截图中显示的运行配置(running config)将 router 作为主机名(hostname)。这正如 switch 是 Cisco 交换机的默认名称一样,均为 Cisco 设备的默认命名。设备名称会显示在命令提示符中。
使用 hostname 命令来更改设备名称。为每台设备选择唯一的名称,以便在网络中轻松识别。下面的示例中,我们将路由器的名称更改为 NorthOfficeRouter 。请注意,新名称会显示在最终的提示符中。
分配 IP 地址
还需要为设备分配一个 IP 地址,以便远程管理它。选择要分配 IP 地址的接口,然后完成以下步骤:
- 进入接口配置模式:
interface <interface-name>
- 分配 IP 地址和子网掩码:
ip address <IP-address>
- 启用接口:
no shutdown
下面是在 Cisco IOS 中执行该过程的效果示例。请注意 config-if 表示您处于接口配置的子模式。
配置密码
由于我们登录的是一台新设备,因此无需输入密码。以下是用于启用密码以增强安全性的命令:
|
enable password <pass-value> |
Defines the password required when using the enable command1 |
|
enable secret <pass-value> |
Sets the password required for any user to enter enable mode and encrypts it |
|
service password-encryption |
Directs the Cisco IOS software to encrypt the passwords, CHAP secrets and similar data saved in its configuration file |
配置横幅
您还可以配置多种横幅:
- 每日消息(motd)横幅:
横幅 motd #Unauthorized access prohibited#
- 登录横幅:
横幅登录 #请输入您的凭据#
- Exec 横幅:
横幅 Exec #欢迎来到网络#
交换机配置
交换机的用途与路由器不同,因此使用的命令也不同。以下是需要了解的一些关键概念:
- 端口(Switchport)模式 — 端口(Switchport)模式决定端口如何处理 VLAN 流量。三个主要模式是 access mode、trunk mode 和 dynamic mode。
- 双工速度 — 双工设置决定端口是否可以同时发送和接收数据。你可以指定 half、full 或 auto。Auto 允许交换机与已连接的设备协商双工设置。
- 速率 — 此设置用于确定端口的数据传输速率。根据交换机的能力,速率可设置为 10 Mbps、100 Mbps、1000 Mbps(1 Gbps)或 auto。
VLAN 配置
VLAN 配置是网络分段与管理的重要组成部分。在 Global Configuration 模式下,你可以使用以下命令:
|
vlan <vlan-id> |
Creates a VLAN |
|
Vlan <vlan-name> |
Assigns a name to the VLAN |
|
vlan switchport access vlan <vlan-id> |
Sets the VLAN that the interface belongs to |
|
no vlan <vlan-id> |
Deletes a VLAN |
|
show vlan |
Display the VLAN configuration |
下面的截图展示了基本端口配置所需的命令:
生成树协议(Spanning Tree Protocol,STP)命令
生成树协议(Spanning Tree Protocol,STP)是思科交换机的一项功能,有助于防止网络环路。STP 在思科交换机上对所有 VLAN 默认启用。您可以针对整台交换机全局修改 STP 设置,也可以为单独的接口或 VLAN 应用特定配置,以便对网络不同部分的 STP 行为进行更精细的控制。命令如下:
spanning-tree mode rapid-pvst
配置 Trunk 端口
需要承载多 VLAN 流量的端口必须配置为 Trunk。首先要把端口设置为 Trunk,然后再分配 VLAN。以下是命令:
switchport trunk native vlan <vlan-id>
在 Cisco 交换机上配置中继端口(trunk ports)需要使用以下命令:switchport trunk 和 switchport trunk allowed vlan 。下面是这些命令的更多详细信息:
|
switchport trunk native vlan <vlan-id> |
Enables trunk mode for the port and sets the native VLAN for untagged traffic on the trunk |
|
switchport trunk allowed vlan <vlan-id> |
Adds the specified VLANs to the current list |
|
switchport trunk allowed vlan remove <vlan-id> |
Removes the specified VLANs from the allowed list |
下面的截图展示了这些 VLAN 命令的运行效果:
网络基础
IP 地址编制与子网划分
以下命令用于 IP 地址编制和子网划分:
|
ip address <ip-value> <bnet-value> |
Assigns an IP and subnet mask |
|
show ip interface <interface-number> |
Displays the status of a network interface as well as a detailed listing of its IP configurations and related characteristics |
|
show ip interface brief |
Provides a concise summary of the IP interface status and configuration |
|
ip address <ip-value> <subnet-value> secondary |
Assigns a secondary IP address |
|
no ip address |
Removes an IP address |
下面的截图显示了以下命令的输出:show ip interface brief 命令:
配置路由协议
您可以使用以下命令来配置路由协议:
|
ip route <network-number> <network-mask> {<ip-address> | <exit-interface>} |
Sets a static route in the IP routing table |
|
ip route 0.0.0.0 0.0.0.0 {next-hop-ip | exit-interface} |
Configures a default route |
|
no ip route {network} {mask} {next-hop-ip | exit-interface} |
Removes a route |
|
router rip |
Enables a Routing Information Protocol (RIP) routing process, which places you in Router Configuration mode |
|
no auto-summary |
Disables automatic summarization |
|
version 2 |
Configures the software to receive and send only RIP version 2 packets |
|
network ip-address |
Associates a network with a RIP routing process |
|
passive-interface interface |
Sets the specified interface to passive RIP mode, which means RIP routing updates are accepted by, but not sent out of, the interface |
|
show ip rip database |
Displays the contents of the RIP routing database |
|
default-information originate |
Generates a default route into RIP |
下面的截图展示了一个静态路由的配置,以及针对所有其他网络的默认路由配置:
配置网络地址转换 (NAT)
使用以下命令配置 NAT。NAT 允许将本地网络中的专用 IP 地址在通过互联网发送之前转换为公网 IP 地址:
|
ip nat [inside | outside] |
Specifies whether the NAT operation is applied to traffic entering or leaving the router’s network |
|
ip nat inside source {list{access-list-number | access-list-name}} interface type number[overload] |
Establishes dynamic source translation. Use the list keyword to specify an ACL to identify the traffic that will be subject to NAT. The overload option enables the router to use one global address for many local addresses |
|
ip nat inside source static local-ip global-ip |
Establishes a static translation between an inside local address and an inside global address |
故障排查与诊断
以下命令可帮助您进行故障排查并执行基础诊断:
|
ping {hostname | system-address} [source source-address] |
Reveals basic network connectivity |
|
traceroute {hostname | system-address} [source source-address] |
Traces the route that packets take to reach a destination |
|
show interfaces |
Displays detailed information about interface status, settings and counters |
|
show ip route |
Shows the routing table of the device |
|
show interface status |
Displays the interface line status |
|
show interfaces trunk |
Lists information about the currently operational trunks and the VLANs supported by those trunks |
|
show version |
Displays information about the IOS version, uptime and hardware configuration |
|
show running-config |
Displays the current active configuration on the device |
|
show tech-support |
Generates a comprehensive report of the device's configuration and status (useful for advanced troubleshooting) |
|
show cdp |
Shows whether CDP is enabled globally |
|
show cdp neighbors [detail] |
Lists summary (or detailed) information about each neighbor connected to the device |
|
cdp run |
Enables or disables Cisco Discovery Protocol (CDP) for the device |
|
show mac address-table |
Displays the MAC address table |
|
show vtp status |
Lists the current VLAN Trunk Protocol (VTP) status, including the current mode |
安全配置
您可以配置访问控制列表,以限制并允许与您的 Cisco 设备之间的流量。使用以下命令:
|
password <pass-value> |
Lists the password that is required if the login command (with no other parameters) is configured |
|
username name password <pass-value> |
Defines one of possibly multiple user names and associated passwords used for user authentication. It is used when the login local line configuration command has been used. |
|
enable password <pass-value> |
Defines the password required when using the enable command |
|
enable secret <pass-value> |
Sets the password required for any user to enter enable mode |
|
service password-encryption |
Directs the Cisco IOS software to encrypt the passwords, CHAP secrets and similar data saved in its configuration file |
|
ip access-list {standard | extended} {acl-name | acl-number |
Creates a standard or extended ACL |
|
permit source <source-wildcar> |
Adds permit rules for a Standard ACL |
|
deny source <source-wildcard> |
Adds deny rules for an Extended ACL |
|
ip access-group {acl-name | acl-number} {in | out} |
Applies an ACL to an interface |
|
show access-lists [acl-name | acl-number] |
Displays ACL configuration |
|
no ip access-list {standard | extended} {acl-name | acl-number} |
Removes an ACL |
|
ip domain-name name |
Configures a DNS domain name |
|
crypto key generate rsa |
Creates and stores (in a hidden location in flash memory) the keys that are required by SSH |
|
transport input {telnet | ssh} |
Defines whether Telnet or SSH access is allowed into this switch. Both values can be specified in a single command to allow both Telnet and SSH access, which are the default settings |
|
ntp peer <ip-address> |
Configures the software clock to synchronize a peer or to be synchronized by a peer |
下方截图显示了用于扩展 ACL(访问控制列表)的命令:
配置 SSH 和远程访问
使用以下命令来配置 SSH 和远程访问:
|
hostname <name> |
Sets a hostname (if not already configured) |
|
ip domain-name [domain-name] |
Configures an IP domain name |
|
crypto key generate rsa |
Generates an RSA key pair for SSH |
|
ip ssh version 2 |
Configures SSH version 2 |
|
username [username] privilege [level] secret [password] |
Creates a local user account |
|
Router(config)# line vty [line-range] Router(config-line)# transport input ssh Router(config-line)# login local |
Configures VTY lines for SSH access |
下方截图显示了 RSA 密钥的生成过程:
实现端口安全
使用以下命令来实现端口安全:
|
switchport port-security |
Enables port security on the interface |
|
switchport port-security maximum <number> |
Sets the maximum number of secure MAC addresses on the port |
|
switchport port-security mac-address {mac-addr | {sticky [mac-addr]}} |
Adds a MAC address to the list of secure MAC addresses and optionally configures them as sticky on the interface |
|
switchport port-security violation {shutdown | restrict | protect} |
Sets the action to be taken when a security violation is detected |
|
show port security [interface interface-id] |
Displays information about security options configured on the interface |
下面的截图展示了在交换机端口上配置端口安全的过程。
管理用户账户
你可以使用以下命令来管理用户账户:
|
username <username> privilege <level> secret <password> |
Creates a local user account |
|
show users |
Displays current user sessions |
|
no username <username> |
Removes a user account |
|
security passwords min-length <length> |
Sets password complexity requirements |
DHCP 配置
使用以下命令来配置 DHCP:
|
ip address dhcp |
Acquires an IP address on an interface via DHCP |
|
ip dhcp pool <pool-name> |
Configures a DHCP address pool on a DHCP server and enters DHCP Pool Configuration mode |
|
domain-name <domain> |
Specifies the domain name for a DHCP client |
|
network network-number [mask] |
Configures the network number and mask for a DHCP address pool primary or secondary subnet on a Cisco IOS DHCP server |
|
ip dhcp excluded-address ip-address [last-ip-address] |
Specifies IP addresses that a DHCP server should not assign to DHCP clients |
|
ip helper-address address |
Enables forwarding of UDP broadcasts, including BOOTP, received on an interface |
|
default-router address [address2 ... address8] |
Specifies the default gateway for a DHCP client |
下面的截图展示了在 Cisco 路由器上进行的基本 DHCP 配置:
监控与日志
以下命令对于监控和日志记录很有用:
|
logging on |
Enables logging globally |
|
logging host {ip-address | hostname} |
Configures logging to a syslog server |
|
logging trap level |
Sets the logging severity level |
|
terminal monitor |
Sends a copy of all syslog messages, including debug messages, to the Telnet or SSH user who issues this command |
|
snmp-server community <community-string> [RO|RW] |
Enables SNMP |
|
snmp-server location <location-string> |
Configures the SNMP server location |
|
snmp-server enable traps |
Enables SNMP traps |
备份、还原与升级
以下命令用于备份、还原和升级:
|
copy running-config startup-config |
Saves the running configuration to startup configurationEnables logging globally |
|
copy running-config tftp |
Copies the running configuration to a TFTP server |
|
copy startup-config tftp |
Copies the startup configuration to a TFTP server |
|
copy tftp: running-config |
Copies the configuration from a TFTP server to the device |
|
copy running-config flash:<file name> |
Copies the configuration to flash |
|
copy {ftp: flash:} |
Copies a new IOS image to the device using TFTP or FTP |
下面的截图显示:正在运行的配置(running config)已备份到启动配置(startup config)。
配置身份验证、授权和计费(AAA)
以下命令用于配置身份验证、授权和计费(AAA):
|
aaa new-model |
Enables AAA |
|
radius-server host {ip-address | hostname} [auth-port port-number] [acct-port port-number] [timeout seconds] [retransmit retries] [key string] |
Configures the RADIUS server |
|
radius-server key {0 string | 7 string | string} |
Sets the RADIUS key |
|
aaa authentication login {default | list-name} method1 |
Configures AAA authentication |
|
aaa authorization {network | exec | commands level | reverse-access | configuration} {default | list-name} method1 |
Configures AAA authorization |
|
aaa accounting {system | network | exec | connection | commands level} {default | list-name} {start-stop | stop-only | none} [method1 |
Configures AAA accounting |
常见用例
Cisco 交换机的常见用例之一是通过 VLAN 进行网络分段以及质量控制。通过为无线设备、电话、摄像头和打印机分别创建独立的 VLAN,你可以实现网络分段,并对不同类型的流量进行优先级管理(例如,将语音流量的优先级高于打印流量)。
Cisco 路由器同样可以使用标准或扩展访问控制列表来实现网络分段。它们可以限制来自特定来源的流量,或限制某些流量类型进入或离开路由器的各个分段。
以下是为网络分段配置 Cisco 路由器的分步指南:
- 配置路由器主机名,并启用一个“secret”密码。
- 为路由器的接口分配 IP 地址。
- 要为所连接的网络段路由流量,可配置静态路由信息,或配置路由协议。
- 配置 DHCP,将 IP 地址以及其他 DHCP 选项分发给连接到网络的客户端。
- 配置 ACL,以加强安全性并限制入站和出站流量。
- 配置 NAT 以实现 Internet 访问。
- 启用日志记录。
- 保存配置。
常见问题场景与故障排除提示
以下是 Cisco 路由器和交换机的常见问题以及解决建议:
场景:设备无法在 VLAN 或子网之间通信
- 检查交换机上的 VLAN 配置和中继(trunking)。
- 验证 IP 地址和子网掩码。
- 检查路由表,并确保存在对应路由。
- 使用 ping 和 traceroute 测试连通性。
- 确认 ACL 没有阻止网络流量。
情景:接口已断开或处于抖动状态
- 检查物理连接和电缆是否完好。
- 使用以下命令验证接口配置:show interface。
- 关闭并重新启用该接口。
- 测试不同的速率和双工设置。
场景:未授权访问尝试或可疑流量
- 查看日志并使用 show logging。
- 检查 ACL 配置和命中次数。
- 验证 AAA 和 TACACS+ 设置。
- 在交换机上启用端口安全。
额外提示
以下提示可帮助您更高效地管理 Cisco 设备:
- 如果您不熟悉 Cisco IOS,请务必利用上下文相关的帮助——只需在命令中的任意位置输入 ? ,即可获得建议以及可用的
- 使用命令快捷方式和制表符补全可以节省时间。例如输入 sh run ,而不是 show running-config。
- 使用上、下箭头(或 Ctrl+P 和 Ctrl+N)可快速访问最近的命令。
- 如果您无法访问 Cisco 路由器,可以下载多种 Cisco 设备模拟器来进行练习,并熟悉这些命令。
始终将安全性置于最高优先级:
- 限制谁可以访问您的 Cisco 设备。
- 确保所有帐户都使用加密的长密码。
- 在分配角色时遵循最小特权原则。
- 创建访问控制列表,以限制不同的流量来源和类型。
- 启用日志记录,并使用第三方监控解决方案;一旦你的配置被意外或恶意篡改,就会及时收到告警。
分享到