路由器配置是网络管理中的基础工作,正确的配置命令能够确保网络的稳定运行和安全防护,以下将详细介绍路由器的基本配置步骤及常用命令,包括初始化配置、接口配置、路由配置、安全配置及高级功能配置等内容,并结合表格说明关键参数,最后附相关FAQs解答。

初始化配置
路由器首次启动时,需通过控制台端口(Console)连接进行初始化配置,通常使用终端软件(如SecureCRT、PuTTY)设置波特率为9600、数据位8、停止位1、无校验、无流控,进入命令行界面后,首先进入全局配置模式:
Router> enable Router# configure terminal Router(config)#
配置主机名和密码
主机名用于标识设备,密码用于限制访问权限:
Router(config)# hostname R1 R1(config)# enable secret cisco123 # 设置特权模式加密密码 R1(config)# line console 0 R1(config-line)# password console123 R1(config-line)# login R1(config-line)# exit R1(config)# line vty 0 4 R1(config-line)# password telnet123 R1(config-line)# login R1(config-line)# exit
配置管理IP地址
若需通过远程管理路由器,需配置接口IP(通常为Loopback0或物理接口):
R1(config)# interface loopback 0 R1(config-if)# ip address 1.1.1.1 255.255.255.255 R1(config-if)# no shutdown R1(config-if)# exit
物理接口配置
进入接口模式并激活接口
以GigabitEthernet0/0为例:

R1(config)# interface gigabitethernet 0/0 R1(config-if)# description WAN_Link_to_Internet # 接口描述 R1(config-if)# ip address 192.168.1.1 255.255.255.0 # 配置IP和子网掩码 R1(config-if)# no shutdown # 激活接口 R1(config-if)# exit
配置接口速率和双工模式
R1(config-if)# speed 1000 # 速率可选10/100/1000 R1(config-if)# duplex full # 双工模式half/full/auto
接口状态排查命令
配置完成后,使用以下命令验证接口状态:
R1# show ip interface brief # 查看接口IP和状态 R1# show interfaces gigabitethernet 0/0 # 查看接口详细信息
路由配置
静态路由配置
当网络结构简单或需要指定特定路径时使用静态路由:
R1(config)# ip route 10.0.0.0 255.255.255.0 192.168.1.2 # 目标网络/子网掩码/下一跳IP R1(config)# ip route 0.0.0.0 0.0.0.0 192.168.1.2 # 默认路由
动态路由配置(OSPF)
对于中大型网络,OSPF协议更为适用,以下为基本OSPF配置:

