In Ubuntu (and I guess in Debian too) there is a system script named update-grub which automatically executes grub-mkconfig -o with the correct path for the grub configuration file.

Is there any similar command for Red Hat based distributions?

If not, how do the system knows where is the grub configuration file to update when a new kernel version is installed?

up vote 14 down vote accepted

Specific actions that need to happen when a RPM package is installed or removed are included within the RPM package itself in pre-install, post-install, pre-uninstall and post-uninstall sections.

For every installed RPM package you can query the RPM database for the exact scripts that are included with the rpm command:

rpm -q --scripts <package-name>

Running that command on a kernel package for CentOS 6 returns among others:

postinstall scriptlet (using /bin/sh):
<snip>
/sbin/new-kernel-pkg --package kernel --install 2.6.32-431.17.1.el6.x86_64 || exit $?

From the manual:

new-kernel-package - tool to script kernel installation

  • 2
    running the same command on Fedora 20 yields /bin/kernel-install instead. Please edit your answer to include this information for future reference – pqnet Aug 26 '14 at 11:26
  • 2
    That is exactly why I started my answer with the steps needed to find out. I think querying the installation scripts is likely to be pretty universal in the RPM world in helping you understand how the kernel update is effected. That way my answer will withstand the test of time much better than only naming a tool/command/support-script which you already demonstrated is very release and distribution dependent. – HBruijn Aug 26 '14 at 12:16
  • 2
    Yeah @HBruijn's right. There's too much variability across Fedora, RHEL, and CentOS to provide a single answer, better to show how to find it. – slm Nov 25 '14 at 2:41

After analyzing the scripts in Fedora, I realize that the configuration file path is read from the symlink /etc/grub2.conf. The correct grub2-mkconfig line is thus:

grub2-mkconfig -o "$(readlink /etc/grub2.conf)"
  • 5
    On CentOS 7 the links seems to be /etc/grub2.cfg – Anthon Dec 22 '14 at 13:28
  • 6
    grub2-mkconfig --output=/boot/grub2/grub.cfg seems to be the approved way in the fedora manual – Kendrick Aug 11 '15 at 23:12

On Fedora I use:

grub2-mkconfig -o "$(readlink -e /etc/grub2.cfg)"

because executing with no option to readlink returns a relative path, and grub2-mkconfig gives an error:

$ ls -l /etc/grub2.cfg
lrwxrwxrwx. 1 root root 22 Dec 10  2015 /etc/grub2.cfg -> ../boot/grub2/grub.cfg

$ readlink /etc/grub2.cfg
../boot/grub2/grub.cfg

$ sudo grub2-mkconfig -o "$(readlink /etc/grub2.cfg)"
/usr/sbin/grub2-mkconfig: line 244: ../boot/grub2/grub.cfg.new: No such file or directory

I use the -e option so that if the symlink doesn't resolve to a file that exists, output displays on stdout so I know something went wrong.

From the man page for readlink:

      -e, --canonicalize-existing
          canonicalize  by  following  every symlink in every component of
          the given name recursively, all components must exist

The /etc/grub2.cfg symlink points at the BIOS version. On a UEFI system, use:

$ sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg 

You could also use:

$ sudo grub2-mkconfig -o $(readlink -f /etc/grub2-efi.cfg)

See https://docs.fedoraproject.org/f27/system-administrators-guide/kernel-module-driver-configuration/Working_with_the_GRUB_2_Boot_Loader.html#sec-Editing_a_Menu_Entry

edit grub file with vi or vim, save changes and close editor with :wq!

This is what you need to run to update grub in RedHat or CentOS:

grub2-mkconfig -o /boot/grub2/grub.cfg

The update-grub script in Ubuntu is actually just a stub for grub-mkconfig, and it can be adapted to other distros without too much pain. Here it is in its entirety:

#!/bin/sh
set -e
exec grub-mkconfig -o /boot/grub/grub.cfg "$@"

This effectively does what is recommended in the CentOS wiki, and in other answers here - the only difference is that you need to change grub to grub2 in the command and the output path.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.