技术文档 活跃维护

cisco-cheatsheet

grplyler/cisco-cheatsheet

思科备忘单:用于引导、模板、配置以及加固思科路由器和交换机的命令和片段。

568
Stars 标星
131
Forks 分支
14
Watchers 关注
3
Open Issues
Python
主要语言
GPL-3.0
开源协议
90 KB
仓库大小
1 年前
最后推送
一键安装扩展 / 插件指令
dsh plugin --profile web add github:grplyler/cisco-cheatsheet
git clone https://github.com/grplyler/cisco-cheatsheet.git
git clone git@github.com:grplyler/cisco-cheatsheet.git
README.md master

机器翻译正文由机器翻译自项目原始文档(英文),排版经程序统一处理,可能存在偏差,请以原项目仓库为准。

思科备忘单

思科 CLI 参考、操作指南和工具

警告,请自行承担风险。我在学习 CCNA 时以教育为目的创建了这些脚本

快速导航

快速片段与脚本

常规部分

完整导航

基础网络

设置


初始化

这些命令会清除所有配置并重启设备

erase startup-config
delete vlan.dat
reload

注意: 记得在重载时选择“不”以保存运行配置。如果选择“是”,运行配置将被保存,并且在重载时你将无法使用新的配置。

基础交换机配置

configure terminal
no ip domain-lookup
hostname S1
line console 0
logging synchronous
exit
banner motd $ Authorized Access Only! And Godzilla will beat Kong any day $
exit
copy running-config startup-config

基本路由器配置

configure terminal
no ip domain-lookup
hostname R1
line console 0
logging synchronous
exit
banner motd $ Authorized Access Only! And Godzilla will beat Kong any day $
exit
copy running-config startup-config

带密码安全的基础配置

可粘贴

configure terminal
no ip domain-lookup
hostname R1
line console 0
logging synchronous
exit
banner motd $ Authorized Access Only! And Godzilla will beat Kong any day $
exit
copy running-config startup-config
conf t
enable secret class
line console 0
password cisco
login
exit
line vty 0 4
password cisco
login
exit
service password-encryption
end
copy running-config startup-config

基础安全

conf t
enable secret class
line console 0
password cisco
login
exit
line vty 0 4
password cisco
login
exit
service password-encryption
end

配置 SSH

show ip ssh
conf t
ip domain-name cisco.com
crypto key generate rsa

username admin secret ccna
line vty 0 15
transport input ssh
login local
exit
ip ssh version 2
exit

设置时钟

显示时钟

show clock

将时钟设置为美国东部时间

clock timezone EST -5

恢复默认时区

no clock timezone

基础加固(需要工作)

conf t
! Logout timer
!
line con 0
 exec-timeout 5
line vty 0 4
 exec-timeout 5

exit

ip ssh time-out 60
ip ssh authentication-retries 3
end

通过FTP备份配置

使用包含的 FTP 服务器

copy running-config startup-config
copy startup-config ftp://192.168.1.10/config.txt

通过控制台备份配置

即将推出

恢复配置

copy ftp://192.168.1.10/config.txt running-config

清除(ROMMON、密码恢复等)

执行启动中断以恢复丢失或未知的密码

警告:此操作将删除设备上的所有当前配置

  1. 确保控制台电缆已连接,波特率为9600
  2. 如果需要,请备份配置
  3. 断开电源
  4. 等待几秒钟
  5. 重新插入交换机电源线
  6. 在15秒内,按住Mode按钮,直到绿色闪光灯闪烁为琥珀色,然后返回绿色闪烁。松开Mode按钮。
  7. 应显示如下内容:

    initialize the flash file system, and finish loading the operating system software#
    
    flash_init
    load_helper
    boot
  8. 运行 flash_init
  9. 运行 copy flash:config.text flash:config.text.old
  10. 运行 boot

    该设备现在应该可以在没有配置的情况下启动,并允许您访问它。

接口


