How to Reset the Root Password on Debian 9 Stretch
In this article, I will show you how to configure sudo, set root password, disable root login and reset root password. This can be done even if you don’t have sudo enabled and you don’t remember the password. Let’s get started.
Installing and Configuring sudo
If you have set up a root password while installing Debian 9 Stretch on your machine, then there’s a chance sudo won’t be configured. So you won’t be able to run any commands as root user if you’re logged in as an ordinary user.
In this section, I am going to show you how to install and configure sudo to run commands as root user without login in as user root.
First log in as root with the following command:
Now type in your root password and press <Enter>.
You should be logged in as root user.
Now update the apt package repository cache with the following command:
The apt package repository cache should be updated.
Now install sudo with the following command:
It should be installed. In my case it is already installed.
Now to add your ordinary user, let’s say shovon to the sudoers file, run the following command:
NOTE: Here shovon is an ordinary user account on my Debian 9 Stretch system.
Now exit out of the root user with the following command:
Now the ordinary user shovon can run any command as root using sudo as follows:
Enter the login password of user shovon and press <Enter>.
The command should run as root as you can see from the screenshot below.
Setting Root Password
If you haven’t set a root password while installing Debian 9 Stretch, then root password by default won’t be set. But sudo should be configured for your ordinary user.
To set a root password then, run the following command:
Now type in the password for your logged in user and press <Enter> to continue.
Now type in your desired root password and press <Enter>.
Now retype the root password and press <Enter>.
The root password should be set. Now you can login as root using your root password.
Changing Root Password
If you want to change the root password, all you have to do is run the following command again:
Now do the same things as you did on the Setting Root Password section of this article.
Disabling Root Login
If you don’t want to allow root login, then you can lock the root account with the following command:
To allow root login again, just set a root password again. For more information, take a look at the Setting Root Password section of this article.
Recovering Lost Root Password
If you happen to forget the root password and you don’t have sudo configured, then don’t give up just yet and think about reinstalling the operating system again. There is a way to reset the root password.
First reboot your computer. You should see the GRUB menu when your computer boots as shown in the screenshot below. Select Debian GNU/Linux and press e.
You should see the following window. Now go to the end of the line as marked in the screenshot below.
Now remove ‘ro quiet’ part from the end of the line and append ‘init=/bin/bash’ to the end of the line as marked in the screenshot below. Once you’re done, press F10.
You should see the following window.
Now run the following command to remount the root (/) partition as readable and writable:
Now run the following command to remount the /proc filesystem:
Now to reset the root password, run the following command:
Now type in your new root password and press <Enter> to continue.
Now re-type your new root password and press <Enter> to continue.
You root password should be changed.
Now press <Ctrl> + <Alt> + <Delete> and your Debian 9 Stretch machine should reboot.
Now you can login as root with your new root password as you can see in the screenshot below.
Before Login:
After Login:
This is how you reset your root password on Debian 9 Stretch. Thanks for reading this article.