How to change password in Linux

In today’s article, we will show you how to change your password in Linux. The root account on a Linux computer is the account with the highest authority on the system. It has control over everything, you can delete and change anything, and has permission to run any program. Simply put, root can do whatever the system supports. As a general rule, casual users should never use the root account, while more experienced users need the root account more often, especially if they spend most of their time on the terminal. So, even if you don’t use the root account often, it helps to set a more secure password so that attackers cannot log into the root account and harm your system.

In this article, we will show you how to change root password on various Linux distributions. We will be going through the process of changing the root password for Ubuntu, CentOS, Debian and Fedora. All this is quite simple to do, and does not take much time. Let’s start.

How to change root password on Ubuntu

Change root password on Ubuntu 16.04 (this works on other versions as well, along with any server version of Ubuntu).

Method 1: Via terminal at boot time (you need to have another account on the machine)

Step 1: Open a terminal.

This can be done either by pressing Ctrl + Alt + T, or by locating the terminal program using the search function. If it’s a VPS or server, just SSH as usual.

Step 2: Enter the command to change the password.

This command will allow you to change your administrator password. First, you will need to enter the user account password in order to authenticate the changes. Of course, this assumes that your account is on the sudoers list, which essentially means that your account is an administrator account. If this is not the case, you will have to change your password using the second method.

sudo -i passwd

It will then ask you for a new UNIX password, and as soon as you enter it, it will ask you to repeat the password. Once this is done, you have successfully changed the superuser password. Now all you have to do is log out of your regular account and simply log in using the username “root”.

There is also a second, more advanced method that can be used if you only had a root account on your Ubuntu machine, or if any other accounts cannot use the sudo command. This method uses grub to get past the operating system altogether and allows you to change the root user’s password without having to know any passwords.

Method 2: Grub (No account required)

Step 1: Enter the grub options screen.

Reboot your computer. If Ubuntu is your only Operating System on the machine, then hold shift after the bios screen goes to the operating system selection menu. If your computer has multiple operating systems, this menu appears automatically. Press the “e” key to enter the grub options screen for ubuntu.

Step 2: Change the grub parameter.

In the edit menu, scroll down to the line starting with “Linux” and add the following lines at the end of the line:

exec /sbin/init

Now press Ctrl + X to boot with this new configuration.

Step 3: Change the root password.

Changing the superuser password is now possible. Run the “passwd” command and enter a new password – re-enter it when prompted. The root password will be changed. Just use the “reboot” command, or if that doesn’t work, use the following command to reboot the system to a normal state:

exec /sbin/init

How to change superuser password on CentOS

This paragraph is dedicated to the – the password reset procedure on CentOS 7 and differs from previous versions of CentOS, therefore we will focus only on the latest version.

Resetting the root password is not as easy on CentOS as it is on Ubuntu. There are several steps and each command must be correct, so be careful with your commands.

Step 1: Enter the grub options screen.

This is done either by rebooting your machine, or by starting it if it has been turned off. When the CentOS boot screen appears, press “e” to enter the grub options menu.

Step 2: Modifying the grub parameter.

Scroll down to the line starting with “Linux” or “linux16”. Replace the word “ro” with “rw init = / sysroot / bin / sh”. Then press Ctrl + X at the same time to boot using this configuration.

Step 3: access to the file system.

With the “chroot / sysroot” command, we can access and execute commands on the file system as if we were running on the operating system. We must do this in order to access the “passwd” command.

Step 4: Change administrator password.

Enter the “passwd root” command to change your superuser password. He will ask you to confirm the password, which you will enter again. Then proceed to the next step.

Step 5: Committing the / etc / shadow file (SELinux only)

If you have SELinux disabled, skip this step. If you don’t have SELinux disabled, enter the following command to ensure that the new / etc / shadow file that was created when you changed your password will be accepted as the new file:

touch /.autorelabel

Once done, just type “exit” followed by another “exit”. If your car freezes, just reset the car. On reboot, it may take several minutes for automatic reassignment.

After rebooting, you should be able to currently log in as root with your new password!

How to change root password on Debian

Changing the root password on Debian is similar to the method used to change it on CentOS – however, it will work on most Debian versions. Note: If something on your system does not look like what we describe in this guide, then you should stop – this may make your system unbootable.

Step 1: Enter the grub options screen.

Reboot the system and press “E” after the boot screen appears and proceed to the next step when done.

Step 2: Modify grub configuration.

Scroll down to the line that starts with “Linux” and go to the end of that line, either by pressing the END key on your keyboard or manually scrolling to the end of the line. Enter the following data to download in the “#” line:


Don’t forget to make white space between the previous arguments and the new argument you just wrote.

Step 3: Install the file system.

Enter the following command to mount the root filesystem in read / write mode. This is necessary in order to be able to edit the root password:

mount -o remount rw /

Then proceed to the next step.

Step 4: Change administrator password.

Just enter the “passwd” command to change your superuser password. Press Enter and then re-enter the new password. When this is done, simply reboot the system using “reboot” or the “exit” command. If it hangs / freezes on the screen, just reset the car.

How to change root password on Fedora

Fedora has two methods for resetting privileged account password. One way is through terminal and administrator account, and the other way is through a similar reset method, which was done with CentOS or Debian, including grub. Here are the steps you need to take to reset your password for both methods.

Method 1: Terminal at boot time (must have a different account on the machine)

DISCLAIMER The account you usually log in with must be able to execute SUDO commands in order to be able to log in as the root account. Otherwise, it won’t work and method 2 will be the only way to reset the admin password.

Step 1: Open a terminal.

Open the menu and find “Terminal”. Open it up. SSH into the machine if it is a server or VPS.

Step 2: Extend your terminal session to root account.

Enter the following command to extend your root account terminal session (this will only work if the user account is in the sudoers / administrators list):

sudo su

Enter the password for your user account, you must be registered as the root account.

Step 3: Change your password.

Changing your password is currently a piece of cake – just use the “passwd” command to change your password as you would normally with any other user account. When this is done, simply issue the “exit” command to end your session as the root account.

Method 2: Grub (No account required)

Step 1: Enter the grub options menu.

Restart your computer and then press “e” when selecting an operating system to enter the grub menu. Once in, find the line that starts with “Linux”, “linux16”, or “linuxefi”. Go to the end of the line and add the following:

rw init=/bin/bash

Then press Ctrl + X to boot into the system.

Step 2: Change the password for the root user account.

Now all you have to do is execute the “passwd” command, which will output something similar to this:

Changing password for user root.

New password: _

Just enter a new password, then enter it again to confirm.

Step 3: Fix the SELinux context of the / etc / shadow file

Like the CentOS manual, you need to run the auto label script for the password change to take effect. To do this, run the following command:

touch /.autorelabel

Now you can restart your computer or run the following command:

/sbin/reboot -f

This first reboot when reassigning will take some time. When you are done rebooting, your new password will be what logs you into the root account.