接口选择

为端口分配IP地址

conf t
int f0/1
ip addr 192.168.10.11 255.255.255.0
end

为接口分配静态 IP

conf t
int g0/0
ip addr 10.0.0.10 255.255.255.0

接口范围

为端口分配 IP 地址

conf t
int f0/1
ip addr 192.168.10.11 255.255.255.0
end

选择单个范围并分配到 VLAN

conf t
int range f0/1-12
switchport mode access
switch access vlan 10
end
conf t
int range f0/13-24
switchport mode access
switchport access vlan 20
end

选择多个接口范围并移动到 VLAN

conf t
int range f0/1-4,g0/1,f0/16-20
switchport mode access
switchport access vlan 10
end

接口验证

show ip interface brief

或

show ip int br

移除 IP 地址

conf t
int f0/1
no ip addr
end

控制台端口

更改控制台波特率

conf t
line con 0
speed 115200
end
conf t
line con 0
speed 9600
end

动态主机配置协议


代码片段:启用路由器 DHCP 服务器

此代码片段在 R1 上配置 DHCP 服务器,并将在 10.0.0.1/24 网络上分配 IP 地址。非常适合与 FTP 服务器 一起使用。

conf t
ip domain name cisco.com
ip dhcp excluded-address 10.0.0.1
ip dhcp pool test
network 10.0.0.0 255.255.255.0
default-router 10.0.0.1
end

片段:启用交换机 DHCP 服务器

ip dhcp pool test
network 10.0.0.0 255.255.255.0
domain-name cisco.com
default-router 10.0.0.1
dns-server 10.0.0.1
lease 4
ip dhcp snooping
ip dhcp-server 10.0.0.3
interface vlan 1
ip address 10.0.0.3

创建 DHCP 池

由于我们无法更改实验室的 IP 地址,这是 Liberty 大学 CCNA 实验的变通方法

conf t
ip domain name cisco.com
ip dhcp excluded-address 10.0.0.1
ip dhcp pool managementpool
network 10.0.0.1 255.255.255.0
default-router 10.0.0.1
end
conf t
ip dhcp excluded-address 192.168.10.1
ip dhcp excluded-address 192.168.10.254
ip dhcp pool office-pool-1
network 192.168.10.0 255.255.255.0
default-router 192.168.10.1
dns-server 192.168.5.5
domain-name linux.org
end

DHCP 验证

show running-config | section dhcp
show ip dhcp binding
show ip dhcp server statistics

Disable DHCP

conf t
no service dhcp
end

重新启用 DHCP

conf t
service dhcp
end

创建 VLAN DHCP

为每个 VLAN 创建一个独立的 DHCP 池

创建 VLAN

conf t
vlan 10
name Management
vlan 20
name Sales
vlan 30
name Operations
end

配置SVI和IP地址

VLAN IP 地址 网关
10 192.168.10.254 192.168.10.1
20 192.168.20.254 192.168.20.1
30 192.168.30.254 192.168.30.1
conf t
int vlan 10
ip address 192.168.10.254 255.255.255.0
ip default-gateway 192.168.10.1
no shut

int vlan 20
ip address 192.168.20.254 255.255.255.0
ip default-gateway 192.168.20.1
no shut

int vlan 30
ip address 192.168.30.254 255.255.255.0
ip default-gateway 192.168.30.1
no shut
end

向 VLAN 添加接口,每个 VLAN 8 个端口

conf t
int range f0/1-7
switchport mode access
switchport access vlan 10

int range f0/8-15
switchport mode access
switchport access vlan 20

int range f0/16-24
switchport mode access
switchport access vlan 30
end

为每个 VLAN 创建 DHCP 池

conf t
ip domain name cisco.com
ip dhcp excluded-address 192.168.10.1
ip dhcp pool vlan10pool
network 192.168.10.0 255.255.255.0
default-router 192.168.10.1
import all