R1(config)# router ospf 1 # 启动OSPF进程号为1 R1(config-router)# network 192.168.1.0 0.0.0.255 area 0 # 宣告直连网段及区域 R1(config-router)# network 1.1.1.1 0.0.0.0 area 0 # 宣告Loopback接口 R1(config-router)# exit
路由协议验证
R1# show ip route # 查看路由表 R1# show ip ospf neighbor # 查看OSPF邻居关系 R1# show ip ospf database # 查看OSPF数据库
安全配置
访问控制列表(ACL)
ACL用于控制数据流的访问权限,标准ACL和扩展ACL配置如下:
- 标准ACL(基于源IP)
R1(config)# access-list 10 permit 192.168.2.0 0.0.0.255 # 允许192.168.2.0/24网段 R1(config)# access-list 10 deny any # 拒绝其他所有 R1(config)# interface gigabitethernet 0/1 R1(config-if)# ip access-group 10 in # 在接口应用ACL(in/out)
- 扩展ACL(基于源/目标IP及端口)
R1(config)# access-list 101 permit tcp 192.168.1.0 0.0.0.255 any eq 80 # 允许HTTP访问 R1(config)# access-list 101 deny icmp any any # 拒绝ICMP
端口安全
限制接口接入的MAC地址数量,防止未授权设备接入:
R1(config)# interface fastethernet 0/0 R1(config-if)# switchport port-security # 启用端口安全(二层交换机命令,部分路由器需使用mac-based限流) R1(config-if)# port-security maximum 2 # 最大允许2个MAC地址 R1(config-if)# port-security violation shutdown # 违规关闭端口
NAT配置
实现内网用户访问外网,常用PAT(端口地址转换):
R1(config)# ip nat inside source list 1 interface gigabitethernet 0/0 overload # ACL 1定义内网网段 R1(config)# access-list 1 permit 192.168.1.0 0.0.0.255 R1(config)# interface gigabitethernet 0/0 R1(config-if)# ip nat outside # 外网接口 R1(config-if)# exit R1(config)# interface gigabitethernet 0/1 R1(config-if)# ip nat inside # 内网接口
高级功能配置
DHCP服务配置
为内网主机自动分配IP地址:
R1(config)# ip dhcp pool LAN_POOL R1(dhcp-config)# network 192.168.1.0 255.255.255.0 R1(dhcp-config)# default-router 192.168.1.1 R1(dhcp-config)# dns-server 8.8.8.8 114.114.114.114 R1(dhcp-config)# lease 7 # 租期7天 R1(config)# ip dhcp excluded-address 192.168.1.1 192.168.1.10 # 排除静态IP
QoS配置
对关键业务流量进行带宽保障,限制非重要流量:
R1(config)# class-map VOICE # 定义流量类别 R1(config-cmap)# match ip dscp ef # 匹配DSCP EF(语音流量) R1(config)# policy-map QOS_POLICY # 定义策略 R1(config-pmap)# class VOICE R1(config-pmap-c)# bandwidth percent 30 # 分配30%带宽 R1(config-pmap-c)# exit R1(config-pmap)# class class-default # 默认类别 R1(config-pmap-c)# bandwidth percent 70 # 剩余带宽 R1(config-pmap-c)# exit R1(config)# interface gigabitethernet 0/0 R1(config-if)# service-policy output QOS_POLICY # 在接口应用策略
VPN配置(IPSec VPN)
站点到站点VPN基本配置:
R1(config)# crypto isakmp policy 10 # 定义IKE策略 R1(config-isakmp)# encryption aes 256 R1(config-isakmp)# hash sha R1(config-isakmp)# authentication pre-share R1(config-isakmp)# exit R1(config)# crypto isakmp key cisco123 address 203.0.113.2 # 对端公网IP和预共享密钥 R1(config)# crypto ipsec transform-set TS esp-aes 256 esp-sha-hmac # 定义IPSec转换集 R1(cfg-crypto-trans)# mode tunnel R1(cfg-crypto-trans)# exit R1(config)# crypto map VPN_MAP 10 ipsec-isakmp # 定义加密映射 R1(config-crypto-map)# match address 100 # 匹配ACL 100(需定义感兴趣流量) R1(config-crypto-map)# set peer 203.0.113.2 R1(config-crypto-map)# set transform-set TS R1(config-crypto-map)# exit R1(config)# interface gigabitethernet 0/0 R1(config-if)# crypto map VPN_MAP # 应用加密映射
配置保存与备份
完成配置后,需保存至启动配置,避免重启丢失:
R1# copy running-config startup-config # 保存当前配置到NVRAM R1# write memory # 等效命令 R1# show running-config # 查看当前配置 R1# show startup-config # 查看启动配置
关键配置参数表
配置项 | 命令示例 | 说明 |
---|---|---|
主机名 | hostname R1 |
设备标识名称 |
特权模式密码 | enable secret cisco123 |
加密存储,优先于enable password |
接口IP地址 | ip address 192.168.1.1 255.255.255.0 |
配置接口IP及子网掩码 |
静态路由 | ip route 10.0.0.0 255.255.255.0 192.168.1.2 |
目标网络、子网掩码、下一跳或出接口 |
OSPF进程 | router ospf 1 |
进程号本地有效,范围1-65535 |
ACL标准规则 | access-list 10 permit 192.168.1.0 0.0.0.255 |
序列号1-99,基于源IP |
NAT动态转换 | ip nat inside source list 1 interface g0/0 overload |
使用ACL定义内网网段,PAT转换 |
DHCP地址池 | ip dhcp pool LAN_POOL |
定义DHCP地址池名称 |
IPSec VPN转换集 | crypto ipsec transform-set TS esp-aes 256 esp-sha-hmac |
定义加密和认证算法 |
相关问答FAQs
Q1: 如何查看路由器当前运行的路由协议及其邻居状态?
A1: 使用以下命令查看路由协议和邻居状态:
- 查看所有路由协议:
show ip protocols
- 查看OSPF邻居状态:
show ip ospf neighbor
- 查看BGP邻居状态:
show ip bgp summary
- 查看EIGRP邻居状态:
show ip eigrp neighbors
若邻居未建立,需检查配置的网段是否在相同区域(OSPF)、AS号是否一致(BGP/EIGRP)以及网络连通性。
Q2: 配置ACL后无法访问网络,如何排查?
A2: ACL配置错误可能导致流量被拒绝,排查步骤如下:
- 检查ACL规则顺序:ACL默认按自上而下匹配,一旦匹配某条规则即停止,需确保拒绝规则位于允许规则之后。
- 验证ACL应用方向:
show ip access-groups
确认ACL是应用在接口的入方向(in)还是出方向(out),inbound ACL用于过滤流入流量。 - 检查ACL通配符掩码:例如
168.1.0 0.0.0.255
表示匹配整个C类网段,若误写为0.0.255
则仅匹配单个IP。 - 使用
debug ip packet detail
观察数据包匹配情况(调试模式需谨慎使用,建议在低峰期操作)。 - 临时关闭ACL测试:
no ip access-group 10 in
若恢复访问,则说明ACL规则需调整。