访问路由器CLI通常通过控制台线、Telnet或SSH连接,连接后需输入用户名密码认证,进入用户模式,输入
enable
命令并提供密码可进入特权模式,获得配置权限执行管理命令。
- 物理控制台连接(首次配置必备)
使用Console线连接路由器Console口与电脑串口/USB(需转接器),通过终端软件(如PuTTY、SecureCRT)设置:- 波特率:9600
- 数据位:8
- 奇偶校验:None
- 停止位:1
- 流控:None
- 远程访问(需预先配置)
- Telnet:
telnet [路由器IP地址]
(不安全,仅限测试环境) - SSH:
ssh -l [用户名] [路由器IP地址]
(推荐,加密通信)
- Telnet:
理解配置模式(层级结构)
思科CLI采用分层模式,不同模式执行不同命令:
- 用户模式 (User EXEC Mode)
- 提示符:
Router>
- 功能:基础查看命令(
show
),无修改权限。 - 进入:登录后默认模式。
- 升级到特权模式:
enable
或en
- 提示符:
- 特权模式 (Privileged EXEC Mode)
- 提示符:
Router#
- 功能:高级查看、调试(
debug
)、测试(ping
,traceroute
)、进入配置模式。 - 进入:在用户模式下输入
enable
(需密码)。 - 降级到用户模式:
disable
或exit
- 进入全局配置模式:
configure terminal
或conf t
- 提示符:
- 全局配置模式 (Global Configuration Mode)
- 提示符:
Router(config)#
- 功能:修改全局参数(主机名、密码、IP路由等)。
- 进入:在特权模式下输入
configure terminal
。 - 退出到特权模式:
exit
或end
(或Ctrl+Z
直接退到特权模式) - 子配置模式均由此进入。
- 提示符:
- 子配置模式 (Subconfiguration Modes)
- 接口配置模式 (Interface Mode):
- 提示符:
Router(config-if)#
- 进入:在全局模式下输入
interface [接口类型] [接口号]
(如interface gigabitethernet 0/0
或int g0/0
) - 功能:配置特定接口的IP地址、状态(up/down)、描述等。
- 提示符:
- 线路配置模式 (Line Mode):
- 提示符:
Router(config-line)#
- 进入:在全局模式下输入
line [线路类型] [起始编号]-[结束编号]
(如line console 0
,line vty 0 4
) - 功能:配置Console、Telnet、SSH访问的密码、超时等。
- 提示符:
- 路由协议配置模式 (Router Config Mode):
- 提示符:
Router(config-router)#
- 进入:在全局模式下输入
router [协议]
(如router ospf 1
,router rip
) - 功能:配置动态路由协议参数。
- 提示符:
- 退出子模式:
exit
(返回上一级模式) 或end
/Ctrl+Z
(直接返回特权模式)。
- 接口配置模式 (Interface Mode):
核心配置命令详解
-
基础配置
- 设置主机名:
Router> enable Router# configure terminal Router(config)# hostname [新主机名] (如 hostname Core-Switch-01) Core-Switch-01(config)#
- 设置特权密码(加密存储):
Core-Switch-01(config)# enable secret [强密码]
- 配置控制台(Console)访问密码:
Core-Switch-01(config)# line console 0 Core-Switch-01(config-line)# password [密码] Core-Switch-01(config-line)# login (强制登录验证) Core-Switch-01(config-line)# exit
- 配置远程登录(VTY)访问密码(推荐SSH):
Core-Switch-01(config)# line vty 0 4 (通常配置0-4共5条线路) Core-Switch-01(config-line)# transport input ssh (仅允许SSH) 或 transport input telnet ssh (允许Telnet和SSH) Core-Switch-01(config-line)# login local (使用本地用户名密码验证,更安全) Core-Switch-01(config-line)# exit Core-Switch-01(config)# username [用户名] secret [强密码] (创建本地用户)
- 加密明文密码:
Core-Switch-01(config)# service password-encryption
- 设置主机名:
-
接口配置
- 进入接口配置模式:
Core-Switch-01(config)# interface [接口类型] [接口号] (如 interface gigabitethernet 0/0 或 int g0/0)
- 配置IP地址和子网掩码:
Core-Switch-01(config-if)# ip address [IP地址] [子网掩码] (如 ip address 192.168.1.1 255.255.255.0)
- 启用接口:
Core-Switch-01(config-if)# no shutdown 或 no shut
- 关闭接口:
Core-Switch-01(config-if)# shutdown
- 添加接口描述:
Core-Switch-01(config-if)# description [描述信息] (如 description Link_to_Branch_Office)
- 进入接口配置模式:
-
路由配置
- 静态路由:
Core-Switch-01(config)# ip route [目标网络] [目标子网掩码] [下一跳IP地址或出站接口] (如 ip route 10.1.2.0 255.255.255.0 192.168.1.2 或 ip route 10.1.2.0 255.255.255.0 gigabitethernet0/1)
- 动态路由 (以OSPF为例):
Core-Switch-01(config)# router ospf [进程ID] (如 router ospf 10) Core-Switch-01(config-router)# network [直连网络地址] [反掩码] area [区域号] (如 network 192.168.1.0 0.0.0.255 area 0) Core-Switch-01(config-router)# exit
- 静态路由:
-
安全配置 (访问控制列表 – ACL)
- 标准ACL (基于源IP):
Core-Switch-01(config)# access-list [ACL编号] permit/deny [源IP] [通配符掩码] (标准ACL编号范围1-99, 1300-1999, 如 access-list 10 permit 192.168.1.0 0.0.0.255) Core-Switch-01(config)# interface [应用接口] (如 int g0/0) Core-Switch-01(config-if)# ip access-group [ACL编号] in/out (如 ip access-group 10 in) # 注意方向(in/out)至关重要
- 扩展ACL (基于源/目IP、协议、端口):
Core-Switch-01(config)# access-list [ACL编号] permit/deny [协议] [源IP] [源通配符] [目标IP] [目标通配符] [eq 端口号] (扩展ACL编号范围100-199, 2000-2699, 如 access-list 110 permit tcp 192.168.1.0 0.0.0.255 any eq 80) Core-Switch-01(config)# interface [应用接口] Core-Switch-01(config-if)# ip access-group [ACL编号] in/out
- 重要: ACL末尾隐含
deny any
,确保有明确的permit
语句允许必要流量。
- 标准ACL (基于源IP):
-
服务配置 (DHCP服务器)
Core-Switch-01(config)# ip dhcp pool [地址池名称] (如 ip dhcp pool LAN_POOL) Core-Switch-01(dhcp-config)# network [网络地址] [子网掩码] (如 network 192.168.10.0 255.255.255.0) Core-Switch-01(dhcp-config)# default-router [默认网关IP] (如 default-router 192.168.10.1) Core-Switch-01(dhcp-config)# dns-server [DNS服务器IP] (如 dns-server 8.8.8.8) Core-Switch-01(dhcp-config)# exit Core-Switch-01(config)# ip dhcp excluded-address [起始IP] [结束IP] (排除不分配的IP,如 ip dhcp excluded-address 192.168.10.1 192.168.10.10)
关键操作与验证
- 保存配置:
这是最重要的一步! 未保存的配置在重启后会丢失。Core-Switch-01# copy running-config startup-config 或 write memory 或 wr (常用简写)
- 查看配置:
show running-config
或sh run
:查看当前运行配置(未保存)。show startup-config
或sh start
:查看已保存的启动配置。show interfaces [接口]
或sh int [接口]
:查看接口状态与统计信息。show ip interface brief
或sh ip int br
:查看所有接口IP状态摘要。show ip route
或sh ip ro
:查看路由表。show access-lists
或sh access-lists
:查看配置的ACL及匹配计数。show protocols
或sh prot
:查看全局和接口层协议状态。
- 测试连通性:
ping [目标IP或主机名]
:测试网络连通性。traceroute [目标IP或主机名]
:跟踪数据包路径。
重要安全与操作规范
- 备份配置: 定期使用
copy running-config tftp:
或copy startup-config tftp:
备份配置到TFTP服务器。 - 密码管理: 始终使用
enable secret
而非过时的enable password
,为特权用户创建本地账户(username ... secret
)并配合login local
。 - 禁用未用服务: 如
no ip http server
,no ip http secure-server
(除非需要),no cdp run
(在非管理网络)。 - SSH优先: 禁用Telnet(
no transport input telnet
),强制使用SSH(transport input ssh
)。 - ACL应用方向: 深刻理解
in
(进入路由器的流量)和out
(离开路由器的流量)的区别。 - 变更管理: 生产环境变更前务必在测试环境验证,并在维护窗口进行,做好回滚计划(备份!)。
- 文档记录: 详细记录配置变更原因、时间、操作人。
故障排查起点
- 检查接口状态(
sh int
/sh ip int br
):确认物理层和数据链路层是否up/up
。 - 检查IP配置(
sh run int [接口]
):确认IP地址、掩码正确,接口未shutdown
。 - 检查路由表(
sh ip route
):确认存在到达目标网络的路由条目。 - 检查ACL(
sh access-lists
):查看ACL匹配计数,确认是否因ACL阻止了流量。 - 测试连通性(
ping
/traceroute
):从源到目标逐跳测试。
免责声明与最佳实践
- 非生产环境练习: 强烈建议在实验室或模拟器(如Cisco Packet Tracer, GNS3, EVE-NG)中练习命令。
- 理解原理: 死记硬背命令危险,务必理解网络基础(IP寻址、子网划分、路由协议、ACL逻辑)。
- 查阅官方文档: 思科命令集庞大且版本间有差异。最权威的参考永远是思科官方文档,使用获取上下文帮助(如
configure terminal ?
)。 - 最小权限原则: 仅授予用户完成工作所需的最低权限。
- 保存!保存!保存! 再次强调,未保存的配置会在设备重启或断电后丢失。
引用说明:
本文核心知识框架与命令语法参考自思科官方文档(Cisco IOS Command References and Configuration Guides),并结合了行业公认的网络工程最佳实践,具体命令行为可能因路由器型号和IOS/IOS-XE版本略有差异,请以实际设备环境为准,思科官方文档库是获取最准确、最新信息的权威来源。
原创文章,发布者:酷番叔,转转请注明出处:https://cloud.kd.cn/ask/6307.html