ip dhcp excluded-address 192.168.20.1
ip dhcp pool vlan20pool
network 192.168.20.0 255.255.255.0
default-router 192.168.20.1
import all

ip dhcp excluded-address 192.168.30.1
ip dhcp pool vlan30pool
network 192.168.30.0 255.255.255.0
default-router 192.168.30.1
import all
end

现在当一个设备插入例如端口 f0/4 并发出 DHCP 请求时,它应该获得一个像 192.168.10.3 的 IP,因为它插入的是分配给 VLAN 10 的端口。

Verify DHCP pool

show ip dhcp pool

删除 DHCP 池

conf t
no ip dhcp pool managementpool
end

中级网络

虚拟局域网


VLAN 创建

conf t
vlan 10
name Faculty
exit
conf t
vlan 20
name Students
exit

端口分配

conf t
interface range Fa0/1-12
switchport mode access
switchport access vlan 10
end
conf t interface range Fa0/13-24
switchport mode access
switchport access vlan 20
end
conf t
interface Gi0/1
switchport mode access
switchport access vlan 99
end

IP分配

conf t
int vlan 99
ip address 10.0.0.1 255.255.255.0
end

验证

show vlan brief

语音和数据 VLAN

假设数据在 VLAN 10,语音在 VLAN 20

conf t
int Fa0/4
switchport mode access
switchport access vlan 10
switchport voice vlan 20
end

管理 VLAN

conf t
vlan 99
name Management
exit
interface Fa0/24
switchport mode access
switchport access vlan 99
exit
int vlan 99
ip addr 10.0.0.1 255.255.255.0
end

删除文件中的 VLAN

delete vlan.dat

删除内存中的 VLAN

警告:确保将端口移动到其他 VLAN,否则它们将无法使用

conf t
no vlan 10
no vlan 20
end

VLAN间路由

在路由器端口上创建多个子接口以启用VLAN间路由。

注意:在为子接口分配IP之前,必须在子接口上调用encapsulation dot1q。

conf t
interface G0/0/1.10
description Default Gateway for VLAN 10
encapsulation dot1Q 10
ip add 192.168.10.1 255.255.255.0
exit

interface G0/0/1.20
description Default Gateway for VLAN 20
encapsulation dot1Q 20
ip addr 192.168.20.1 255.255.255.0
exit

interface G0/0/1.99
description Default Gateway for VLAN 99
encapsulation dot1Q 99
ip addr 192.168.99.1 255.255.255.0
exit

interface G0/0/1
description Trunk link to S1
no shut
end

特兰克斯


创建多交换机 VLAN 干道

S1

conf t
interface Gi0/1
description Trunk Line to S2 Gi0/1
switchport mode trunk
switchport trunk native vlan 99
switchport trunk allowed vlan 99
end

注意:记得在每个交换机的干道上设置本地 VLAN(例如设置为 99),以免出现本地 VLAN 不匹配的警告

干道验证

show interface trunk
show interface g0/1 switchport

以太通道


以太通道协议 LACP 和 PAgP 配置多个物理接口和链路,使其作为一个逻辑接口使用。您可以配置最多 8 个端口作为单个链路使用。这可以增加带宽并提高冗余性。

注意:mode active 将以太通道组设置为使用 LACP 协议

配置以太通道

在通过两条以太网电缆连接的两台交换机之间配置以太通道。

conf t
int range f0/1-2
channel-group 1 mode active
exit
int port-channel 1
switchport mode trunk
switchport trunk allowed vlan 1,2,20

验证以太通道

show interfaces trunk
show etherchannel summary

DTP(动态中继协议)


配置DTP

conf t
int gi0/1
switchport mode dynamic auto
end

或

conf t
int gi0/1
switchport mode dynamic desirable
end

禁用DTP

用于连接不支持思科专有 DTP 的设备或创建静态干道

conf t
int gi0/1
switchport mode trunk
switchport nonegotiate
end

验证DTP

