Admins often get requests to reset or change account passwords. This activity usually follows a security incident where the admins are required to allocate new passwords for all user accounts.
In rare cases, admins might also need to reset passwords for their accounts. This usually happens in the case of lost passwords or when the admin needs to rotate passwords due to security reasons.
Regardless of the reason, changing the root password in a Linux system is critical to maintaining security and access control.
In this tutorial, we will go into the details of changing the root password in Linux. We’ll also discuss how to set passwords for non-root accounts.
Note that the processes we’ll discuss are applicable to most mainstream Linux distributions.
Let’s get the prerequisites out of the way.
You need a system running a Linux distribution (Ubuntu or CentOS), an admin account, and access to the Linux operating system with command-line interface access.
Table of Contents
- How to Reset a Root User Password in Linux
- How to Change Other Users’ Passwords in Linux
- How to Reset a Root Password in Single User Mode on Ubuntu
- How to Reset the Lost Password for a Linux User
How to Reset a Root User Password in Linux
Let’s start with the toughest challenge – resetting a root user account password in Linux.
Start by accessing the server using SSH, either through a client like PuTTY or directly through the terminal.
Once you’re connected, enter the following command to change the password for the root account:
# passwd root
If you’re not logged in as the root user, you will be asked to enter the current root password. As such, it’s critical that you are logged in as the root user when resetting the password.
The command will ask for the new password twice.You will see a success message when the command changes the root password.
How to Change Other Users’ Passwords in Linux
You can change the password of non-root accounts through a similar process based on the bash passwd command.
Log in as a root user and launch the terminal.
Enter the passwd command and the account to start the process. The syntax of this command is:
For instance, use the following command to change the password for the user account user1:
The command will request the new password and then the confirmation.
You will see a success message at the end of the command execution.
How to Reset a Root Password in Single User Mode on Ubuntu
As an administrator, there are times when you log into an account and find that you have forgotten the password.
In Ubuntu’s single-user mode, changing the root password is a simple operation. Before proceeding, make sure you’re logged in with an account that has administrative privileges.
We’ll use Ubuntu 22.04 for the demonstration. However, the process should work without problems with other versions and distros.
Start by rebooting the system into Single-user mode. In the GRUB menu, choose the desired boot item with the arrow keys. Underneath, you can see that the e key allows you to change the boot parameters.
Start editing the boot parameters. Locate the line starting with linux or linux16 and add single at the end. Next, locate ro quiet splash $vt_handoff
and replace it with rw init=/bin/bash.
Press Ctrl + X to boot with the modified boot parameters.
When the system boots up, you will be in the Single User mode console with root privileges. The prompt will indicate that you are in a root shell. The file system is mounted in READ ONLY mode.
So, the next step is to remount the filesystem with WRITE permissions. For this, run the command:
# mount -o remount,rw /Now, use the passwd command to change the root password. We suggest choosing a strong password. Now, reboot the system with the reboot command.
Once the system has rebooted, log in using the new root password.
If you are on a CentOS system, you can follow these steps to reset the root password in the Single User mode.
How to Reset the Lost Password for a Linux User
Let’s consider the scenario where you log in with a username and try to change the password. The simplest way to do it is to log in as root or a sudo user and change the user’s password with the passwd command.
Now, if the system uses LDAP authentication, the command could need the user’s lost password.
To address this issue, you can replicate the encrypted password of a user that’s usually stored in the /etc/shadow file.
We assume that you are logged in as the administrator.
Start the process by opening the /etc/shadow file in your preferred file editor. We’ll open it in VIM with the following command:
# vi /etc/shadow
Locate the user and find the encrypted password. This will be the sequence of characters following the user’s name, positioned between two colons (:).
Next, identify another user whose password you are aware of. Copy the encrypted password string of this second account and replace the password string of the target user with it.
Save the file and exit.
Now, in the terminal, go through the process of resetting the password and use the password of the known account (that you replaced in the /etc/shadow file).
Resetting passwords and changing the root password are common activities admins carry out fairly regularly.
In addition, security experts suggest rotating the passwords, especially for administrative accounts, to minimize the threats associated with compromised passwords.
Changing passwords is often a straightforward task where admins use a simple command to change the password for an account. The good thing here is that the process is similar for all distros, and the commands work the same.
When changing passwords, we strongly urge applying the best password practices that strengthen the security of the accounts on the system.
Security is a central consideration for the RedSwitches teams. Our engineers and support teams dedicate significant efforts to building and delivering bare metal server infrastructure that our clients can use without worrying about security issues interfering with their business operations.
We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Q. How can I change the root password in Linux?
You can use the following command to change the root password in Linux:
sudo passwd root
After running this command, you will be prompted to enter the current password for the root account. Then, you can enter and confirm a new password for the root user. Once the password is successfully changed, you can use the new password for future logins.
Q. Can I change the root password in CentOS?
Yes, you can change the root password in CentOS by following the steps mentioned in this tutorial. The process of changing the root password in CentOS is the same as changing it in other Linux distributions.
Q. How can I change the root password in Ubuntu?
To change the root password in Ubuntu, use the following command:
sudo passwd root
After running this command, you will be prompted to enter the current password for the root account. If you have never set a root password before, you can leave it blank. Then, you can enter and confirm a new password for the root user. Once the password is successfully changed, you can use the new password for future logins.
Q. Can I reset the root password as a non-root user?
No, you cannot reset the root password as a non-root user. Only the root user or a user with sudo privileges can reset the root password in Linux.
Q. What is the GRUB menu, and how can I access it?
The GRUB (Grand Unified Bootloader) menu is a boot menu that appears when you start your computer. It allows you to choose which operating system or kernel to boot into.
To access the GRUB menu, you need to reboot your system and press the appropriate key (usually Esc or Shift) while the system starts. The key may vary depending on your system, so it’s best to refer to your system’s documentation or search online for the specific key to access the GRUB menu.
Q. What is rescue mode, and how can it help reset the root password?
Rescue mode is a special mode that allows you to repair or recover a Linux system when it cannot boot normally. In rescue mode, you can access a minimal environment with basic functionality, including mounting and modifying the filesystem. This can be useful for resetting the root password when you have forgotten it. By selecting the rescue mode option in the GRUB menu and following the steps mentioned earlier, you can gain access to the root shell prompt and reset the root password.
Q. What should I do if I encounter unauthorized access to my root account?
If you suspect unauthorized access to your root account, it is important to take immediate action to protect your system. Here are a few steps you can take:
1. Change the root password using the earlier steps as soon as possible.
2. Check the system logs for any suspicious activity.
3. Disable or remove any unnecessary services or user accounts.
4. Install and configure a firewall to restrict access to your system.
5. Regularly update your system with the latest security patches.
6. Consider using strong authentication methods, such as SSH keys, instead of password-based authentication.
If you believe your system has been compromised, it is also advisable to seek professional help to investigate and mitigate any potential security risks.
Q. Is resetting a lost root password possible without using the GRUB menu or rescue mode?
No, the GRUB menu or rescue mode is typically required to reset a lost root password in Linux. These methods provide the necessary access and privileges to modify the system files and reset the password. Without using these methods, resetting the root password as a regular user is impossible.
Q. Can I use password management software to reset or change the root password in Linux?
No, password management software is primarily designed to manage and securely store individual user passwords. It is not typically used for resetting or changing the root password in Linux. To reset or change the root password, follow the steps mentioned earlier, either through the GRUB menu or rescue mode.