how to change root password in linux

Cloud VPS Hosting

In today’s tutorial, we will show you how to change the root password in Linux. The root account on a Linux machine is the highest authority account on the system. It has control over everything, can delete and modify anything, and has permission to run any program. Simply put, the root account can do anything that the system supports. Typically, casual users never need to use the root account, while more experienced users need the root account more often, especially if they spend a large amount of their time at a terminal. So, even if you don’t use the root account much, it helps to put a more secure password so that malicious users don’t log into the root account and wreak havoc on your system.

In this tutorial, we will be showing you how to change your root password on various Linux distributions. We will go over the process of changing the root password  for Ubuntu, CentOS, Debian, and Fedora. All of these are fairly simple to do, and won’t take much time to complete. Let’s get started.

How to change root password on Ubuntu

Changing your root password on Ubuntu 16.04 (this works on other versions, along with any Ubuntu server version) is very easy.

Method 1: Through a terminal while booted (Must have other account on machine)

Step 1: Open a Terminal.

This can be done by either pressing Ctrl+Alt+T, or by finding the Terminal program using the search function. If it’s a VPS or a server, simply SSH in like you normally would.

Step 2: Enter the password change command.

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

sudo -i passwd

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

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 are unable to use the sudo command. This method uses grub to get past the operating system altogether, and allows you to change the password of the root user without needing to know any passwords.

Method 2: Grub (No account needed)

Step 1: Enter the grub options screen.

Restart your machine. If Ubuntu is your only operating system on the machine, then hold shift after the bios screen passes to enter the operating system selection menu. If you have more than one operating system on your machine, then this menu shows up automatically. Press the ‘e’ key to enter the grub options screen for ubuntu.

Step 2: Edit the grub option.

When in the editing menu, scroll down to the line starting with the word “linux”, and append the following to 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 root password is now possible. Execute the “passwd” command, and enter your new password – re-enter it when prompted. Your root password has now been changed. Simply use the “reboot” command, or if that doesn’t work, use the following command to restart your system to its normal state:

exec /sbin/init

How to change root password on CentOS

This tutorial focuses on CentOS 7 – the procedure for a password reset actually differs between CentOS 7 and previous versions of CentOS, which is why we will only focus on the latest version.

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

Step 1: Enter the grub options screen.

This is done by either restarting your machine, or by starting it if it was already off. When the CentOS boot screen appears, press ‘e’ to enter the grub options menu.

Step 2: Edit the grub option.

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 the filesystem.

Using the command “chroot /sysroot”, we are able to access and perform commands on the filesystem as if we were booted into the operating system. We need to do this in order to access the “passwd” command.

Step 4: Change your root password.

Enter the command “passwd root” to change your root password. It will ask you to confirm the password, which is where you will enter your new password again. Once done, proceed to the next step.

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

If you have SELinux disabled, skip this step. If you do not have SELinux disabled, enter the following command to make sure the new /etc/shadow file that was created when you changed your password is accepted as the new file:

touch /.autorelabel

After this is done, simply type “exit”, followed by another “exit”. If your machine hangs/freezes, just reset your machine. While rebooting, it may take a few minutes for the auto relabeling to occur.

Once you reboot, you should be able to now login as the root user with your new root password!

How to change root password on Debian

Changing your root password on Debian is similar to the method used for changing it on CentOS – however, this will work on most versions of Debian. NOTE: If something on your system does not look like what we describe in this tutorial, then you should stop – you may cause your system to become unbootable.

Step 1: Enter the grub options screen.

Do this by restarting your system and pressing ‘e’ once the boot screen shows up. Proceed to the next step when done.

Step 2: Edit your 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 by manually scrolling to the end of the line. Enter the following to boot into the ‘#’ prompt:

init=/bin/sh

Don’t forget to have a blank space between the previous arguments and the new argument that you just wrote.

Step 3: Mount the filesystem.

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

mount -o remount rw /

Once done, proceed to the next step.

Step 4: Change your root password.

Simply enter the “passwd” command to change your root password. Enter, then re-enter your new password when prompted. When done, just reboot your system by using the “reboot” or “exit” command. If it hangs/freezes on this screen, just reset your machine.

How to change root password on Fedora

Fedora has two methods for resetting the root account’s password. One way is through a terminal and an administrator account, and the other way is through a similar method of resetting that was done with CentOS or Debian, involving grub. Here are the steps you need to take to reset your password for both methods.

Method 1: Terminal while booted (Must have other account on machine)

DISCLAIMER: The account with which you log in normally has to be able to execute sudo commands in order to be able to log in as the root account. Otherwise, this will not work, and method 2 will be the only way to reset the root password.

Step 1: Open a terminal.

Open your menu and search for ‘Terminal’. Open it. SSH into your machine if it is a server or a VPS.

Step 2: Escalate your terminal session to the root account.

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

sudo su

Enter your user account’s password, and you should be logged in as the root account.

Step 3: Change the password.

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

Method 2: Grub (No account needed)

Step 1: Enter the grub options menu.

Restart your machine, then press ‘e’ when choosing an operating system to enter the grub menu. Once in, search for the line starting 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 that needs to be done is to execute the “passwd” command, which will output something similar to this:

Changing password for user root.

New password: _

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

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

Like the CentOS guide, you need to run the auto label script in order to have your password change take effect. This is done by executing the following command:

touch /.autorelabel

You can now reboot your machine either by executing the following command:

/sbin/reboot -f

This first reboot while relabeling will take a while. When you finish rebooting, your new password will be what logs you into the root account.

If you use one of our Linux VPS hosting services, you can simply ask our technical support via live chat to change your root password on any Linux distribution your server is running on. They are available 24/7 and will change the root password of your Linux machine in less than five minutes.

If you liked this post on how to change the root password on Linux, please share it with your friends via social media. If you have any questions, please leave a comment below – our system administrators will reply to it.