Configuring your boot environment

GRUB2 is the default boot loader for RHEL 7. By default, it doesn't use any fancy configuration options, but it is wise to at least secure your grub boot loader.

How to do it…

There are many advantages to having your grub and boot environment output to serial console in an enterprise environment. Many vendors integrate virtual serial ports in their remote control systems, as does KVM. This allows you to connect to the serial port and easily grab whatever is displayed in a text editor.

Setting a password on the GRUB2 boot loader mitigates possible hacking attempts on your system when you have physical access to the server or console. Perform the following steps for this recipe:

  1. First, edit /etc/sysconfig/grub with your favorite editor.
  2. Now, modify the GRUB_TERMINAL_OUTPUT line to include both console and serial access by executing the following command line:
    GRUB_TERMINAL_OUTPUT="console serial"
  3. Add the GRUB_SERIAL_COMMAND entry, as follows:
    GRUB_SERIAL_COMMAND="serial --speed=9600 --unit=0 --word=8 --parity=no –stop=1"
  4. Now, save the file.
  5. Create the /etc/grub.d/01_users file with the following contents:
    cat << EOF
    set superusers="root"
    password root SuperSecretPassword
    EOF
    
  6. Next, update your grub configuration by running the following commands:
    ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-3.10.0-229.4.2.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-229.4.2.el7.x86_64.img
    Found linux image: /boot/vmlinuz-3.10.0-229.1.2.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-229.1.2.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-fe045089e49942cb97db675892395bc8
    Found initrd image: /boot/initramfs-0-rescue-fe045089e49942cb97db675892395bc8.img
    done
    ~]#
    

How it works…

The behavior of grub2-mkconfig is defined by the directives of the files in /etc/grub.d. These files, based on the configuration in /etc/sysconfig/grub, autogenerate all the menu entries in the grub.cfg file. You can modify its behavior by adding files with bash code in this directory.

For instance, you could add a script that would add a menu entry to boot from the CD/DVD ROM drive.

The user root, which is added to /etc/grub.d/01_users, is the only one allowed to edit menu entries from the console, mitigating the weakness in GRUB to force rescue mode by adding 1 or rescue at the end of the kernel line.

There's more…

The grub2-mkconfig command is specific for BIOS-based systems. In order to do the same on UEFI systems, modify the command as follows:

~]# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

In order to access the GRUB terminal over the same serial connection, you need to specify an additional kernel option: console=ttyS0,9600n8.

You can either modify the kernel lines in /boot/grub2/grub.cfg (or /boot/efi/EFI/redhat/grub.cfg manually, but you do risk losing the change when your kernel is updated), or manually regenerate the file using grub2-mkconfig.

It's best to add it to the GRUB_CMDLINE_LINUX directive in /etc/sysconfig/grub and regenerate your grub.cfg file.

Passwords for GRUB users can be encrypted using the grub2-mkpasswd-pbkdf2 command, as follows:

~]# grub2-mkpasswd-pbkdf2
Enter password:
Reenter password:
PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.C208DD5E318B1D6477C4E51035649C197411259C214D0B83E3E83753AD58F7676B62CDF48E31AF0E739844A5CF9A95F76AF5008AF340336DB50ECA23906ECC13.9D20A66F0CADA12AA617B293B5BBF7AAD44423ECA513F302FEBF5CB92A0DC54436E16D7CD6E09685323084A27462C2A981054D52F452F5C2F71FBACD2C31AEFA
~]#

Then, you can substitute the clear text password in /etc/grub.d/01_users with the generated hash. Here's an example:

password root grub.pbkdf2.sha512.10000.C208DD5E318B1D6477C4E51035649C197411259C214D0B83E3E83753AD58F7676B62CDF48E31AF0E739844A5CF9A95F76AF5008AF340336DB50ECA23906ECC13.9D20A66F0CADA12AA617B293B5BBF7AAD44423ECA513F302FEBF5CB92A0DC54436E16D7CD6E09685323084A27462C2A981054D52F452F5C2F71FBACD2C31AEFA

All the entries that are automatically generated are bootable but not editable from the console, unless you know the user and password. If you have custom menu entries and want to protect them in a similar way, add --unrestricted to the menu entry definition before the accolades. Here's an example:

menuentry 'My custom grub boot entry' <options> --unrestricted {
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset