How to change root password linux
How to change root password linux
How to Change a Password in Linux
In this guide, we’ll cover how to change a password in Linux using the passwd command followed by the name of the desired user. This method can be used to change the password for the root user as well as for individual users.
*Note: before changing any passwords, it is advised that you write down any pre-existing passwords as well as any new passwords you intend to create into a notepad file or store them in a password management software where they can be accessed as needed.
Resetting the Root Password
In order to reset the root password of a Linux server, follow these steps below.
passwd root
Changing Passwords in Linux for Non-root Users
As in the example above, to change a password in Linux for anyone other than the root user, you can use the passwd command followed by the name of the intended user.
For example, let’s say you have a user named “user_1” and you need to change their password. To do so, you would use the following command:
passwd user_1
Same as in the process outlined above, once the command has been entered, you will be prompted to enter a new password for this user. After typing in the new password, hit enter to finalize the changes.
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
Seeking a better Dedicated Server solution? In the market for Private Cloud or Colocation services? Check out Hivelocity’s extensive list of products for great deals and offers.
With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.
Unsure which of our services is best for your particular needs? Call or live chat with one of our sales agents today and see the difference Hivelocity can make for you.
How to Change root Password on Linux
The root account has the ability to control anything on a Unix system, and Linux is no different. One has to use the passwd command to change Linux password data, and the same goes for root. Due to the special nature of this command, some precautions do apply. Notably, it’s not possible to change Linux password data for the root user readily on an Ubuntu or Ubuntu spin system without first making some changes.
A majority of users who want to change Linux password data will need to open a graphical terminal. You can hold down Ctrl, Alt and T to do so or perhaps click on the Applications menu, head to system tools and select terminal. CentOS users who don’t have a graphical interface installed will want to log into their virtual terminal with a user account, assuming they have the sudo packages updated.
Method 1: Change root Password on Most Linux Distributions
Assuming your distribution has the sudo package installed, you can change the root passwd with the sudo passwd root command. You’ll be asked for your sudo password first, and then you’ll have to enter a new UNIX password twice. Your password will be tested for complexity, so you’ll want to make sure that it’s good. Once you’ve entered the password in twice, your root account should be changed. Hold down Ctrl and Alt then push F1-F6 to get to an empty virtual terminal. Type root and then the new password to make sure it works. Considering the hazards related to working as root, make sure to type exit to get out of this console. Hold down Ctrl, Alt and F7 to get back to your graphical environment if you’re not working from a server. This method should work on Debian and many other distributions. It should also work with Arch if you have the all the prerequisite packages installed.
Method 2: Change root Password on Ubuntu Linux
Method 3: Change Linux Password Data as root
If you’re logged in as root say on a CentOS, Fedora or Red Hat Enterprise Linux server’s virtual terminal, then you can change the password simply by typing passwd and then pushing enter. You’ll receive a prompt that reads “Enter new UNIX password:” and then you’ll be asked to type it a second time. Keep in mind that you should have actually logged into root in order to do this, or perhaps used sudo su to access a root prompt. This won’t work on an unaltered Ubuntu or Linux Mint system, but it’s good for those running server systems or using ssh to log into ones remotely. Make sure before you try this that you have # as the symbol in your prompt. Depending on the default login shell set for root, the prompt may have other bits of information in it.
Remember that regardless of what method or which distribution you’re working on, you can also type whoami and push enter to find out who you’re logged in as.
How To Change a Lost Root Password – Linux
Changing a lost root password in Linux is not terribly difficult. If you have access to sudo, then it is very easy. If not, then it is a little more involved, but is fairly straight forward. This guide covers systemd based distributions, which is most modern distributions. I have tested it on CentOS 7, Debian Stretch, Ubuntu 18 LTS, and OpenSUSE LEAP 15. It should work on Mint, Fedora, Oracle Linux 7, SLES, and RHEL 7.
If You Can Use Sudo
If you can login and use sudo, then open a terminal run the command sudo passwd, then press enter. Follow the prompts. It is normal to not see any output when you are typing your sudo password or the new root password. Here is an example:
If everything went as it should, you should see the last line from the example in your terminal.
If You Can’t Use Sudo
Suppose this happens:
If you don’t have access to a user that can use sudo, you will need to reboot your system and load a shell instead of your init system, mount your root file system, change the root password, reset the SELinux labels, and reboot the system. It sounds (and is) complicated, but it is a straight forward process that most people can do with some guidance.
Boot into a shell
When you reboot your system, you will need to be paying attention so you can interrupt the normal boot process. When the grub screen appears, quickly press the Esc key to stop it from automatically booting. The grub screen will look different depending on your distribution, but it will look similar to one of these:
If all is well, you should get a prompt that looks something like this:
Mount the Root File System
The root file system will already be mounted, but it will be read only. The following command will allow you to write to your drive.
If all is well, you should see no output and a new prompt.
Changing the Root Password
Run the command passwd. Follow the prompts. You will not see anything as you are typing the password. Your screen should look something like this so far:
Reset SELinux Labels
On Red Hat based systems such as RHEL, CentOS, Fedora, etc., changing the root password from the minimal environment will cause the SELinux label on file storing your passwords to be incorrect. If this happens, you won’t be able to login. Create the file /.autorelabel to automatically fix this when you boot the system. Use the command touch /.autorelabel to create it.
On systems that don’t have SELinux enabled, this won’t hurt anything. If you aren’t sure, go ahead and do it anyway.
Boot the System
Now you can fully boot the system. Start systemd with the exec command followed by the path of the systemd binary. Replace PATH in the example below with the appropriate value from the table.
OS | PATH |
---|---|
CentOS 7 Oracle Linux 7 RHEL 7 Fedora | /lib/systemd/systemd |
openSUSE | /usr/lib/systemd/systemd |
Debian Ubuntu Mint | /bin/systemd |
Once the system boots, test the new root password and reboot.
By using the information on this site, you agree to hold Tyler’s Guides harmless for any damages or loss resulting from its usage.
If you find an error in any of the guides on this site, please report it using the contact page.
As an Amazon Associate I earn from qualifying purchases.
How to Reset or Change the Root Password in Linux
Home » SysAdmin » How to Reset or Change the Root Password in Linux
In Linux, root privileges (or root access) refers to a user account that has full access to all files, applications, and system functions.
Most basic Linux user accounts run with limited privileges. This keeps users from making mistakes or accidentally exposing the system to vulnerabilities.
Some versions of Linux will elevate your user privileges for a set amount of time around (15 minutes) before reverting. Other versions only perform a single task with elevated privileges.
It’s a good idea to change your passwords regularly and consider using enterprise password management software.
This guide will help you change your Linux root password in Ubuntu or CentOS, or reset the password.
Changing Your Root Password in Ubuntu
Step 1: Open a Terminal Window
Right-click the desktop, then left-click Open in terminal.
Alternately, you can click Menu > Applications > Accessories > Terminal.
Step 2: Change Your Root Password
In the terminal window, type the following:
The system will prompt you to enter your password – this is the same password you use to log in to the system.
Next, the system will prompt you to enter a new password. Do so, and then re-enter it exactly the same when the system prompts you to retype the password. This double-entry confirms that you have typed the password correctly.
Resetting a Root Password in Ubuntu
In some situations, you may need to access an account for which you’ve lost or forgotten a password.
Step 1: Boot to Recovery Mode
Restart your system. Once you see the splash screen for the computer manufacturer, hold down the shift key. The system should come up with a black and white GRUB, or boot menu, with different Linux kernel versions displayed.
Select the second one from the top – the highest revision, followed by (recovery mode). Press Enter.
Step 2: Drop Out to Root Shell
The system should display a menu with different boot options. Use the arrow keys to navigate to the option labeled root and press Enter.
The system should respond by giving you a command-line interface with a prompt.
Step 3: Remount the File System with Write-Permissions
Right now, your system only has read-only access to your system. That means it can look at the data, but cannot make any changes. But we need write-access to change the password, so we’ll need to remount the drive with the appropriate permissions.
At the prompt, type:
Press Enter. This should allow you to make changes to the data on the hard drive.
Step 4: Change the Password
At the prompt, type:
Substitute the name of the user for username, then press Enter. The system asks you to type a new UNIX password and then to retype it.
Once you’ve entered and confirmed the new password, reboot the system by entering the following:
Hit Enter, and your system should restart. Don’t press any keys, let the system come up to the login screen, and test to make sure the new password works.
Changing the Root Password in CentOS
Changing a password in CentOS is almost identical to changing it in Ubuntu.
Step 1: Access the Command Line (Terminal)
Right-click the desktop, then left-click Open in Terminal. Or, click Menu > Applications > Utilities > Terminal.
Step 2: Change the Password
At the prompt, type the following, then press Enter:
The system should prompt you to enter your existing password. Do so, then follow the prompts to enter and confirm a new root password.
Reset Root Password in CentOS
This is a similar process as in Ubuntu, with a couple of variations.
Step 1: Access Boot Menu
Restart the system, then tap the Esc key about once per second to launch the GRUB menu.
Step 2: Edit Boot Options
Use the arrows to highlight the version of Linux you boot into, then press e.
Use the arrows to highlight the line that starts with kernel or Linux.
Press E.
At the end of the line, add a space then type single. Press Enter, then boot into single-user mode by pressing Ctrl-X or B. (The system will display the command to use.)
Step 3: Remount the Drive
You should have a command line, and you’ll have root privileges. To enable read/write access on your hard drive, type the following:
Step 4: Changing the Password
Type the following:
Press Enter, and the system should prompt you to enter and confirm a new password.
Step 5: Restart
Type the following, pressing enter after each line:
Your system should restart. Confirm that your new password works by logging in.
Note: If you ever come across a Linux boot failure, be sure to save our guide on using GRUB rescue to troubleshoot it.
If you already have access to your user account, resetting or changing your password in Linux is simple.
It can be more challenging if you’ve lost or forgotten a password, but with a little creative restarting and editing, you shouldn’t find it too hard.
How to Change the root Password in Linux
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.
Table of Contents
How to Change the Root Password in 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 accounts on the 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.
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:
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:
How to change the 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:
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 the 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:
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:
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 the 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 accounts on the 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):
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:
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:
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:
You can now reboot your machine either by executing the following command:
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.