Change the Root Password in Linux

To change the root password in Linux, you’ll need to run the “passwd” command from a root-level command prompt. If you know the current root password, you can use “su” to access the root account and run “passwd” right from a terminal window. If you’ve forgotten the root password, however, you’ll need to boot into single-user mode first, in order to get permission to run “passwd.” Learn how to change the Linux root password from the command line when booted normally or in single-user mode.

Steps

With the Current Root Password

  1. Press Ctrl+Alt+T to open a terminal window.[1] As long as you know the current root password, you can change it right from the desktop. This keyboard shortcut will open a new terminal window with a command prompt in all Linux desktop environments.
    • If you don’t have the current root password, see this method.
    • If you’re not using a desktop environment, you’re already at a command prompt, so just continue to the next step.
  2. Type su at the command prompt, then hit Enter.[2] A new line will open beneath the command prompt that says “Password:”
  3. Type the current root password, then press Enter. When the password is accepted, you’ll be brought back to the command prompt as the root user.
    • If you type the password incorrectly, just run su again and retry.
    • Passwords are case-sensitive.
  4. Type passwd, then press Enter. A new line will open beneath the command that says, “Enter new UNIX password:”
  5. Type a new password, then press Enter. The password you type will not appear on the screen, but you’ll be prompted to type it a second time to confirm.
  6. Retype the new password, then press Enter. You will see a message that says “password updated successfully.”
  7. Type exit and then press Enter. This will log you out of the root account.

Without the Current Root Password

  1. Start the computer and press the E key at the Grub menu.[3] This method will work for most popular Linux distributions (Ubuntu, CentOS 7, Debian).If you’ve lost the root password, you can reset it in single-user mode. The Grub menu appears right after you turn on the computer. In most cases, it only stays on the screen for a few moments.
    • If you don’t press E before the Grub menu disappears, reboot and try again.
    • There are many distributions of Linux, some more obscure than others. If you’re not able to get to single-user mode with this method, check your distribution’s website for instructions specific to your system.
  2. Use the keys to scroll to the line that begins with “linux /boot”. This is the line you’ll need to modify in order to boot into single-user mode.
    • In CentOS and some other distributions, the line may begin with “linux16” instead of just “linux.”
  3. Use the keys to move the cursor to the end of the line. The line ends with ro, so place the cursor right after that.
  4. Type init=/bin/bash at the end of the line. The end of the line should now look like this: ro init=/bin/bash.
    • Note the space between ro and init=/bin/bash.
  5. Press Ctrl+X to boot into single-user mode. This will tell the system to boot directly to a root-level command prompt.
  6. Type mount –o remount,rw / at the prompt, then press Enter. This mounts the file system in read-write mode.
  7. Type passwd at the prompt, then press Enter. Since booting into single-user mode gives you root access, there’s no need to pass additional parameters to the “passwd” command.
  8. Type a new password for the root user, then press Enter. Note that you won’t see the characters you type on the screen. This is normal.
  9. Type the new password again when prompted, then press Enter. When the system confirms you’ve re-entered the same password, you’ll see a message that says “password updated successfully.”
  10. Type reboot –f and then press Enter. This command reboots the system normally.

Tips

  • Make sure your passwords are 8 characters or more and contain a mix of letters (upper and lowercase), numbers and symbols.
  • To change the password for another user, "su" to root and type "passwd <username>".

Related Articles

Sources and Citations