0%

RHCSA (EX200) - 设置 root 密码

设置 root 密码

将 node2 的 root 密码设置为 flectrag。您需要获得系统访问权限才能进行此操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Send key -> Ctrl+Alt+Del
# Press 'e' to edit the selected item
# linux ($root)/... rd.break console=tty0
# Press Ctrl-x to start
switch_root:/# mount | grep sysroot
/dev/vda3 on /sysroot type xfs (ro,relatime,attr2,inode64,noquota)
...
switch_root:/# mount -o remount,rw /sysroot/
switch_root:/# mount | grep sysroot
/dev/vda3 on /sysroot type xfs (rw,relatime,attr2,inode64,noquota)
...
switch_root:/# chroot /sysroot/
sh-4.4# echo flectrag | passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.
# 确保所有未标记的文件(包括此时的 /etc/shadow)在启动过程中都会重新获得标记
sh-4.4# touch /.autorelabel
sh-4.4# exit
exit
switch_root:/# exit
logout
...

在红帽企业 Linux 8 中,可以使从 initramfs 运行的脚本在某些点暂停,提供 root shell,然后在该 shell 存在的情况下继续。这主要是为了进行测试,但也可以使用该方法来重置丢失的 root 密码。

要访问该 root shell,请按照以下步骤操作:

  1. 重新启动系统。

  2. 按任意键(Enter 除外)中断启动加载器倒计时。

  3. 将光标移至要启动的内核条目。

  4. 按 e 编辑选定的条目。

  5. 将光标移到内核命令行(以 linux 开头的行)。

  6. 附加 rd.break。利用该选项,就在系统 initramfs 向实际系统移交控制权前,系统将会中断。

  7. 按 Ctrl+x 使用这些更改进行启动。

此时,系统会显示 root shell,且磁盘上的实际根文件系统会在 /sysroot 中以只读方式挂载。由于进行故障排除经常要求修改根文件系统,因此您需要将根文件系统更改为读/写。

系统尚未启用 SELinux,因此您所创建的任何文件都没有 SELinux 上下文。有些工具(例如 passwd 命令)首先会创建一个临时文件,然后移动新文件以代替要编辑的文件,从而有效地创建不带 SELinux 上下文的新文件。因此,当您对 passwd 命令使用 rd.break 时,/etc/shadow 文件并没有获得 SELinux 上下文。