2

How can I capture the latest kernel version from grub.conf ( in case default=0)?

For example the latest kernel from the file is 2.6.18-409.el5

 #          all kernel and initrd paths are relative to /boot/, eg.
 #          root (hd0,0)
 #          kernel /vmlinuz-version ro root=/dev/rootvg/slash
 #          initrd /initrd-version.img

#boot=/dev/cciss/c0d0
default=0
timeout=5
#splashimage=(hd0,0)/grub/splash.xpm.gz
#hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-409.el5)
    root (hd0,0)
    kernel /vmlinuz-2.6.18-409.el5 ro root=/dev/rootvg/slash
    initrd /initrd-2.6.18-409.el5.img

I tried this:

  grep kernel /etc/grub.conf | grep -v "#" |sed s'/-/ /' | head -1 | awk '{print $3}'
  2.6.18-409.el5

but I feel this approach isn't reliable and doesn't handle some cases .

What is the right approach to get the latest kernel only from `grub.conf``.

  • Could you give more detail on what you mean by "latest"? Do you mean by version number, by the order they appear in the file, or something else? Your example grub.conf contains only one, but if there were more, your command would still show the first. – JigglyNaga Jun 8 '16 at 12:51
0

The approach is specific to the grub version, note that for grub 2+ then kernel has been replaced by linux, so this approach takes that into account

awk '/\/vmlinuz{kernel[$2]}
     END{
     asorti(kernel);
     latest=kernel[length(kernel)];
     print "Latest kernel : ",gensub(/\/vmlinuz-([[:digit:].-]*[[:digit:]]).*$/,"\\1","1",latest)}
    ' /boot/grub/grub.cfg

Sample Output

Latest kernel :  3.13.0-86

Note: Requires [ gnu-awk ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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