Answered by the Webhosting Experts

How to Change a Password in Various Linux Distribution

 

In the diverse ecosystem of Linux, managing security credentials is a fundamental skill for both casual users and system administrators. Whether you are updating a forgotten login, rotating keys for security compliance, or performing emergency recovery on a locked system, knowing the right commands and environments is essential. This guide provides a comprehensive walkthrough on how to navigate password changes across various Linux distributions, from standard terminal commands for users and root, to advanced recovery techniques using the GRUB bootloader and rescue environment when the standard login screen is no longer an option.

Changing the Current User Password

The most frequent password task you’ll encounter is updating your own credentials. In Linux, this is handled by a dedicated utility that manages the encryption and storage of your secret key within the system’s shadow password suite. Because this process is self-contained, it is the safest and most common way to maintain account security or rotate passwords according to a regular schedule.

Method 1 – Changing User Password as the User

The first method is whether you’re logged into the user you wish the change the password for. If that is the case, proceed to send the command passwd and type the current password followed by the new password twice.

Changing User Password Logged in as That User
Changing User Password Logged in as That User

Method 2 – Changing User Password as Root User

The second method involves changing the user’s password while being logged in as the root user. This will allow you to change the password without knowing the current user password. The command to use is passwd username, ensuring to change the “username” to the user you wish to change the password for.

Changing User Password as the Root User

Changing the Root Password

The root account, or superuser, holds total control over the operating system, making its password the most critical piece of security on your machine. Depending on your distribution’s philosophy, whether it’s a sudo-heavy system like Ubuntu or a traditional root-centric system like Debian, the method for changing this password varies. Understanding how to update the root credential ensures you never lose administrative access to core system configurations.

To change the root password logged in as root, you will require to run the command passwd and enter the new password twice.

Changing the Root Password
Changing the Root Password

Resetting a Password via GRUB (The Bootloader)

When you are completely locked out of your system and cannot reach a terminal, you must go to the bootloader level. GRUB (Grand Unified Bootloader) allows you to intercept the boot process before the operating system even starts. By temporarily modifying the kernel’s boot parameters, you can force the system to give you a root shell, bypassing the standard login screen entirely to fix a forgotten password.

The video within the section below is a good general flow for most of the distributions. Specific commands will be in each relevant section to be used as reference. 

If you’ve lost access to root, you can utilize the Virtual Console/IPMI to gain access as per the articles below.

How to Access Your Hivelocity Server’s IPMI

How to Use the Virtual Console with Your VPS

AlmaLinux Password Reset

  1. Reboot the machine while pressing SHIFT key in short intervals which will take you to the GRUB menu.
  2. Select your latest kernel and press “e” to edit the entry
  3. Find the line starting with “Linux” and place rd.break enforcing=0 at the end of it.
  4. Press Ctrl+X to reboot and let the modifications apply
  5. Access /sysroot directory by mounting /sysroot with read and write permissions using mount -o rw,remount /sysroot
  6. Change the directory environment to /sysroot using chroot /sysroot
  7. Change root password using passwd root
  8. [If applicable] Set the appropriate SELinux context. The command creates a hidden file called .autorelabel in the root directory. During the reboot, SELinux detects this file and relabels all the files on the system with appropriate SELinux contexts. This process takes quite some time in systems with huge disk space. The command to run is touch  /.autorelabel
  9. Run the command exit twice and it will reboot the system

Debian / Proxmox Password Reset

  1. Boot into GRUB.
  2. Edit the kernel (e key)
  3. Find the line “linux /boot/vmlinuz” and at the end of it add init=/bin/bash
  4. Activate the changes using ctrl+X
  5. Remount root as read/write using mount -rw -o remount /
  6. Change root password and reboot using passwd root
  7. Reboot using the reboot command

CentOS 5/6 Password Reset

  1. Reboot the machine.
  2. Press any key/SHIFT in short intervals to enter the boot menu. 
  3. From the GRUB menu, select kernel the relevant kernel and press the ‘e’ key.
  4. Find the /vmlinuz-… line at the end of this line one of the following (choose one): 1, s, or single. Press Enter to save your changes (using a space after that line then adding “s”).
  5. You should be back on the kernel /vmlinuz… line. Press the ‘b’ key to boot to these temporary options to allow you to recover your root account.
  6. Now you will boot up to the root shell. Enter the command passwd to set your new password. If you are unable to save the file be sure to run the following mount -o remount /
  7. Once you have completed this, run the reboot command.

