Working on the GRUB console

When presented with the GRUB menu, as well as selecting the entry we wish to boot, we can either edit existing entries or shell out to the GRUB console. Working on the GRUB console enables us to enter our own sets of commands. Remember the trilogy that should accompany each stanza:

  • root
  • kernel
  • initrd

We can enter these commands, but also reinstall GRUB if required. More simply, in the console, we can also edit or append to the exiting entries; using the e key, we can edit an entry, and a can be used to append an option to the kernel line. From the following screenshot, we can view these options:

Editing the kernel arguments allows you to specify the runlevel target to boot to; using this method, it is possible to reset the password of the root user.

To recover a forgotten root password, we can boot the system to runlevel 1; by default, this will log you in directly as root.

  1. Firstly, we must select the entry in the menu to boot to. If there is more than one, do not use the Enter key.
  2. With the menu entry highlighted, choose the letter a.
  3. This will take you directly to the end of the kernel line where you can add the number 1 to boot to runlevel 1.

    Note

    It is important to note that CentOS System Administration Essentials assumes that no prior runlevel has already been specified in the kernel arguments.

With the number added, just hit the Enter key, and the system will boot to the single user mode and logged in as root. Once the system has been booted, you can effectively change a password using the passwd command.

It is possible to prevent this behavior; we have to be cautious to avoiding the prevention genuine recovery mechanisms of our server. If there is enough physical protection of the server, then perhaps we do not need to make any changes. However, if we cannot ensure physical security of the server, we can edit the /etc/sysconfig/init file by changing the SINGLE=/sbin/sushell line to the following:

SINGLE=/sbin/sulogin

The sulogin command will prompt for the root user's password.

Tip

If sulogin has been set and you still need emergency access as root, it is possible by specifying init=/bin/bash instead of 1 as the runlevel.

If our boot situation is a little more serious, or in human terms, it won't boot, then we can enter the GRUB command prompt using the option c. Using the command help, we can determine what commands are available from the minimal shell. To reinstall GRUB with the correct drivers to access the boot partition, execute the following command:

grub> setup(hd0)

The preceding command will check to see if /boot/grub/stage1 or /grub/stage1 exists on the bootable partition. This way, it determines which partition to use as root and copies the stage1 file to the MBR complete with the drivers needed to access the bootable partition. We can then choose to restart the system with the reboot command.

Not only can we use the GRUB console to repair GRUB, we can use it to boot the system and verify the menu items. By specifying the root filesystem to be used for booting, we can check the path required to access the kernel and initrd. We can use the normal tab completion on the GRUB shell to see directories and filenames.