How to Recover or Rescue Corrupted Grub Boot Loader in CentOS 7

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.

Requirements

  1. Download CentOS 7 DVD ISO Image

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.

System Boot Menu
System Boot Menu

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.

Select CentOS 7 Troubleshooting
Select CentOS 7 Troubleshooting

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.

Rescue CentOS 7 System
Rescue CentOS 7 System
Rescue CentOS 7Process
Rescue CentOS 7Process

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.

CentOS 7 Rescue Prompt
CentOS 7 Rescue Prompt

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.

Mount CentOS 7 Image
Mount CentOS 7 Image

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 /dev/vda or /dev/xvda.

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
Install Grub Boot Loader in CentOS 7
Install Grub Boot Loader in CentOS 7

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.

Exit CentOS 7 Grub Prompt
Exit CentOS 7 Grub Prompt

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.

CentOS 7 Grub Menu
CentOS 7 Grub Menu

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.

Root Encrypted Password
Root Encrypted Password
Delete Root Encrypted Password
Delete Root Encrypted Password

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.

Set New Root Password in CentOS 7
Set New Root Password in CentOS 7

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Matei Cezar
I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

19 Comments

Leave a Reply
  1. Hi all,

    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?

    Reply
  2. I keep getting the error ‘Welcome to emergency mode!‘ whenever I start my centos7 on my virtual box. I welcome your assistance.

    Reply
  3. 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

    Reply
  4. After installing Grub if you don’t find the file “/boot/grub2/grub.cfg” then run the command grub2-mkconfig --output=/boot/grub2/grub.cfg which will automatically make grub.cfg with existing Linux images on the system.

    Reply
    • Hi Vasu,

      I followed the steps, exited from console and while reboot i removed the CentOS DVD and now i am getting following error

      error: no such devices: 3fda0d3e-xxxx-xxxxx-xxxxx
      Entering rescue mode...
      grub rescue>
      

      Please can you help me how do i go from here?

      Thanks
      Partha

      Reply
      • 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.

        Reply
  5. 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

    Regards
    Partha

    Reply
  6. 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.

    Reply
    • 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

      Reply
  7. 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.

    Reply
  8. # chroot /mnt/sysimage
    # lsblk
    

    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

    # /sbin/grub2-install /dev/sda
    

    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

    Reply
  9. 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?

    Reply
      • 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.

        Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.