I want to script a somewhat automated method for editing /etc/default/grub and appending fips=1 boot=UUID=???? to GRUB_CMDLINE_LINUX=

coincidentally /etc/fstab contains UUID=12345blabla /boot xfs defaults 0 0. But in the event mount types are not by UUID in /etc/fstab what is a reliable way of deducing the UUID of the boot partition on linux [any RHEL/Centos 7.6 system or later]?


I'll start off by saying that this doesn't address the part of your question about automated modification of /etc/default/grub, but I suspect that shouldn't really be needed.

The UUID should be static unless you're reformatting the partition. Assuming you know the underlying device, you look at /dev/disk/by-uuid, and see something like:

$ ls -l /dev/disk/by-uuid
... 9577f213-665a-427b-aa45-8927b177224e -> ../../sda1

So you just need to find the corresponding one for the device that is your boot partition.

There's also the blkid command:

$ blkid /dev/sda1
/dev/sda1: UUID="9577f213-665a-427b-aa45-8927b177224e" ...

Are you looking at a situation where the UUID changes for some reason?

| improve this answer | |
  • UUID should never change. I guess it's a matter of expecting to parse output of mount command and find which dev is just /boot and pulling that UUID, and NOT for /boot/efi. Or maybe parse /etc/fstab – ron Mar 21 '19 at 19:15
  • Do the commands that I suggested above not work for you? – Andy Dalton Mar 22 '19 at 16:21

First, let's find the boot partition with

fdisk -l | grep '^/dev/[a-z]*[0-9]' | awk '$2 == "*"' 

Then feed its output, as kindly suggested by Andy Dalton above, to blkid

fdisk -l | grep '^/dev/[a-z]*[0-9]' | awk '$2 == "*"' | blkid $1
| improve this answer | |

If you are looking for the UUID of the device mounted as /boot on a running system, a clean way is findmnt from util-linux:

$ findmnt --output=UUID --noheadings --target=/boot 

With these self-explanatory options it outputs the UUID only. It also saves you the hassle of figuring out which device is the relevant one.

If you are just looking for the UUID of a device you know of, regardless of whether it is mounted or not, you can use blkid, as mentioned in Andy Dalton's answer:

$ sudo blkid -s UUID -o value /dev/vda1 

The -s tag and -o format options restrict the output to the value of the UUID tag only.

You can then append the UUID to GRUB_CMDLINE_LINUX with a command as:

sed -i.bkp 's/^\([[:blank:]]*GRUB_CMDLINE_LINUX=".*\)\(".*\)$/\1 fips=1 boot=UUID='"$newuuid"'\2/' /etc/default/grub

Which assumes the -i option's syntax of GNU sed: it will edit the file in-place, also creating a grub.bkp backup file.
Note that we are not checking if a boot command line parameter is already there; we are just appending fips=1newuuid before the second ", thus assuming your current command line is enclosed in double quotes and does not contain any double quote.

| improve this answer | |

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.