fbpx Forgot Root Password on Linux? How to Recover (Step by Step) | Certificação Linux

Forgot Root Password?

📅 01/06/2020 ⏱ 2 min ✍️ Uira Ribeiro
Linuxcommandshow-to
Forgot Root Password?
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

Losing the root password can be a problem. However, there are two ways to recover the password:

The first one is to use a user account that belongs to the "sudoers" group, and can change the root password using the sudo command:

$ sudo su -
# passwd

If it is not possible to log into the system with a privileged account, it is still possible to restart the computer and enter emergency mode to change the root password.

On Debian/Ubuntu systems, the step-by-step process is:

  1. Restart the computer;
  2. Enter the Grub Menu;
  3. Choose the Menu line that will be used for boot;
  4. Press the "E" key to edit the line;
  5. Find in the editor the line that starts with "linux" or "linux16";
  6. At the end of this line, add a space, and append the number 1;
  7. Press Ctrl-X to restart the system;
  8. Use the passwd command to change the root password;
  9. Restart the system normally;

On systems like CentOS and Fedora, especially those that use SELinux, it is a bit different:

  1. Restart the computer;
  2. Enter the Grub Menu;
  3. Choose the Menu line that will be used for boot;
  4. Press the "E" key to edit the line;
  5. Find in the editor the line that starts with "linux" or "linux16";
  6. Look for "ro" (readonly) on this line. Delete "ro" and type instead: "rw init=/sysroot/bin/sh"
  7. Press Ctrl-X to restart the system;
  8. Type "chroot /sysroot";
  9. Use the passwd command to change the root password;
  10. If the system uses SELinux, force it to relabel the root partition with the command "touch /.autorelabel"
  11. Type "exit";
  12. Restart the system normally with the command "reboot";
Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp