Ubuntu – Seeking for a better solution to restrict access in GRUB2 menu


I just read that in certain situations you should also protect access to your GRUB2 menu by setting a password and may be refining acces by adding --unrestricted or --users as arguments to menuentries und submenus.

I read the corresponding pages in the Ubuntu Community Documentation and the Arch Wiki. So, I created /etc/grub.d/01_security, stored usernames and passwords in there, made the file executable and ran update-grub. This is working as intended, every action in the menu prompts for username and password, but I also want to modify the automatically generated entries to either restrict them to certain users (via --users) or make them available for everyone, but not editable by everyone (via --unrestricted).

I was able to find the proper lines in 10_linux and edit them accordingly, however I'd love to see an easier solution. Perhaps an option like GRUB_DISABLE_RECOVERY="true" or GRUB_DISABLE_OS_PROBER=true in /etc/default/grub for easy (re)configuration (for linux and os-prober generated entries).

Here's a diff from my 13.10 installation:

$ diff /etc/grub.d/10_linux /etc/grub.d/10_linux_bak
<       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} --unrestriced \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^$
>       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_inde$
<       echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} --unrestricted \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_$
>       echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
<     echo "submenu --unrestricted '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_$
>     echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"

tl;dr: I'd love the see a simple solution for GRUB2 entries that cannot be modified without a password or are limited to certain users. (Yes, GRUB_DISABLE_RECOVERY="true" is active.)

Best Answer

Okay I found that variables in /etc/default/grub are read and exported by grub-mkconfig in /usr/sbin/. The following patch is what I had in mind and hopefully complies with quality requirements and coding standards for the files modified. Patch for 30_os-prober should be similar and hopefully follows tomorrow.

As explained above the variables should be defined in /etc/default/grub like so GRUB_PWRESTRICTION_LINUX="--users user1" or so GRUB_PWRESTRICTION_LINUX="--unrestricted".

I'm looking forward to your feedback.

--- /usr/sbin/grub-mkconfig-orig    2013-10-19 11:22:56.653129020 +0200
+++ /usr/sbin/grub-mkconfig 2013-10-19 11:49:34.961392887 +0200
@@ -230,6 +230,12 @@

 if test "x${grub_cfg}" != "x"; then
   rm -f "${grub_cfg}.new"
--- /etc/grub.d/10_linux-orig   2013-10-19 23:28:50.195071600 +0200
+++ /etc/grub.d/10_linux    2013-10-19 23:40:17.429375336 +0200
@@ -120,9 +120,17 @@
      title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
      grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
-      echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
+      if [ x"${GRUB_PWRESTRICTION_LINUX}" != x ]; then
+        echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} "${GRUB_PWRESTRICTION_LINUX}" \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
+      else
+        echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
+      fi
-      echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
+      if [ x"${GRUB_PWRESTRICTION_LINUX}" != x ]; then
+        echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} "${GRUB_PWRESTRICTION_LINUX}" \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
+      else
+        echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
+      fi
   echo "recordfail" | sed "s/^/$submenu_indentation/"
   if [ x$type != xrecovery ] ; then
@@ -320,7 +328,11 @@
    boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
     # TRANSLATORS: %s is replaced with an OS name
-    echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
+    if [ x"${GRUB_PWRESTRICTION_LINUX}" != x ]; then
+      echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' "${GRUB_PWRESTRICTION_LINUX}" \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
+    else
+      echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
+    fi

   linux_entry "${OS}" "${version}" advanced \