2.1 华为 VRP 系统基础
欢迎来到实战篇!从现在开始,我们将通过命令行(CLI)来驾驭网络设备。 华为设备运行的操作系统称为 VRP (Versatile Routing Platform),通用路由平台。
1. 命令行视图 (The Views)
VRP 的命令行是分层级的,不同的事情要在不同的“房间”(视图)里做。
| 视图名称 | 提示符 | 作用 | 进入命令 | 退出命令 |
|---|---|---|---|---|
| 用户视图 | <Huawei> | 只能看,不能改(权限最低) | 登录即进入 | quit (断开) |
| 系统视图 | [Huawei] | 修改全局配置(改名、创建VLAN等) | system-view | quit |
| 接口视图 | [Huawei-GE0/0/0] | 修改特定接口的 IP、开关等 | interface g0/0/0 | quit |
技巧:随时按
Tab键可以补全命令;输入?可以查看帮助。
2. 第一次上手:基础配置
让我们完成一个最简单的任务:给路由器改个名,并配个 IP。
<Huawei> system-view # 1. 从用户视图进入系统视图
Enter system view, return user view with Ctrl+Z.
[Huawei] sysname R1 # 2. 修改设备名称为 R1
[R1] # (提示符变了!)
[R1] interface GigabitEthernet 0/0/0 # 3. 进入 G0/0/0 接口视图
[R1-GigabitEthernet0/0/0] ip address 192.168.1.1 24 # 4. 配置 IP (支持掩码简写 /24)
[R1-GigabitEthernet0/0/0] display this # 5. 查看当前视图下的配置
#
interface GigabitEthernet0/0/0
ip address 192.168.1.1 255.255.255.0
#
return2.1 后悔药:Undo 命令
在 VRP 中,几乎所有的配置命令前面加上 undo 关键字,就变成了删除/恢复默认该配置。
- 删除 IP 地址:
undo ip address - 删除 VLAN:
undo vlan 10 - 恢复默认主机名:
undo sysname - 关闭功能:
undo info-center enable(关闭信息中心)
记住:配置错了不要慌,
undo一下回到从前。
3. 保存配置 (Save)
非常重要! VRP 的配置默认只保存在内存中(Current Configuration)。如果断电重启,未保存的配置会全部丢失! 必须手动将其保存到设备的闪存/硬盘中(Saved Configuration)。
<R1> save
The current configuration will be written to the device.
Are you sure to continue? [Y/N]y
Now saving the current configuration to the slot 0.
Save the configuration successfully.注意:save 命令通常在用户视图 <R1> 下执行。默认保存的文件名为 vrpcfg.zip。
4. 配置文件进阶管理
除了简单的保存,我们还需要掌握如何管理这些配置文件,比如查看、删除、指定下次启动的配置等。
4.1 内存配置 vs 启动配置
- Current-Configuration (当前配置):运行在内存中。你敲入命令后立即生效,但断电即失。
- Saved-Configuration (启动配置):保存在 Flash 存储介质中(类似硬盘)。设备启动时会读取这个文件。
4.2 查看文件列表 (Dir)
使用 dir 命令查看设备存储中的文件:
<R1> dir
Directory of flash:/
Idx Attr Size(Byte) Date Time FileName
0 -rw- 1,488 Jun 06 2024 13:00:06 private-data.txt
1 -rw- 1,722 Jun 06 2024 13:18:08 vrpcfg.zip <-- 默认配置文件
2 -rw- 1,722 Jun 06 2024 13:30:00 backup.cfg <-- 备份文件4.3 备份与另存为
你可以把当前配置保存为另一个名字,用于备份:
<R1> save backup.cfg
Are you sure to save the configuration to backup.cfg? (y/n)[n]:y
...
Configuration file had been saved successfully4.4 指定下次启动文件
如果你有多个配置文件(比如 vrpcfg.zip 和 experiment.cfg),可以通过命令告诉设备下次开机用哪个:
<R1> startup saved-configuration flash:/experiment.cfg
Info: Succeeded in setting the file for booting system查看下次启动的设置:
<R1> display startup
MainBoard:
...
Startup saved-configuration file: flash:/vrpcfg.zip
Next startup saved-configuration file: flash:/experiment.cfg <-- 下次生效4.5 删除文件
删除不需要的配置文件(操作需谨慎):
<R1> delete flash:/backup.cfg
Delete flash:/backup.cfg? (y/n)[n]:y
Info: Deleting file flash:/backup.cfg...succeed.delete:放入回收站(部分设备支持undelete)。delete /unreserved:彻底删除,无法恢复。
4.6 清空配置 (重置设备)
如果你想把设备恢复出厂设置,需要清空下次启动的配置文件,然后重启:
<R1> reset saved-configuration
The saved-configuration file will be erased.
Are you sure? (y/n)[n]:y
<R1> reboot
Info: The system is now comparing the configuration...
System will reboot! Continue? (y/n)[n]:y5. 常用查询命令 (Display)
display version:查看设备版本、运行时间。display current-configuration:查看当前所有配置。display interface brief:查看接口摘要(UP/DOWN 状态)。display ip routing-table:查看路由表。
6. 实用系统管理技巧
除了基本的配置,还有一些命令能让你的操作更顺手,或者让设备更规范。
6.1 设置系统时间
日志记录和故障排查都依赖准确的时间。
<R1> clock timezone BJ add 08:00:00 # 1. 设置时区 (北京时间 +8)
<R1> clock datetime 12:00:00 2024-10-01 # 2. 设置具体时间 (HH:MM:SS YYYY-MM-DD)6.2 命令行体验优化
默认情况下,如果 10 分钟不操作,CLI 会自动退出,这在做实验时可能很烦人。
[R1] user-interface console 0 # 进入 Console 用户界面视图
[R1-ui-console0] idle-timeout 0 0 # 设置为永不超时 (分 秒),生产环境不建议设为 0
[R1-ui-console0] history-command max-size 20 # 增加历史命令缓存条数 (默认10条)6.3 设置登录标语 (Header)
为了合规或提示警告信息,可以设置登录前的 Banner。
[R1] header login information "Welcome to Huawei VRP Lab!"
[R1] header shell information "Authorized users only."6.4 快捷键 Cheat Sheet
| 快捷键 | 作用 |
|---|---|
Tab | 自动补全命令 (最常用!) |
Ctrl + Z | 直接退回到用户视图 (等同于连续 quit) |
Ctrl + C | 终止当前运行的命令 (如 ping, trace) |
Ctrl + ] | 终止连接 (如 Telnet 卡死时) |
Ctrl + A | 光标移动到行首 |
Ctrl + E | 光标移动到行尾 |
? | 查看帮助 / 查看可用的命令 |
7. 基础安全:设置 Console 密码
默认情况下,华为路由器的 Console 口(串口)登录是不需要密码的,这非常不安全。我们来设置一个密码。
方式一:仅密码验证 (Password Mode)
这是最简单的模式,所有管理员共用一个密码。
[R1] user-interface console 0
[R1-ui-console0] authentication-mode password
Please configure the login password (maximum length 16):huawei123注:新版本 VRP 输入密码时不会回显。
方式二:AAA 验证 (用户名+密码)
推荐使用 AAA (Authentication, Authorization, Accounting) 模式,可以为不同管理员创建不同账号。
# 1. 创建用户
[R1] aaa
[R1-aaa] local-user admin password cipher huawei123
[R1-aaa] local-user admin service-type terminal # 允许该用户通过终端(Console)登录
[R1-aaa] local-user admin privilege level 15 # 赋予最高权限 (0-15)
[R1-aaa] quit
# 2. 应用到 Console 接口
[R1] user-interface console 0
[R1-ui-console0] authentication-mode aaa退出并重新登录,就会提示输入用户名和密码了。