show dtp interface gi0/1

高级网络

OSPFv2

OSPF 路由器 ID

所有命令
show ip ospf neighbor
show ip ospf database 
启用路由器OSPF进程

启动模式:全局,未启用

enable
conf t
router ospf 10
配置回环
enable
conf t
interface Loopback 1
ip addr 1.1.1.1 255.255.255.255
end
配置 OSPF 路由器 ID

将 1.1.1.1 替换为所需的 ID

conf t
router ospf 10
router-id 1.1.1.1
end
修改 OSPF 路由器 ID

需要用 'y' 确认提示

conf t
router ospf 10
router-id 1.1.1.2
end
clear ip ospf process

验证

show ip proto | include Router ID

OSPF - 点到点网络

网络命令语法

Router(config-router)# network network-address wildcard-mask area area-id

使用网络命令配置 OSPF

以下配置将三个互相连接的路由器配置为 OSPF 点到点网络的三角形。

conf t
router ospf 10
network 10.10.1.0 0.0.0.255 area 0
network 10.10.1.4 0.0.0.3 area 0
network 10.10.1.12 0.0.0.3 area 0
end
使用整个千兆接口
conf t
router ospf 10
network 10.10.1.1 0.0.0.0 area 0
network 10.10.1.5 0.0.0.0 area 0
network 10.10.1.14 0.0.0.0 area 0
end
使用 ip ospf 配置 OSPF

直接在接口上配置 OSPF,而不是使用 network 命令。

Grammar: Router(config-if)# ip ospf <process-id> area <area-id>

R1(config)# router ospf 10
R1(config-router)# no network 10.10.1.1 0.0.0.0 area 0
R1(config-router)# no network 10.1.1.5 0.0.0.0 area 0
R1(config-router)# no network 10.1.1.14 0.0.0.0 area 0
R1(config-router)# interface GigabitEthernet 0/0/0
R1(config-if)# ip ospf 10 area 0
R1(config-if)# interface GigabitEthernet 0/0/1 
R1(config-if)# ip ospf 10 area 0
R1(config-if)# interface Loopback 0
R1(config-if)# ip ospf 10 area 0
R1(config-if)#
OSPF 被动接口
conf t
router ospf 10
passive-interface loopback 0
end
conf t
router ospf 10
passive-interface Gi0/0/0
end
查找指定路由器和备份
show ip ospf interface GigabitEthernet 0/0/0
将 OSPF 从广播模式改为点对点模式
conf t
interface GigabitEthernet 0/0/0
ip ospf network point-to-point
环回和点对点网络

环回可以用来模拟真实的局域网

conf t
interface Loopback 0
ip ospf network point-to-point
show ip route | include 10.10.1

多接入 OSPF 网络

配置 OSPF 优先级
conf t
int g0/0/1
ip ospf priority 255
end

其中 255 可以是从 0 到 255 的值,数值越高路由器被选为 DR 的可能性越大。

修改单区域 OSPF

调整参考带宽
Router# router ospf 10
Router(config-router) auto-cost reference bandwidth 1000

其中 1000 是链路速度,单位为 Mbps 常用值:10、100、1000

手动设置 OSPF 链路成本
conf t
int g0/0/1
ip ospf cost 25
interface l0
ip ospf cost 15
end
显示 OSPF Hello 包间隔
show ip ospf int g0/0/1
设置OSPF Hello报文间隔
Router(config-if)# ip ospf hello-interval <seconds>
conf t
int g0/0/1
ip ospf hello-interval 30
end

注意:dead-interval 会自动设置为 hello-interval * 4

设置 OSPF 死亡间隔

OSPF 默认路由

传播默认路由
conf t
ip route 0.0.0.0 0.0.0.0 loopback 1
router ospf 10
default-information originate
验证传播的默认路由
show ip route | begin Gateway

验证单区域OSPF