CentOS 7 / CloudLinux Password Reset

  1. Boot the machine and press shift to enter GRUB.
  2. Select your kernel and press e to edit.
  3. Go to the line of Linux and change ro with rw init=/sysroot/bin/sh
  4. Now press Control+x to start on single user mode.
  5. Now access the system with this command chroot /sysroot
  6. Reset the password passwd root
  7. [If applicable] Update SELinux information touch /.autorelabel
  8. Exit chroot using exit command
  9. Reboot system using reboot command

Ubuntu Password Reset

  1. Reboot the computer and press Shift/Escape keys in short interval to boot into GRUB.
  2. Highlight your kernel and press e to edit.
  3. Find the line starting with “linux”, delete ro and replace with rw init=/bin/bash at the end of that line.
  4. Press Ctrl + X to boot.
  5. Change password using passwd root
  6. Reboot system using exec /sbin/init

Plesk Password Reset

  1. Login as root.
  2. Run command plesk login
  3. Run command plesk bin admin --set-admin-password -passwd 'NEWPASSWORDHERE'

FreeBSD Password Reset

  1. During boot, select option 2 “Boot Single User”.
  2. Press Enter when prompted with “Enter full pathname of shell or RETURN for /bin/sh:” message in the shell.
  3. Mount root in rw using command mount -u / and mount all using command mount -a
  4. Reset the password using command passwd and proceed to reboot using command reboot

Red Hat Enterprise Linux / Oracle Linux Password Reset

  1. Press ‘e’ at the boot menu to change kernel parameters.
  2. Search for the line containing “rhgb quiet”.
  3. Replace ro to rw init=/sysroot/bin/sh
  4. Press Ctrl-X to continue the system startup.
  5. Now access the system with this command chroot /sysroot
  6. Reset the password passwd root
  7. [If applicable] Update SELinux information touch /.autorelabel
  8. Exit chroot using exit command
  9. Reboot system using reboot command

Resetting a Password via Rescue or Recovery Environment

For those who prefer a more guided approach than manual GRUB editing, many Linux distributions offer a dedicated Recovery Mode. This environment acts as a maintenance toolkit, booting the system into a minimal state with a simplified menu. Using a rescue environment is often the most reliable way to reset passwords because it automates the mounting of file systems and provides a direct path to a root prompt in a controlled, safe-mode setting. 

  1. You will require downloading and mounting a recovery ISO such as system-rescue to your dedicated hardware or VPS. Ensure to view links to both relevant articles above for reference.
  2. Once the ISO is mounted, boot into the virtual drive and press on the first option in the menu as below.

    SystemRescue
    SystemRescue

  3. The rescue environment will boot and you will be presented with the following screen as below.

    SystemRescue Shell
    SystemRescue Shell

  • Run the following commands in the order as listed below:
    1. lsblk to display current drives and partitions
    2. mkdir /mydrive to create a temporary directory to mount the partition with the root/OS files
    3. mount /dev/sda2 /mydrive to mount the partition to the newly created /mydrive directory
    4. lsblk to review and confirm the work
    5. chroot /mydrive to change our shell into the system that is in that partition. If you encounter an error proceed unless you’re hitting a hard wall. At that point reach out to the Hivelocity Support team for further assistance
    6. passwd root to change the root user password
    7. Unmount the ISO and reboot the unit using the reboot command

      Performing the root Password Reset
      Performing the root Password Reset

Need More Personalized Help?

If you have any further issues, questions, or would like some assistance checking on this or anything else, please reach out to us from your my.hivelocity.net account and provide your server credentials within the encrypted field for the best possible security and support.

If you are unable to reach your my.hivelocity.net account or if you are on the go, please reach out from your valid my.hivelocity.net account email to us here at: support@hivelocity.net. We are also available to you through our phone and live chat system 24/7/365.