Ubuntu – Sed Usage to update GRUB file

16.04grub2scriptssedtext processing

I am new to using sed but quickly learning to love it for its general flexibility in automation. I am looking to run a script with various security controls every time I deploy a new system, and part of that requires me to lock down the GRUB. I am looking to unlock the general Ubuntu option from the GRUB menu so anyone can use it after that without a password, while keeping everything else password protected.

that being said, I am looking to search the /boot/grub/grub.cfg file for a specific line and add –unrestricted to that option. ( We only use 16.04.03 so all systems would theoretically have the same file upon install )

The line in the file is:

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-2aa7ed2c-67a7-42d5-84fb-0ddab74c5dd0' {

And I want it to look like:

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-simple-2aa7ed2c-67a7-42d5-84fb-0ddab74c5dd0' {

I know sed -i is for in place editing and s/ should search. But beyond that I am lost on the syntax here to make it actually do what we need. As far as I can tell, that positioning of –unrestricted is needed as I believe it would be easier if we didnt have to have such a long search, but again I am new to sed and unsure. Could someone help me with this sed insertion? Preferably also explain to me why different syntax goes where it does, so I know for the future.

Best Answer

Here is the command.

sed -i "s/--class os/--class os --unrestricted/" filename

This will replace the line in the file without requiring a temp file, due to the -i option.

If it doesn't matter where in the line the "--unrestricted goes", this might be simpler.

sed -i "s/'Ubuntu'/'Ubuntu' --unrestricted/" filename
Related Question