华为交换机恢复出厂设置的三种方法(PNP键/CLI命令/清空配置) - 夜莺博客

华为交换机恢复出厂设置的三种方法(PNP键/CLI命令/清空配置)

交换机利旧使用时,通常需要先恢复出厂设置再升级到最新版本。华为官方渠道总结了三种恢复出厂配置的方法:长按 PNP 键、一键式 reset factory-configuration、以及通过 reset saved-configuration 清空配置文件。本文结合实操讲解三种方法的适用场景、命令步骤和恢复出厂后的必要配置。

先交代本文与站内已有文章的分工:《华为交换机恢复出厂设置:三种方法的区别与验证》回答的是"三种方法在原理和文件保留范围上差在哪",本文只做一件事——把三种方法的命令逐条拆开对比(提示语、交互确认、影响范围、耗时、能否脚本化),并给出恢复后可以照抄执行的一次性验证脚本,方便现场操作和批量巡检。

准备工作

利旧交换机若已使用过,建议先保存当前配置用于后续恢复;恢复出厂后设备无法远程登录,务必使用 Console 口操作(默认 9600-8-N-1,无流控)。先备份、再动手,否则一旦回退,业务配置就没有退路。

<HUAWEI> save
The current configuration will be written to the device. Are you sure to continue? [Y/N]:y
Info: Please input the file name ( *.cfg, *.zip ) [vrpcfg.zip]:
Now saving the current configuration to the slot 0.
Info: Save the configuration successfully.

