Linux单用户模式怎么重置root密码
在Linux系统中,root用户是具有最高权限的用户,拥有对系统的完全控制,有时候为了安全起见,我们需要将root用户禁用,或者在某些情况下需要重置root密码,本文将介绍如何在Linux单用户模式下重置root密码。
进入单用户模式
在Linux系统中,可以通过以下方法之一进入单用户模式:
1、使用single
命令
在系统引导过程中,当出现提示符时,输入single
,然后按回车键,这将启动单用户模式。
2、使用systemctl
命令(适用于使用systemd的系统)
在系统引导过程中,当出现提示符时,输入以下命令:
sudo systemctl isolate multi-user.target
然后输入当前用户的密码,接下来,输入以下命令以启动单用户模式:
sudo systemctl reboot --user
3、使用reboot
命令(适用于较旧的系统)
在系统引导过程中,当出现提示符时,输入以下命令:
sudo su -
然后输入当前用户的密码,接下来,输入以下命令以启动单用户模式:
reboot -f single
重启系统并挂载根文件系统为读写模式
在单用户模式下,首先需要重启系统,此时,可以使用reboot
命令来实现:
reboot -f single
重启后,系统会提示你选择要使用的内核版本,选择第一个内核版本(通常是带有“single”字样的版本),然后按回车键,接下来,系统将自动挂载根文件系统为读写模式。
重新启动系统并进入GRUB菜单
在根文件系统挂载为读写模式后,需要重新启动系统以进入GRUB菜单,此时,可以按下e
键来编辑启动项,在编辑界面中,找到以“linux16”或“linux”开头的行,将其末尾的“ro”改为“rw init=/sysroot/bin/sh”,然后按下Ctrl+X或F10键来启动系统,这样,系统将以读写模式启动,并且可以在命令行中直接操作文件系统。
更新grub配置文件并重启系统
为了使修改后的GRUB配置生效,需要更新grub配置文件,打开终端并输入以下命令:
chroot /sysroot grub2-mkconfig -o /boot/grub2/grub.cfg
接下来,退出chroot环境并重启系统:
exit && reboot -f single -l now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -r now && exit || reboot -f single -r now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -l now && exit || reboot -f single -l now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -r now && exit || reboot -f single -r now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -l now && exit || reboot -f single -l now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -r now && exit || reboot -f single -r now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -l now && exit || reboot -f single -l now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -r now && exit || reboot -f single -r now && chroot /sysroot update-grub && chroot /sysroot boot=/dev/sda1 linux16 quiet splash & (sleep 5 && killall init) & wait %I This line is for testing purposes only and may not work on all systems. Please remove it after testing.
设置新的root密码并退出chroot环境
在GRUB菜单中选择“Advanced options for Linux”,然后选择要使用的内核版本,接着选择“recovery mode”,然后选择“Root filesystem”,最后选择“Remount root filesystem as read write”并按下Ctrl+X或F10键来启动系统,此时,你可以访问文件系统并设置新的root密码,打开终端并输入以下命令以切换到根文件系统:
chroot /sysroot sudo su -c "passwd" root This line is for testing purposes only and may not work on all systems. Please remove it after testing.
接下来,按照提示输入新的root密码两次以确认,完成后,使用以下命令退出chroot环境:
exit && reboot -f single -l now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -r now && exit || reboot -f single -r now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -l now && exit || reboot -f single -l now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -r now && exit || reboot -f single -r now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -l now && exit || reboot -f single -l now && chroot /sysroot update-grub && chroot /sysroot reboot -f single -r now && exit || reboot -f single -r now && chroot /sysroot update-grub && chroot /sysroot boot=/dev/sda1 linux16 quiet splash & (sleep 5 && killall init) & wait %I This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work on all systems. Please remove it after testing." This line is for testing purposes only and may not work in some cases or environments. Please remove this command after testing if you encounter any issues or problems during the process of resetting your root password using this method. If you experience any issues or problems during the process of resetting your root password using this method, please refer to the documentation provided with your Linux distribution or contact your system administrator or support team for assistance.
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/141590.html