A while ago, we described how to reset root user password in Unix (FreeBSD). Today, we will see how to reset or recover root user password in Linux. Trust me, resetting root user password on Linux is very easy. Even an intermediate Linux user can easily reset the root user password by following the methods described below. Please note that you can reset the password only if you have physical access to the system. For remote systems and VPS, you might need your service provider's help.
I tested this guide on the following three distributions:
- Arch Linux,
- CentOS,
- Ubuntu.
However the steps are almost same for other distributions as well.
First, we will start from Arch Linux.
Table of Contents
1. Reset root user password in Arch Linux
This method was tested in Arch Linux, however It may work on other Arch based Linux distros such as Antergos, Manjaro Linux etc..
At the GRUB boot menu, select the Arch Linux entry by using the arrow keys.
Press e to edit the boot menu entry. Find the line that starts with word linux:
linux /boot/vmlinuz-linux root-UUID=d474f2-e6a2-4cc3-9899-aa98af13 rw quiet
At the end of the above line, add the following line:
init=/bin/bash
Refer the following screenshot.
After appending the above line, press CTRL-X
or F10
to continue booting into the single user mode. This change will be only temporary and will not be saved in your boot menu.ist
file.
Now, you reached the single user mode.
Type the following command to mount your root (/) file system in read/write mode.
mount -n -o remount,rw /
Then, change your root user password using command:
passwd
Enter the new password twice.
Finally, type the following command to apply the changes and start your Arch Linux in to normal mode.
exec /sbin/init
Voila! You can now log in to your Arch Linux server using the new root user password.
2. Reset root user password in CentOS 8.x
To recover or reset Root user password in CentOS 8 and RHEL server editions, refer the following guide.
3. Reset root user password in CentOS 7.x
We can do it in two methods. Both are slightly different, but easy. This method will also work on RHEL 7.x, Scientific Linux 7.x.
Method 1:
Power on your CentOS 7 server. At the boot menu, select the Kernel you want to boot up and press e to edit the selected boot entry.
Find the line rhgb quiet :
...and replace it with init=/bin/bash
Then press CTRL+X
to enter into single user mode.
Type the following command to mount root (/) file system in to read/write mode.
mount -o remount,rw /
Now, change the root user password with command:
passwd root
Enter the new password twice.
Finally, run the following command to update SELinux:
touch /.autorelabel
Then, type the following command to apply changes and restart CentOS 7:
exec /sbin/init
Now, you'll be able to log in to CentOS 7 as root user with new password.
Method 2:
At the GRUB boot menu, select the boot entry and press e to edit the selected item.
Find the line ro :
Change the ro line to rw and add an extra line init=/sysroot/bin/sh
Press CTRL+X to enter in to single user mode.
Then type:
chroot /sysroot/
Then change the root user password with command:
passwd root
Update SELinux with command:
touch /.autorelabel
Reboot your system.
exit
reboot
Now, you can log in to the system with new password.
4. Reset root user password in CentOS 6.x
If you want to reset root user password for CentOS 6.x systems and older versions, then follow these steps. This method will also work on RHEL 6.x, Scientific Linux 6.x.
At the boot menu, select the Kernel you want to boot up and press e to edit the selected item.
Select second line (Starts with the word Kernel) and press e to edit that line.
Go to the end of the line and type S to start in to the single user mode.
Then, press ENTER key and press b key to boot into to single user mode.
Mount the root (/) file system in read/write mode:
mount -o remount,rw /
Change the root password with command:
passwd root
Finally restart your CentOS 6 server.
sync
reboot
You can now able to login with new password.
5. Reset sudo user password in Ubuntu
I tested this how-to on Ubuntu 15.10 server, however it might work on other Ubuntu versions like Ubuntu 15.04/14.10/14.04 etc.
You can do it in two methods.
Method 1:
At the Grub boot loader menu, select the Kernel you use and press e to edit the entry.
Find the line that starts with word linux
. Add the following line at the end.
init=/bin/bash
Then, press CTRL-X
or F10
to boot in to single user mode.
Type the following command to mount root (/) file system in to read/write mode.
mount -o remount,rw /
Then, change the password of your administrative account using command:
passwd sk
Here sk is my administrative account.
Type the new password twice.
Finally, type the following command to start Ubuntu with updated password.
exec /sbin/init
Now, you'll be able to log in with new password.
This method is so easy. Still curious to know another way to reset the password? read on.
Method 2:
At the GRUB boot leader menu, select Advanced options for Ubuntu entry.
Select the second option i.e recovery mode.
You'll see the list of recovery options. Select root Drop to root shell prompt option.
Now, you'll be reached to the single user mode.
Type the following command to mount root (/) file system in read/write mode.
mount -o remount,rw /
Then change the password of the administrative user, for example sk, using command:
passwd sk
Enter the new password twice.
Then, type exit to return back to the recovery menu. Select Resume to start your Ubuntu system.
You might see the following message that says: You are now going to exit the recovery mode. Press ENTER to continue.
That's it. You can now able to login with new password.
All of the above methods should work on both Desktop and server editions.
Download - Free eBook: "Amazon S3 Tutorial"
What if I don't know the user name?
I don't want to reset the root user password, but the other normal user's password. How can I do it? Or You might ask I am going to reset the password of my client system, and we both don't know the username. Is it possible to reset the password of a specific user? Yes, you can.
After entering into the single user mode, you can view the list of existing users with command:
$ cat /etc/passwd
Sample output:
root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin systemd-timesync:x:100:103:systemd Time Synchronization,,,:/run/systemd:/bin/false systemd-network:x:101:104:systemd Network Management,,,:/run/systemd/netif:/bin/false systemd-resolve:x:102:105:systemd Resolver,,,:/run/systemd/resolve:/bin/false systemd-bus-proxy:x:103:106:systemd Bus Proxy,,,:/run/systemd:/bin/false syslog:x:104:109::/home/syslog:/bin/false messagebus:x:105:110::/var/run/dbus:/bin/false uuidd:x:106:111::/run/uuidd:/bin/false sshd:x:107:65534::/var/run/sshd:/usr/sbin/nologin sk:x:1000:1000:sk,,,:/home/sk:/bin/bash
Or, you can only list the usernames with command:
awk -F':' '{ print $1}' /etc/passwd
Sample output:
root daemon bin sys sync games man lp mail news uucp proxy www-data backup list irc gnats nobody systemd-timesync systemd-network systemd-resolve systemd-bus-proxy syslog messagebus uuidd sshd sk
The above commands are same for many Linux distributions.
Conclusion
In this tutorial, we have learned how to reset the root user password, and also the normal user's password. Do not blindly trust that you're safe after setting up a strong password for your root user account and other system accounts. As you can see in this tutorial, the passwords can be easily reset within few minutes, no matter if it's either root or normal user. We must be more careful and encrypt our data, partition and of course we should setup a strong password for GRUB boot loader in order to protect our system. Refer the following guide to know how to setup a password to the Grub boot loader.
1 comment
Thank you, i wish i had seen this article earlier
But anyway saved my life