验证OSPF邻居
show ip ospf neighbor
验证 OSPF 协议
show ip protocols
验证 OSPF 进程信息
show ip ospf
验证 OSPF 接口设置
show ip ospf int g0/0/1
show ip ospf int brief

其中 g0/0/1 是你希望查看 OSPF 信息的接口。

conf t
int g0/0/1
ip ospf dead-interval 100
end

操作指南

FTP 服务器使用

  1. 克隆仓库:

    git clone https://github.com/grplyler/cisco-utils
  2. 安装 python 依赖(用于 ftp 服务器):

    pip install -r requirements.txt
  3. 运行 python ftp_server.py

    python3 ftp_server.py
  4. 将脚本拉到网络设备上(警告:请备份以避免任何损失)

    Switch#> copy ftp://192.168.1.10/sw_base.txt running-config

    将192.168.1.10替换为连接到交换机或路由器的计算机的IP地址。

在Fedora工作站上安装Packet Tracer

(本教程的版权归 philpinch 所有,来自 Fedora 论坛)

1. 登录到Fedora GNOME桌面

删除旧版本的PacketTracer(如有必要):

rm -rf /opt/pt
rm -rf /usr/share/applications/cisco-pt7.desktop
rm -rf /usr/share/applications/cisco-ptsa7.desktop
rm -rf /usr/share/icons/hicolor/48x48/apps/pt7.png

2. 从 netacad 网站下载 PacketTracer_730_amd64.deb 包。

打开一个终端:

mkdir -p tmp/pt730

将 PacketTracer_730_amd64.deb 包复制到 tmp/pt730

4. 我们将要在此文件夹中提取 deb 文件:

cd tmp/pt730
ar -xv PacketTracer_730_amd64.deb
mkdir control
tar -C control -Jxf control.tar.xz
mkdir data
tar -C data -Jxf data.tar.xz

5. 复制 PacketTracer 文件以安装它:

cd data
cp -r usr /
cp -r opt /

6. 配置 Gnome 环境:

sudo xdg-desktop-menu install /usr/share/applications/cisco-pt7.desktop
sudo xdg-desktop-menu install /usr/share/applications/cisco-ptsa7.desktop
sudo update-mime-database /usr/share/mime
sudo gtk-update-icon-cache --force --ignore-theme-index /usr/share/icons/gnome
sudo xdg-mime default cisco-ptsa7.desktop x-scheme-handler/pttp
ln -sf /opt/pt/packettracer /usr/local/bin/packettracer

在 Linux 上使用 minicom 访问控制台

正在建设中

在 Linux 上使用 stty 配置串口

使用 stty 设置默认配置为 Cisco 控制台默认值,9600 bps,8N1,无流控:

stty -F /dev/ttyUSB0 9600 litout -crtscts

或者:

stty -F /dev/ttyUSB0 cs8 -parenb -cstopb -echo raw speed 9600

 # What the arguments mean:
 #   cs8:     8 data bits
 #   -parenb: No parity (because of the '-')
 #   -cstopb: 1 stop bit (because of the '-')
 #   -echo: Without this option, Linux will sometimes automatically send back
 #          any received characters, even if you are just reading from the serial
 #          port with a command like 'cat'. Some terminals will print codes
 #          like "^B" when receiving back a character like ASCII ETX (hex 03).

在 Linux 上使用屏幕进行控制台访问

为此,您需要一条 USB 控制台线。可以在亚马逊上以大约 9-12 美元购买。

  1. 将 USB 控制台线从计算机的 USB 端口连接到 Cisco RJ-45 控制台端口。

  2. 如果您还没有安装 screen 程序,请安装它。

apt install screen
  1. 找到 USB 设备。

如果这是你插入的第一个 USB 串口设备,它应该是 /dev/ttyUSB0。第二个应该是 /dev/ttyUSB1,以此类推。

你可以使用 ls /dev | grep USB 来验证。

  1. 运行 screen

你将需要 root 权限。

screen /dev/ttyUSB0

