How to reset Root Password in CentOS 7

Share on Social Media

Learn how to reset root password in CentOS 7 with this step-by-step guide. Regain access to your system quickly and securely. #centlinux #linux #recovery

Problem Statement

As we know that, the root is the superuser in Linux. We often come to situations, where, the root password is not available. Although it is trivial to reset root password, if you have a user with sudo rights, otherwise it becomes a little tricky.

In this post, I will demonstrate, how to reset root password in CentOS 7.

Read Also: How to reset Root Password in CentOS 6

How to reset Root Password in CentOS 7
How to reset Root Password in CentOS 7

Reset Root Password in CentOS 7

To reset root password we must have physical access to the Server machine. Because we have to login into Emergency target.

Reset Root Password in CentOS 7 1
Reset Root Password in CentOS 7 1

Once successfully reach at emergency target, follow the steps below to reset root password.

By default / filesystem is mounted at /sysroot with ro (read only) option in emergency target. We have to change it to rw (read write) mode, so we can change the files in our / filesystem.

Reset Root Password in CentOS 7 2
Reset Root Password in CentOS 7 2

Now, we have our / filesystem in rw mode mounted at /sysroot. Now, we have to chroot to /sysroot directory, so we can execute commands and change files therein.

Reset Root Password in CentOS 7 3
Reset Root Password in CentOS 7 3

We have successfully chroot to our / filesystem. Let’s change root password using passwd command.

Reset root Password 1
Reset root Password 1

This file /.autorelabel is created to notify the SELinux to perform relabelling of all the files in the / filesystem. It is because the passwd command does not update the existing /etc/shadow file, instead it creates a copy and then update the password. Therefore, the newly created file does not has proper SELinux file contexts.

Reset root Password 2
Reset root Password 2

Now, Let the system boot with default settings. System start-up will take some more time as compare to regular boots because of the relabelling process.

Reset root Password 3
Reset root Password 3

Login with new password.

If you’re serious about leveling up your Linux skills, I highly recommend the Linux Mastery: Master the Linux Command Line in 11.5 Hours by Ziyad Yehia course. It’s a practical, beginner-friendly program that takes you from the basics to advanced command line usage with clear explanations and hands-on exercises. Whether you’re a student, sysadmin, or developer, this course will help you build the confidence to navigate Linux like a pro.

👉 Enroll now through my affiliate link and start mastering the Linux command line today!

Disclaimer: This post contains affiliate links. If you purchase through these links, I may earn a small commission at no extra cost to you, which helps support this blog.

Conclusion

Resetting the root password in CentOS 7 is a straightforward process that ensures you can regain access to your system when needed. By following the outlined steps, you can securely reset your credentials and restore control without compromising system integrity. Always prioritize strong, memorable passwords and consider implementing additional security measures to prevent unauthorized access in the future.

Searching for a skilled Linux admin? From server management to security, I ensure seamless operations for your Linux systems. Find out more on my Freelancer profile!

Leave a Reply