If you have forgotten your root password on a Red Hat Enterprise Linux 6.x based server or a CentOS 6.x based server, you can reset your root password in several different ways providing you have access to the server. The process of changing/resetting the root password involves either booting into single user mode or booting into rescue mode. Both of these options are described below. In the examples below, RHEL 6.3 was used.
Booting into Single User Mode – runlevel 1
The easiest method of resetting a lost root password is to boot your system into “Single User Mode” and simply issue the “passwd” command. In Single User Mode, your system will boot to what is known as “runlevel 1”. Your local file systems will be mounted, however, your network interfaces will not be activated. Once in Single User Mode, you will have access to a shell prompt where you can reset your root password. Unlike rescue mode, Single User Mode will automatically attempt to mount your file system. Below is a step by step guide to resetting the root password.
Boot your system
To access Single User Mode, you will need to boot your system to the “GRUB” bootloader menu screen. Here you will then be able to add additional parameters to the kernel boot parameter line.
As your system starts to boot, you will need to press the “Escape” key to access the GRUB menu screen:
From this initial menu, highlight your operating system using the arrow keys. At this screen you are presented with various options. Now press “e” to edit.
Now highlight the line starting “kernel” and press “e” again to edit the highlighted parameter.
Once you press “e” you will be taken to the end of the line. Here you will need to press the “space” key and then add the number “1” to the end of the line as per image below.
Now press “Enter” to accept your entry. You will be taken back to the previous screen.
Now press “b” to boot your system.
Your system should now boot into “Single User Mode”
At the command prompt you can issue the command “passwd” to change the root account password.
Now reboot your system by issuing the “reboot” command.
Your system will now reboot normally back to its normal runlevel.
Booting into Rescue Mode (method 2)
Rescue Mode will give you the ability to boot your server from bootable installation media such as an iso image, CD or DVD instead of the system’s Hard Drive. You would normally use “Rescue Mode” when you have a system that needs to boot from an alternative boot device due to issues encountered at boot time. Rescue Mode gives you the ability to boot and access/edit files that are stored on the system’s hard drive.
To boot your system into “Rescue Mode”, you must be able to boot your system by one of the following methods:
Boot by using a CD ROM or DVD.
Boot from an USB device.
Once you have booted your system by one of the above methods, you will need to select the option “Rescue Installed System”
Select the Language to use during Rescue Mode. In this example, English has been chosen.
Select Keyboard Type
Select the Keyboard type you are currently using. In the example a UK keyboard has been selected.
What type of Media Contains your Rescue Image?
At this point, you must provide media that contains the rescue image. In this example we are booting from an installation DVD, so we have chosen “Local CD/DVD“. Choose the option that matches your boot media.
Set up Networking
Networking is not needed for a root password reset. To continue without Networking select the option “No”. If you do choose the option “Yes”, you will be asked to manually define your network settings or use a DHCP server for a leased IP address. In this example we will choose the option of No network.
The rescue environment will now attempt to locate your Linux installation and mount this at the mount point “/mnt/sysimage“.
Rescue Environment Continued
Your system is now mounted under “/mnt/sysimage”. Click “OK” to start a shell.
Select the option “Shell Start Shell”. Then click “OK”.
You should now arrived at a command prompt. In the example this is identified as “bash-4.1#“
To switch this environment to make it your root environment issue the command: chroot /mnt/sysimage
Your prompt will now change to “sh-4#“.
Look at mounted Filesystems
To confirm your system is mounted, you can issue the command “df -hT“.
Reset Password for Root
Now to reset the root password you need to type the command passwd root
Now provide the new password for the root account.
To exit out of the rescue shell environments you will need to type “exit” twice. This will then take you to a reboot screen.
At this point, you will need to remove the bootable media you booted into Rescue Mode with as you are going to reboot the system from the system’s hard drive.
System Reboots – SELinux Relabel
The system will now boot, you will see various messages possibly including a SELinux relabel message. SELinux needs to carry out a relabel as the system was booted using a rescue kernel.
Login with New Password
You should now have arrived at the login screen. Here you will be able to login with your root account using your new password. (You may need to login as a normal user then “su” to root depending on your system set-up)