以特定波特率运行。

screen /dev/ttyUSB0 9600
screen /dev/ttyUSB0 115200

要退出屏幕,请按 Ctrl-a,然后按 Ctrl-d

如果您的连接有问题,例如延迟或不稳定,cisco 串行连接默认需要以下设置:

  • 9600 波特率
  • 8 数据位
  • no 奇偶校验
  • 1 停止位
  • no 流程控制

要使用 screen 精确地做到这一点:

screen /dev/ttyS0 9600,cs8,-parenb,-cstopb,-hupcl
screen /dev/ttyS0 19200,cs8,-parenb,-cstopb,-hupcl
screen /dev/ttyS0 115200,cs8,-parenb,-cstopb,-hupcl

使用 odd 奇偶校验:

screen /dev/ttyS0 9600,cs8,parenb,parodd,-cstopb,-hupcl

使用 even 奇偶校验:

screen /dev/ttyS0 9600,cs8,parenb,-parodd,-cstopb,-hupcl

更多详情请参见 http://www.noah.org/wiki/Screen_notes

通过控制台进行 Linux 文件传输(minicom / xmodem)

操作指南即将到来!

通过控制台进行 Windows 文件传输(HyperTerminal / xmodem)

操作指南即将到来!

工具

子网划分/计算

ipcalc (*nix)

Debian/Ubuntu

apt install ipcalc

费多拉

dnf install ipcalc
yum install ipcalc

sipcalc (*nix)

Debian/Ubuntu

apt install sipcalc

或用你的包管理器替换 apt

whatmask(*nix)

示例用法

$ whatmask 10.0.1.12/30

------------------------------------------------
           TCP/IP NETWORK INFORMATION
------------------------------------------------
IP Entered = ..................: 10.0.1.12
CIDR = ........................: /30
Netmask = .....................: 255.255.255.252
Netmask (hex) = ...............: 0xfffffffc
Wildcard Bits = ...............: 0.0.0.3
------------------------------------------------
Network Address = .............: 10.0.1.12
Broadcast Address = ...........: 10.0.1.15
Usable IP Addresses = .........: 2
First Usable IP Address = .....: 10.0.1.13
Last Usable IP Address = ......: 10.0.1.14

安装

Debian/Ubuntu

apt install whatmask

或者用你的包管理器替换 apt

本站来源与版权声明
  • 本文标题:cisco-cheatsheet - 思科备忘单:用于引导
  • 本文链接:https://www.cn121.com/manual/grplyler-cisco-cheatsheet.html
  • 原项目:grplyler/cisco-cheatsheet 版权归原作者 grplyler 及贡献者所有
  • 收录信息:本站于 2026-11-22 收录本项目,本页所列协议与仓库指标均为收录当时的状态;该日期之后原项目的版本更新与协议变更,本页不作同步。
  • 开源协议:收录时本项目采用 GPL-3.0(查看 LICENSE 原文),本站译文为其衍生内容;使用、修改、分发请以该仓库 LICENSE 原文为准。基于本页内容形成的衍生作品, 须同样以该协议发布。
  • 站点出处:本文首发于 OneTwoOne,收录自 GitHub 开源项目 grplyler/cisco-cheatsheet。
  • 翻译说明:本页正文为人工智能生成内容——由机器翻译对原项目 README 初译、经程序校验排版,可能存在错漏,请以原项目文档为准。
  • 引用声明:商业转载、第三方聚合或 AI 检索训练引用时,请务必保留以上来源出处、本文永久链接,以及原项目的版权声明与许可信息。
  • 下架通道:若原项目此后变更或收紧了许可协议、或作者/权利人认为本站的收录方式(译文、排版适配、简介翻译等)超出其授权范围,请通过 xyd3302001@163.com 发送下架通知,并附上项目地址与本页链接。本站核实后将第一时间删除本页内容,或改为不复制原文的目录性收录;署名更正等其他要求可一并提出。