<HUAWEI> dir flash:/*.zip
Directory of flash:/
  Idx  Attr     Size(Byte)  Date        Time       FileName
    1  -rw-        2,548  Jan 01 2026 00:12:31   vrpcfg.zip

<HUAWEI> tftp 10.10.10.10 put flash:/vrpcfg.zip vrpcfg-backup.zip
Info: Transfer file in binary mode.
Info: Uploading the file to the remote TFTP server... Done.

把 display current-configuration 的输出一并另存为本地 txt,双份备份更稳妥。常见缺省用户名为 admin,缺省密码为 admin@huawei.com;如提示错误,请参考对应型号和版本的官方文档。

方法一:长按 PNP 键恢复出厂配置

对于支持 PNP 键的设备,长按(6 秒以上)PNP 键,设备即恢复出厂配置并自动重新启动。该方法无需登录设备,操作最简单,适合现场快速处理。

  • 适用前提:机箱面板带 PNP 键(多款 S 系列接入交换机具备),且设备能正常启动;
  • 操作要点:长按期间不要松手,观察到指示灯变化后再松开,设备会自动重启;
  • 恢复范围:与一键式恢复一致——除系统软件包、补丁、模块和许可文件外全部清除;
  • 局限:只能现场操作,无法从远端执行,也写不进自动化脚本。

方法二:一键式恢复出厂配置

在用户视图下执行 reset factory-configuration 命令:

<HUAWEI> reset factory-configuration
Warning: The command will delete all the configurations and files (except the
startup, patch, module, and license files) from the device. Continue? [Y/N]:y
Warning: The system will reboot after configurations and files are deleted. Continue? [Y/N]:y

该命令删除除系统软件包、补丁、模块和许可文件外的所有配置与文件,并自动重启。它是三种方法里"清得最彻底"的一种:手工创建的配置文件、自定义名称的启动文件都会被删除,flash 目录回到刚出厂的状态。

命令需要两次确认,两次都输入 y 才会真正执行;任何一次输入 n 或直接回车,命令立即中止且不产生副作用。该命令的可用性取决于软件版本,若提示无法识别,请改用方法三,或先升级系统软件再执行。

方法三:通过清空配置文件恢复出厂配置

<HUAWEI> reset saved-configuration
Warning: The action will delete the saved configuration in the device.
The configuration will be erased to reconfigure. Continue? [Y/N]:y
Info: Succeeded in clearing the configuration in the device.

<HUAWEI> reboot
Warning: The configuration has been modified, and it will be saved to the next
startup saved-configuration file flash:/vrpcfg.zip. Continue? [Y/N]:n
System will reboot! Continue?[Y/N]:y

重启时系统询问是否保存当前配置,必须选择 N,否则配置会重新写入启动文件,前面的清空动作等于白做——这是实操中最高频的翻车点。此方法只清除当前配置与配置文件,其他数据文件保留。

执行顺序不能颠倒:先 reset saved-configuration 清空,再 reboot,最后在保存提示处回答 N。如果先 reboot 再清空,设备已经带着旧配置重新启动了一轮。

三种方法命令逐条对比

把三条路径放在一张表里看,现场选型会快很多:

对比项 方法一:PNP 键 方法二:reset factory-configuration 方法三:reset saved-configuration + reboot
是否需要登录设备 否,人到现场按键 是,用户视图执行 是,用户视图执行
命令条数 无(长按 6 秒) 1 条,2 次确认 2 条,2 次确认
是否自动重启 自动重启 自动重启 需手工执行 reboot
删除范围 配置 + 文件(保留系统包/补丁/模块/许可) 配置 + 文件(保留系统包/补丁/模块/许可) 仅当前配置与启动配置文件
flash 其他数据文件 删除 删除 保留
能否脚本化 不能 可以(依次回答 y / y) 可以(依次回答 y / n / y)
典型耗时 重启约 2~5 分钟 删除 + 重启约 3~8 分钟 清空秒级 + 重启约 2~5 分钟
适用场景 密码丢失、Console 也进不去 整机下线利旧、要求彻底清干净 只想去掉配置、保留日志等文件

一句话选型:能登录就用方法二(最彻底);只想清配置、保留其他文件用方法三;登不进去才用方法一。

恢复后的一次性验证脚本

"恢复成功"不能靠感觉,要有明确判据:display saved-configuration 无输出、display current-configuration 只剩缺省项、flash 中没有自定义文件、启动文件指向正常的 vrpcfg.zip。下面这组命令可以一次性粘贴到 Console 执行:

<HUAWEI> screen-length 0 temporary
<HUAWEI> display startup
<HUAWEI> display saved-configuration
<HUAWEI> display current-configuration
<HUAWEI> dir flash:
<HUAWEI> display version
<HUAWEI> display device

需要给多台设备做"恢复后自检"时,可以用下面这段脚本通过 Console 把同样的命令跑一遍并自动判定,省掉逐台肉眼核对:

# 依赖: pip install pyserial
import time, serial

PORT, BAUD = "/dev/tty.usbserial-A1B2C3", 9600
CMDS = ["display startup", "display saved-configuration",
        "display current-configuration", "dir flash:"]

def run(ser, cmd, wait=2.0):
    ser.reset_input_buffer()
    ser.write((cmd + "\r").encode())
    time.sleep(wait)
    return ser.read_all().decode("utf-8", "ignore")

with serial.Serial(PORT, BAUD, timeout=1) as ser:
    time.sleep(3)
    ser.write(b"\r")                      # 唤醒 Console,确认已进入用户视图
    time.sleep(1)
    run(ser, "screen-length 0 temporary", 1)
    out = {c: run(ser, c) for c in CMDS}

verdict = {
    "启动配置文件为缺省 vrpcfg.zip": "vrpcfg.zip" in out["display startup"],
    "已无保存的配置":               "interface" not in out["display saved-configuration"],
    "运行配置仅剩缺省项":           "sysname" not in out["display current-configuration"],
}
for k, v in verdict.items():
    print(("PASS  " if v else "FAIL  ") + k)

判据可以按需要加严,例如检查 flash 里是否残留自定义文件名、比对 display version 的软件版本是否满足上线要求。判定项越多,移交给下一环节时越放心。

恢复出厂配置后的必要配置

设备恢复出厂后,通过 Console 口登录进行基本配置:

<HUAWEI> system-view
[HUAWEI] interface MEth 0/0/1
[HUAWEI-MEth0/0/1] ip address 10.1.1.2 24
[HUAWEI-MEth0/0/1] quit
[HUAWEI] ip route-static 0.0.0.0 0 10.10.10.10

根据网络规划配置管理网口 IP 和默认路由,再开启 CLI 登录(如 STelnet),并补齐用户名、权限与 SSH 参数:

[HUAWEI] stelnet server enable
[HUAWEI] rsa local-key-pair create
[HUAWEI] aaa
[HUAWEI-aaa] local-user admin password irreversible-cipher Admin@12345
[HUAWEI-aaa] local-user admin privilege level 15
[HUAWEI-aaa] local-user admin service-type ssh
[HUAWEI-aaa] quit
[HUAWEI] user-interface vty 0 4
[HUAWEI-ui-vty0-4] authentication-mode aaa
[HUAWEI-ui-vty0-4] protocol inbound ssh
[HUAWEI-ui-vty0-4] quit
[HUAWEI] save

若希望恢复原配置,可通过已备份的配置文件进行恢复:先把备份文件上传到 flash,再用 startup saved-configuration 指定为启动文件,最后 reboot。

失败与异常场景排查

  • 重启时误选 Y:当前运行配置被写回启动文件,恢复等于没做,需重新执行方法二或方法三。
  • 长按 PNP 键无反应:该型号不支持 PNP 键,或设备未正常启动;改用 Console 登录后执行命令。
  • reset factory-configuration 提示无法识别:当前版本不支持一键式恢复,改用方法三清除配置文件。
  • 恢复后远程登录不上:出厂配置下管理网口没有 IP、没有用户名,必须先用 Console 完成基本配置。
  • 恢复后仍能 ping 通或配置还在:确认设备是否真的重启过,以及保存提示处是否回答了 N。

相关文章:Huawei S5700 恢复出厂与密码恢复(BootROM)、H3C 交换机恢复出厂设置(CLI 与 BootWare)、华为静态路由配置详解。

原文链接:http://www.ict18.com/812.html