基础操作 | |
switch>enable | 进入特权模式 |
switch#config terminal | 进入配置模式 |
switch(config)#hostname sw1 | 设置主机名为sw1 |
switch(config)#end | 退回到特权模式 |
switch#exit | 返回上级视图 |
switch>show clock | 查看时间 |
switch>clock datetime | 设置时间 |
switch>show user | 查看在线用户(简略信息) |
switch# clear counters GigabitEthernet0/1 | 清除接口计数器 |
switch# clear mac address-table | 清除 MAC 地址表 |
设备配置文件相关,核心概念 | |
running-config: | 设备当前运行的配置(存储在 RAM 中),重启后丢失。 |
startup-config: | 设备保存的配置(存储在 NVRAM 中),重启后自动加载。 |
switch# show startup-config | 看 NVRAM 中保存的配置(即设备下次启动时会加载的配置) |
switch# copy running-config startup-config | 将当前运行的配置(running-config)保存到 NVRAM(比如配置文件修改错误想撤销) |
switch# configure memory | 将NVRAM中保存的启动配置(startup-config)合并到当前运行配置(running-config)中 |
switch# erase startup-config | 清空 NVRAM 中的配置(删除 startup-config 文件),设备重启后将恢复出厂默认状态。 |
switch# reload | 重启设备 |
switch>write | 保存信息到NVRAM |
--------------------------------------------------------------------------------------------------------
常用show命令
--------------------------------------------------------------------------------------------------------
show version # 查看设备型号、IOS 版本、运行时间、内存及序列号。
show running-config # 显示当前运行配置(RAM 中)。
show startup-config # 显示保存的启动配置(NVRAM 中)。
show clock # 查看设备时间及时区。
show ip route # 查看路由表(所有协议学习到的路由)。
show ip interface brief # 查看所有接口的 IP 地址及状态(快速检查 IP 配置)。
show interfaces # 查看所有接口的详细状态(带宽、双工、错误计数等)。
show interface GigabitEthernet0/1 # 查看指定接口的详细统计信息
show interface GigabitEthernet0/1 counters # 查看接口流量异常
show controllers GigabitEthernet0/1 # 查看接口物理层信息(如电缆类型 DCE/DTE)。
show interfaces status # 查看接口的简要状态(是否启用、VLAN、速率)。
show interfaces usage # 查看接口的带宽状态
show interface trunk # 查看所有 Trunk 端口
show arp # 查看 ARP 表(IP 与 MAC 地址映射)。
show vlan brief # 查看所有 VLAN 及分配的端口。
show mac address-table # 查看 MAC 地址表(动态/静态 MAC 与接口映射)。
show port-security # 查看端口安全配置及违规记录。
show etherchannel summary # 查看端口聚合(EtherChannel)状态
show etherchannel port-channel # 查看端口聚合(EtherChannel)状态
show cdp neighbors # 查看直连的 CDP 邻居设备及接口(需启用 CDP)。
show ip protocols # 显示所有运行的路由协议(RIP、OSPF、EIGRP 等)及参数。
show spanning-tree # 查看生成树协议(STP)全局状态。
show spanning-tree vlan [ID] # 查看指定 VLAN 的 STP 详细信息。
安全与 ACL
show access-lists # 查看所有 ACL 规则及匹配计数。
show ip access-lists # 查看 IPv4 ACL 规则。
show logging # 查看系统日志(包括安全事件、接口状态变化)。
show users # 查看当前登录用户(控制台、SSH 会话)。
系统与日志
show processes cpu # 查看 CPU 利用率及进程占用情况。
show memory # 查看内存使用情况。
show logging # 查看历史系统日志(调试信息、错误消息)。
show monitor # 查看实时系统日志(调试信息、错误消息)。
show history # 查看当前会话输入过的命令历史。
其他实用命令
show ip dhcp binding # 查看 DHCP 分配的 IP 地址与 MAC 映射(需启用 DHCP 服务)。
show ip nat translations # 查看 NAT 转换表(动态/静态映射)。
show ip http server # 查看 HTTP/HTTPS 服务状态。
show ip sockets # 查看设备当前的网络连接(TCP/UDP 端口监听状态)。
--------------------------------------------------------------------------------------------------------
端口安全(限制 MAC 数量)
switch(config-if)# switchport port-security # 启用端口安全
switch(config-if)# switchport port-security maximum 2 # 允许最多 2 个 MAC
switch(config-if)# switchport port-security violation shutdown # 违规关闭端口
switchport port-security mac-address [MAC地址] # 绑定静态MAC地址
--------------------------------------------------------------------------------------------------------
静态聚合(LACP/PAGP)
interface Port-channel 1 # 创建 Port-channel 接口,例如 interface Port-channel 1
switchport mode trunk # 设置端口模式(access/trunk)
switchport trunk allowed vlan 10,20 # 如果是trunk模式
switch(config)# interface range GigabitEthernet0/1-2 # 批量配置端口
switch(config-if-range)# channel-group 1 mode active # 启用 LACP(active 模式)
Switch(config-if-range)# switchport mode trunk
show etherchannel summary # 查看 EtherChannel 状态
show etherchannel port-channel
--------------------------------------------------------------------------------------------------------
#RIP#
show ip rip database # 查看 RIP 路由数据库。
#OSPF#
show ip ospf neighbor # 查看 OSPF 邻居表(状态、Router ID、接口)。
show ip ospf database # 查看 OSPF 链路状态数据库(LSDB)。
#EIGRP#
show ip eigrp neighbors # 查看 EIGRP 邻居表。
show ip eigrp topology # 查看 EIGRP 拓扑表。
#BGP#
show ip bgp summary # 查看 BGP 邻居状态摘要(AS 号、路由数)。
show ip bgp # 查看 BGP 路由表(路径属性、最佳路径标记)。
########################################################################################################
switch(config)# interface loopback 0 # 创建环回接口(注意拼写) 激活逻辑接口
Switch(config)# interface GigabitEthernet0/1 # 进入接口配置视图
Switch(config)# interface range GigabitEthernet0/1-24 # 批量端口组
Switch(config-if)# no switchport # 把物理端口变成三层口
Switch(config-if)# shutdown # 关闭接口
Switch(config-if)# no shutdown # 启用接口
Switch(config-if)# ip address 192.168.1.254 255.255.255.0 # 配置主 IP 地址
Switch(config-if)# ip address 192.168.1.1 255.255.255.0 secondary # 配置 Secondary IP
switch(config-if)# speed 100 # 设置速率为 100 Mbps
switch(config-if)# duplex full # 设为全双工模式
switch(config)# interface vlan 1 # 进入管理 VLAN 接口
switch(config-if)# ip address 192.168.1.10 255.255.255.0 # 配置管理 IP
switch(config-if)# no shutdown # 启用接口
switch(config)# ip default-gateway 192.168.1.1 # 设置默认网关
--------------------------------------------------------------------------------------------------------
switch(config)# username admin secret [密码] # 创建本地账号
switch(config)# service password-encryption # 手工加密配置文件中的所有明文密码
switch(config)# enable password 123456 # 配置特权模式(enable)的明文或弱加密密码
switch(config)# enable secret 123456 # 配置特权模式(enable)的强加密密码(使用不可逆的 Type 5 MD5 哈希)
--------------------------------------------------------------------------------------------------------
switch(config)# line vty 0 4 # 同时配置 0-4 号 VTY 线路
switch(config)# exec-timeout 0 0 # 关闭超时断开
switch(config-line)# password Cisco123 # 设置明文密码(需配合 login 生效)
switch(config-line)# login # 强制密码验证
switch(config-line)# transport input ssh # 仅允许 SSH(安全建议)
--------------------------------------------------------------------------------------------------------
switch(config)# line console 0 # 进入CON接口
switch(config-line)# exec-timeout 0 0 # 关闭超时断开
switch(config-line)# logging synchronous # 可选:避免日志打断输入
switch(config-line)# password Aa123456 # 密码配置密码
--------------------------------------------------------------------------------------------------------
接口VLAN相关配置
--------------------------------------------------------------------------------------------------------
configure terminal 进入配置状态
nterface range {port-range} 进入组配置状态
--------------------------------------------------------------------------------------------------------
配置VLAN
Switch(config)# vlan 20 # 创建VLAN
Switch(config-vlan)# name test20 # 命名VLAN
Switch(config-vlan)# mtu mtu-size # 改变MTU大小
--------------------------------------------------------------------------------------------------------
将端口分配给一个VLAN
Switch(config)# interface fastethernet0/1 # 进入要分配的端口
Switch(config-if)# switchport mode access # 定义二层口
Switch(config-if)# switchport access vlan 2 # 把端口分配给某一VLAN
--------------------------------------------------------------------------------------------------------
配置VLAN trunk
例:
Switch(config)# interface fastethernet0/4 # 进入端口配置状态
Switch(config-if)# switchport trunk encapsulation dot1q # 配置trunk封装802.1Q
Switch(config-if)# switchport mode trunk # 配置二层trunk模式
Switch(config-if)# switchport trunk allowed vlan 10,20 # 定义TRUNK允许的VLAN
Switch(config-if)# switchport trunk allowed remove vlan 10 # 禁止trunk中某个VLAN的传输
Switch(config-if)# switchport mode dynamic auto # 自动协商是否成为Trunk或desirable模式
Switch(config-if)# switchport mode dynamic desirable # 如果对端是dynamic auto、dynamic desirable或trunk:会协商成Trunk,如果对端是access模式:端口保持Access模式。
Switch(config-if)# switchport trunk native vlan vlan-id # 指定802.1Q native VLAN(默认VLAN)
--------------------------------------------------------------------------------------------------------
路由器配置命令
--------------------------------------------------------------------------------------------------------
switch# show ip route # 查看路由表
switch# show protocols # 显示接口协议状态
switch# show ip protocols # 显示已配置的路由协议
switch(config)# ip route 192.168.2.0 255.255.255.0 10.0.0.1 # 静态路由
--------------------------------------------------------------------------------------------------------
RIP(Routing Information Protocol)
1. 基础配置
Switch(config)# Switch rip # 启用 RIP 协议
Switch(config-Switch)# version 2 # 指定版本 2(支持 CIDR)
Switch(config-Switch)# no auto-summary # 关闭自动汇总(必须配置!)
Switch(config-Switch)# network 192.168.1.0 # 宣告直连网段
Switch(config-Switch)# network 10.0.0.0
Switch(config-Switch)# passive-interface GigabitEthernet0/0 # 禁止接口发送 RIP 更新
2. 查看与验证
Switch# show ip route rip # 查看 RIP 学习到的路由
Switch# show ip protocols # 查看 RIP 配置摘要
Switch# show ip rip database # 查看 RIP 路由数据库
Switch# debug ip rip # 实时查看 RIP 更新报文
--------------------------------------------------------------------------------------------------------
OSPF(Open Shortest Path First)
1. 基础配置
Switch(config)# Switch ospf 100 # 启用 OSPF,进程 ID 为 100(本地有效)
Switch(config-Switch)# Switch-id 1.1.1.1 # 手动设置 Switch ID(可选)
Switch(config-Switch)# network 192.168.1.0 0.0.0.255 area 0 # 宣告网段并指定区域
Switch(config-Switch)# network 10.0.0.0 0.255.255.255 area 1
Switch(config-Switch)# passive-interface GigabitEthernet0/0 # 禁止接口发送 OSPF Hello 包
2. 查看与验证
Switch# show ip route ospf # 查看 OSPF 路由表
Switch# show ip ospf neighbor # 查看 OSPF 邻居表
Switch# show ip ospf database # 查看 OSPF 链路状态数据库(LSDB)
Switch# show ip ospf interface # 查看接口的 OSPF 参数
Switch# debug ip ospf events # 调试 OSPF 事件(邻居建立、LSA 更新)
--------------------------------------------------------------------------------------------------------
BGP(Border Gateway Protocol)
1. 基础配置
Switch(config)# Switch bgp 65001 # 启用 BGP,本地 AS 号为 65001
Switch(config-Switch)# neighbor 2.2.2.2 remote-as 65002 # 指定 BGP 邻居及对端 AS 号
Switch(config-Switch)# network 192.168.1.0 mask 255.255.255.0 # 宣告本地网络
Switch(config-Switch)# neighbor 2.2.2.2 update-source Loopback0 # 指定更新源接口(可选)
Switch(config-Switch)# neighbor 2.2.2.2 ebgp-multihop 2 # 允许非直连 EBGP(TTL>1)
2. 查看与验证
Switch# show ip bgp summary # 查看 BGP 邻居状态摘要
Switch# show ip bgp neighbors # 查看详细的 BGP 邻居信息
Switch# show ip bgp # 查看 BGP 路由表
Switch# show ip route bgp # 查看 BGP 学习到的路由
Switch# debug ip bgp updates # 实时查看 BGP 更新报文
########################################################################################################
跟踪路由(Tracing the Route)
SUN-A> traceroute <有故障的主机 | 有故障的IP地址> # Linux路由追踪
C:\windows\> tracert <有故障的主机 | 有故障的IP地址> # windows路由追踪
C:\windows\> ipconfig / all # 显示详细信息(包括DNS服务器、MAC地址、DHCP租期等)
C:\windows\> ipconfig /release # 释放当前IP地址(需管理员权限)
C:\windows\> ipconfig /renew # 重新获取IP地址(需管理员权限)
Switch# show arp # 交换机arp缓存
C:\windows\> arp -a # windows arp缓存
验证终端系统的路由表
C:\windows\> netstat –rn
C:\windows\> route –f add 0.0.0.0 mask 0.0.0.0<需要添加入的网关地址>
C:\windows\> route [–f ] [[print | add | delete | change] [destination][mask netmask] [gateway]]
C:\windows\> route add mask <网络掩码> <网关ip地址>
C:\windows\> route delete mask <网络掩码> <网关ip地址>
C:\windows\> nbtstat <相应的参数>
SUN-A> netstat -rn路由器