In this tutorial we’ll cover the process of rescuing a corrupted boot loader in CentOS 7 or Red Hat Enterprise Linux 7 and recover the a forgotten root password.
The GRUB boot loader can sometimes be damaged, compromised or deleted in CentOS due to various issues, such as hardware or software related failures or sometimes can be replaced by other operating systems, in case of dual-booting. A corrupted Grub boot loader makes a CentOS/RHEL system unable to boot and transfer the control further to Linux kernel.
The Grub boot loader stage one is installed on the first 448 bytes at the beginning of every hard disk, in an area typically known as the Master Boot Record (MBR).
Read Also: How to Rescue, Repair and Recover Grub Boot Loader in Ubuntu
The MBR maximum size is 512 byes long. If from some reason the first 448 bytes are overwritten, the CentOS or Red Hat Enterprise Linux cannot be loaded unless you boot the machine with a CentOS ISO image in rescue mode or using other boot loading methods and reinstall the MBR GRUB boot loader.
Recover GRUB Boot Loader in CentOS 7
1. On the first step, download the latest version of CentOS 7 ISO image and burn it to a DVD or create a bootable USB stick. Place the bootable image into your machine appropriate drive and reboot the machine.
While the BIOS performs the POSTs tests, press a special key (Esc, F2, F11, F12, Del depending on the motherboard instructions) in order to enter BIOS settings and modify the boot sequence so that the bootable DVD/USB image is booted first at machine start-up, as illustrated in the below image.
2. After the CentOS 7 bootable media has been detected, the first screen will appear in your machine monitor output. From the first menu choose the Troubleshooting option and press [enter] key to continue.
3. On the next screen choose Rescue a CentOS system option and press [enter] key to move further. A new screen will appear with the message ‘Press the Enter key to begin the installation process’. Here, just press [enter] key again to load the CentOS system to memory.
4. After the installer software loads into your machine RAM, the rescue environment prompt will appear on your screen. On this prompt type
1 in order to Continue with the system recovery process, as illustrated in the below image.
5. On the next prompt the rescue program will inform you that your system has been mounted under
/mnt/sysimage directory. Here, as the rescue program suggests, type chroot /mnt/sysimage in order to change Linux tree hierarchy from the ISO image to the mounted root partition under your disk.
6. Next, identify your machine hard drive by issuing the below command in the rescue prompt.
# ls /dev/sd*
In case your machine uses an underlying old physical RAID controller, the disks will have other names, such as
/dev/cciss. Also, in case your CentOS system is installed under a virtual machine, the hard disks can be named
However, after you’ve identified your machine hard disk, you can start installing the GRUB boot loader by issuing the below commands.
# ls /sbin | grep grub2 # Identify GRUB installation command # /sbin/grub2-install /dev/sda # Install the boot loader in the boot partition of the first hard disk
7. After the GRUB2 boot loader is successfully installed in your hard disk MBR area, type exit to return to the CentOS boot ISO image tree and reboot the machine by typing init 6 in the console, as illustrated in the below screenshot.
8. After machine restart, you should, first, enter BIOS settings and change the boot order menu (place the hard disk with the installed MBR boot loader on the first position in boot menu order).
Save BIOS settings and, again, reboot the machine to apply the new boot order. After reboot the machine should start directly into the GRUB menu, as shown in the below image.
Congratulations! You’ve successfully repaired your CentOS 7 system damaged GRUB boot loader. Be aware that sometimes, after restoring the GRUB boot loader, the machine will restart once or twice in order to apply the new grub configuration.
Recover Root Password in CentOS 7
9. If you’ve forgotten the root password and you cannot log in to CentOS 7 system, you can basically reset (blank) the password by booting the CentOS 7 ISO DVD image in recovery mode and follow the same steps as shown above, until you reach step 6. While you’re chrooted into your CentOS installation file system, issue the following command in order to edit Linux accounts password file.
# vi /etc/shadow
In shadow file, identify the root password line (usually is the first line), enter vi edit mode by pressing the
i key and delete the entire string in between the first colon
“:” and the second colon
”:”, as illustrated in the below screenshot.
After you finish, save the file by pressing the following keys in this order
Esc -> : -> wq!
10. Finally, exit the chrooted console and type init 6 to reboot the machine. After reboot, login to your CentOS system with the root account, which has no password configured now, and setup a new password for root user by executing the passwd command, as illustrated in the below screenshot.
That’s all! Booting a physical machine or a VM with a CentOS 7 DVD ISO image in recovery mode can help system administrators to perform various troubleshooting tasks for a broken system, such as recovering data or the ones described in the tutorial.
19 thoughts on “How to Recover or Rescue Corrupted Grub Boot Loader in CentOS 7”
When I’m updating my centos 7 server. one package “Libmnl” I installed and updated the server. after rebooting the server is completely down. if I am trying to troubleshoot the server after GRUB boot it is showing blank console. it is not coming to single-user mode also. This is a staging server
Can anyone help me, how to fix this issue?
I keep getting the error ‘Welcome to emergency mode!‘ whenever I start my centos7 on my virtual box. I welcome your assistance.
I think its filesystem problem try to fix it by running fsck.
I completed the steps listed here successfully. The only addition was that I had to manually mount /boot and /boot/efi.
I ran grub2-install /dev/sda and grub2-install /dev/sdb (I have RAID1 for /boot and manually cloned /boot/efi).
I got no errors, but when I try to reboot it just returns to the grub> prompt
After installing Grub if you don’t find the file “/boot/grub2/grub.cfg” then run the command
grub2-mkconfig --output=/boot/grub2/grub.cfgwhich will automatically make grub.cfg with existing Linux images on the system.
Thanks for sharing the tip with us, hope this will help other users who are unable to find grub.cfg file in Linux.
I followed the steps, exited from console and while reboot i removed the CentOS DVD and now i am getting following error
Please can you help me how do i go from here?
If anyone else ends up here with this issue. I ran these steps successfully but it still went to the “gurb>” menu (even after attempting to recreate the /boot/grub2/grub.cfg file). There was no error but it dd not replace the grub.cfg file. I manually moved it to grub.cfg.bak and copied a rpmsave version of the grub.cfg file to grub.cfg. I also did a restorecon -rv /boot/grub2/ (although I don’t know if this had any impact).
Now it boots properly.
I am not able to recover my VM (Centos-7.1) . I followed the steps and after reboot i see grub>
Can any one please help me
I was unable to recover my centos 7 VM on VirtualBox following above steps. Though grub2-install was successful it goes to grub> prompt after reboot.
I think I found the fix for this!
Short answer: “Looks like this is intentional to avoid the fallout of unsuspecting users running grub2-install. To regain ability to grub2-install on EFI, install package grub2-efi-modules.”
Thanks Richard. I’ve installed grub2-efi-modules on my Centos 7.5 server. Let’s hope I never have to use the recovery procedure.
Incidentally, I discovered that removing a working Centos 7 server drive from a laptop for a short period of time can cause the boot sector to become corrupted.
You can read about my experience and the on-going saga at https://www.centos.org/forums/viewtopic.php?f=47&t=67772
The method for fixing a bad boot sector clearly doesn’t work in Centos 7.5. I’m getting the same results as Vijay.
The /usr/lib/grub/ directory on my server contains another directory: /i386-pc. There is no /x86_64.efi directory anywhere on the server.
Where is the author? Missing in action?
You will be wasting your time if you follow the author’s instructions for repairing a bad boot sector.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
sda1 8:1 0 120M 0 part /boot/efi
sda2 8:2 0 180G 0 part /
sda3 8:3 0 14G 0 part [SWAP]
sda4 8:4 0 43G 0 part /home
I am getting error like:
/sbin/grub2-install: error : /usr/lib/grub/x86_64.efi/modinfo.sh doesn't exist. Please specify --target or --directory
Can you please tell me what I need to do for this error
I’m getting that error as well – did you make any headway on resolving it?
“Looks like this is intentional to avoid the fallout of unsuspecting users running grub2-install. To regain ability to grub2-install on EFI, install package grub2-efi-modules.”
Hey. Great article. Almost perfect but when I get to the grub2-install step it doesn’t work. It says only grub2-macbless in the list instead of all the ones you list. I tried reinstalling the grub files but it says there is an error with YUM on YumMain line 28 not found. Any ideas on what this is?
It sounds like you didn’t successfully chroot to /mnt/sysimage?
chroot doesn’t fix the problem.
It sounds like there may be more than one reason why a Centos 7 system that has been operating successfully for several years suddenly decides not to boot up again. This article doesn’t consider any alternatives.