1. Blog
  2. Manuals

How to Reset the Root Password on Linux: Ubuntu, Debian, AlmaLinux

How to Reset the Root Password on Linux: Ubuntu, Debian, AlmaLinux

A forgotten root password does not mean a lost server. If you can reach the console — physical, or VNC in your hosting control panel — you can set a new password in a few minutes. The method depends on the distribution and on what access you still have.

Check the easy options first

You have a user with sudo rights. No reboot needed:

sudo passwd root

You log in with an SSH key. If key-based root login works, run passwd — it will not ask for the old password.

Hosting control panel. Many providers offer a password reset right in the server control panel. Check this before rebooting.

If none of these apply, you need the server console. On a VPS that is the VNC console in the control panel: SSH will not do, because you have to interrupt the boot process.

Ubuntu and Debian: reset through GRUB

  1. Reboot the server and wait for the GRUB menu. If it does not appear, hold Shift right after boot starts (BIOS systems) or tap Esc a few times (UEFI systems).
  2. Select the main entry and press e to edit the boot parameters.
  3. Find the line that starts with linux. At its end, replace ro with rw and add init=/bin/bash:
linux /boot/vmlinuz-... root=UUID=... rw init=/bin/bash
  1. Press Ctrl+X or F10. The system boots straight into a root shell without asking for a password.
  2. Set a new password:
passwd
  1. Flush the changes to disk and reboot:
sync
reboot -f

The edited boot parameters apply once: on the next reboot the system starts normally.

AlmaLinux, Rocky Linux, RHEL: rd.break

On these systems you also have to deal with SELinux.

  1. Reboot the server, select the main entry in the GRUB menu and press e.
  2. Find the line starting with linux and append rd.break.
  3. Press Ctrl+X. Boot stops early at the switch_root:/# prompt.
  4. Remount the system partition read-write and switch into it:
mount -o remount,rw /sysroot
chroot /sysroot
  1. Set a new password:
passwd
  1. Create the file that tells SELinux to relabel the filesystem on boot. Without this step you will not be able to log in:
touch /.autorelabel
  1. Leave the shell and continue booting:
exit
exit

Relabelling takes from a couple of minutes to half an hour depending on disk size. The server reboots by itself — do not interrupt it.

If rd.break asks for the root password, use init=/bin/bash instead, as in the Ubuntu instructions, and also run touch /.autorelabel after changing the password.

Works on any distribution: rescue mode

If the boot loader is password-protected or you cannot edit its parameters, boot the server from a live image or in the rescue mode many providers offer in their control panel.

  1. Identify the root partition of the installed system:
lsblk -f
  1. Mount it and switch into the installed system:
mount /dev/sda2 /mnt
chroot /mnt
  1. Change the password and leave:
passwd
exit
umount /mnt

On systems with SELinux, run touch /.autorelabel before exiting. Then boot the server from its main disk.

If the disk is encrypted (LUKS), open it first with cryptsetup open — you will need the encryption passphrase. Without it the data cannot be recovered.

After you are back in

  • Confirm the login works before closing the console.
  • Set up SSH key login so the root password is only needed at the console; add two-factor SSH authentication for critical servers.
  • Create a separate sudo user. Next time you can change the root password without rebooting.
  • Store the password in a password manager — for example your own Vaultwarden.

Why this is possible at all

Anyone with console access to a server can get root — that is a property of every system, not a Linux vulnerability. Protect access to your hosting control panel at least as well as the server itself: with a strong password and two-factor authentication.