How to Regenerate Grub2 Config Files on Linux

Grub2 config file may need to be re-generated after changing Grub2 configurations such as configuration changes and setting default boot entries. The Grub2’s config file may be at different locations depending on your Linux distro and whether your Linux is booted in BIOS or UEFI mode. This makes regenerating Grub2 config file not easy for Linux users especially beginners. In this post, I will introduce a way that should be portable and easy to use. This method is tested on Fedora and will likely work on RHEL / CentOS / Scientific Linux . For Ubuntu users, please check the “Notes for Ubuntu users” at the end.

The quick way: use the grub2-regen-cfg.bash script

We provide a script grub2-regen-cfg.bash that you can directly use to regenerate the Grub2 config file.

The manual way: step by step

Depending whether your Linux systems is under BIOS or UEFI mode, we choose different config file locations. The files under /etc/ is the most portable location as far as I know as some Linux distros may change the default location that grub2 choose to store the config files under /boot/.

if [ -d /sys/firmware/efi ]; then
    grubcfg="/etc/grub2-efi.cfg"
else
    grubcfg="/etc/grub2.cfg"
fi

After choosing the correct location of the grub2 config file and store it in $grubcfg, we can re-generate the config file by:

# cp $grubcfg $grubcfg.bak # make a backup in case bad things happened
# grub2-mkconfig -o $grubcfg

Okay, it is done.

Notes for Ubuntu users

Ubuntu and its derived distros have a useful script named update-grub and you can use it to update your grub configuration file by running sudo update-grub.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

One comment:

  1. Note that if the /etc/grub2.cfg or /etc/grub2-efi.cfg is not a softlink to the configuration file under /boot/grub2/, you may manually copy the files to the correct location or re-construct the soft link structure.

Leave a Reply

Your email address will not be published. Required fields are marked *