温馨提示×

debian如何替换root密码

小樊
46
2026-09-01 23:38:43
栏目: 智能运维

在 Debian 系统中,替换(修改)root 密码有几种常见方式,取决于你当前是否能登录、是否知道原密码。


一、已知当前 root 密码(或可使用 sudo)

方法 1:直接修改 root 密码

su -

输入当前 root 密码后执行:

passwd

按提示输入新密码两次即可。

方法 2:使用 sudo(普通用户有 sudo 权限)

sudo passwd root

输入当前用户密码,再设置新的 root 密码。


二、忘记 root 密码(无法登录)

方法 1:通过单用户模式(推荐)

  1. 重启 Debian
  2. 在 GRUB 启动菜单中选择 Debian
  3. e 编辑启动项
  4. 找到以 linux 开头的行,例如:
    linux /boot/vmlinuz-... root=... ro quiet
    
  5. ro 改为 rw,并在末尾添加:
    init=/bin/bash
    
  6. Ctrl + X 启动

进入 shell 后执行:

passwd root

设置新密码

然后重启:

exec /sbin/init

或:

reboot -f

方法 2:使用 Live CD / 救援模式

  1. 使用 Debian Live USB 启动
  2. 挂载原系统根分区:
    mount /dev/sdXn /mnt
    
  3. chroot 进入:
    chroot /mnt
    
  4. 修改密码:
    passwd root
    
  5. 退出并重启

三、注意事项

  • Debian 默认 允许 root 登录(服务器版)
  • 桌面版可能默认 禁用 root 密码登录,可使用:
    sudo passwd root
    
  • 修改后建议测试一次 root 登录

如果你愿意,可以告诉我:

  • 物理机 / 虚拟机 / VPS
  • 是否能进入 GRUB
  • Debian 版本(如 11 / 12)

我可以给你更精确的步骤。

0