4 Essential Linux Boot Commands
Linux systems administrators need to understand how the Linux boot process works. Though the bootloader is typically configured at the time of installing Linux automatically, understanding the boot process will help systems administrators know how to recover from boot failures. Likewise, if you are brave enough to attempt to install a distribution like Gentoo or Arch from scratch, you'll need to configure the Linux bootloader by hand. Here are four essential Linux boot commands every sysadmin needs to know.
Mkinitrd: How To Create The Ramdisk That Linux Boots From
Let's set a couple of things straight here. Technically, there are boot commands and startup commands, though both are typically referred to as boot commands. There is a difference, though. The boot commands are what the MBR calls to start the bootloader along with the appropriate files the boot loader needs to start Linux. The startup commands are what the bootloader uses to start things like the Linux kernel and essential kernel modules. New to Linux commands, here's a quickstart guide for you.
Both boot and startup commands are essential Linux boot commands. Without any of them, the Linux OS won't load properly, but in this section, we're going to look at the boot process specifically.
When the Linux bootloader loads, it mounts and uses a mini filesystem. That file system is completely ram-based. It contains the basic kernel components and commands required to let the bootloader start using the startup commands. That mini ram-based file system is called the initrd or initramfs.
That's where the mkinitrd command is used. The mkinitrd command rebuilds that ram-based file system for the bootloader.
Why would you want to use mkinitrd?
There are a few reasons you would want to use the mkinitrd commands.
The boot parameters of the Linux OS changed for some reason. This could be because you installed a new kernel or installed a new piece of hardware that requires the kernel or kernel modules to be rebuilt. Either way, if there are changes to the system that would affect how the Linux OS is booted, the ramdisk needs to know about it.
The Linux OS isn't booting properly. This could be for a variety of reasons. For instance, let's say you updated the kernel and that update did not go well. You could boot Linux to a safe environment and rebuild the initrd ramdisk to target an older version of the Linux kernel that did work.
How to use mkinitrd
It's highly suggested that you double-check the manual for the mkinitrd command for the Linux distribution you are using. For instance, in general, you will want to use the command followed by the flag to overwrite the existing initrd image followed by the name of both the old and new image.
EG. mkinitrd -f /boot/initrd-$(uname -r).img $(uname -r)
The '-f' flag tells the mkinitrd command to overwrite the existing initdrd ram file. However, this does not work the same for all Linux distributions. For example, the '-f' flag will work as expected with REHL but not CentOS. In CentOS, that flag lists the existing files instead. So, make sure to use the 'man' command for the mkinitrd command first and read through its documentation for your specific flavor of Linux.
Dracut: When Mkinitrd Just Won't Cut It
Technically the mkinitrd command is nothing more than a wrapper for the Dracut command. So, why would you need to know about this command?
Well, it's common to use one computer to fix issues with another one, correct? That's when you'll use the Dracut command.
Both that mkinitrd and dracut commands achieve the same purpose. Both re-create the initrd ram-based file for the Linux bootloader. The Dracut command is a little more versatile, though.
The mkinitrd command can be used if you are re-building the initrd file on the same computer having boot issues. If you are rebuilding that file for a different computer, though, use the dracut command.
EG. Dracut –force —no-hostonly
The command above will rebuild the initrd file with all of the currently installed Linux kernel modules and not just the ones required for booting. This is useful when you need to rebuild the initrd file for another computer system but are not sure which kernel modules it needs to boot.
Once a system is booted and is in working order, the initrd file can be re-built again with only the kernel modules required to boot the system by omitting the '—no-hostonly' flag from the command example above.
If you are using the dracut command to recover a PC that is no longer booting, make sure to either use a live DVD for the Linux distribution installed on the non-operating computer or a computer using the same Linux OS and hardware profile as the non-working PC. The Dracut command uses the currently installed kernel and kernel modules to rebuild the initrd file, so you'll want to make sure the same kernel and modules are being used to create that file.
In general, though, you'll use the Dracut command whenever you need more fine-grained control over the initrd creation process. The mkinitrd command is a little more limited with what it can do but is easier to use. That makes the Dracut command an essential Linux boot command that all Linux sys-admins need to understand.
Grub2-install: The Command That Installs The Most Popular Linux Bootloader
Grub2 has become the defacto bootloader for most Linux distributions today. Just about all major Linux distributions, including Red Hat and Ubuntu, have moved to use Grub2 as the default bootloader. Though Grub2 is typically configured at the time the Linux OS is installed, it's important to understand how to reinstall it.
Why would you want to re-install the Grub2 bootloader?
There are a bunch of reasons where reinstalling the Grub2 bootloader is required. The most common is system recovery. For example, if the Linux OS attempts to update the kernel but has an issue, systems administrators may need to use an older version of the Linux kernel to get the system operational again. In this example, the Grub2 bootloader needs to be re-written and told to use, or at the minimum, give an option to use that older kernel to boot the system with.
Another time systems administrators may want to update the Grub2 bootloader is when hardware changes are made to the computer. Let's say a larger storage drive is being installed on the computer. The old storage drive was cloned to the new, larger drive. That new drive has a different drive designation in the system, though. The Linux OS referenced the old drive as sda1 while the new drive is referenced at sdb1 instead. In this case, the Grub2 bootloader needs to be told where the files are that it needs to start the Linux OS.
How to install the Grub2 bootloader
The command below will reference the Fedora Linux operating system. Though the usage of the Grub2 install command is generally the same, each Linux distribution might store its boot files in different locations. You will need to reference the documentation for the Linux distro that you are using for the location of the boot files.
The Grub2 install process is a two-part procedure. The first step is to create the configuration files for Grub2, then the bootloader itself needs to be installed.
EG. grub2-install /dev/sda
The '/dev/sda' part of that command designates the primary hard disk of the system Grub2 is being installed on. Use the 'lsblk' command to list the drives in your system and to find out which one is the primary drive. If you are installing Grub2 on a UEFI-based system, you'll need to make sure there is a FAT32 file partition at the beginning of that drive that is at least 128MB in size first.
Grub2-mkconfig: When Installing The Bootloader Isn't Enough
Before re-installing the Grub2 bootloader, it may be necessary to remake its configuration files. This could be for a variety of reasons. For instance, if you change the hardware in your computer, the configuration files for the bootloader might need to know where a new drive is located or which new kernel modules it needs to access for the boot procedure. Sometimes, especially in the case of a kernel panic, the Grub2 bootloader may not need to be re-installed, but instead, the config files for Grub only need to be re-created.
Remaking the Grub2 boot configuration files is easy.
Eg. grub2-mkconfig -o /boot/grub2/grub.cfg
The last part of that command string tells the grub2-mkconfig command where to place the Grub2 config files. Though that drive path is typical for most Linux distributions, it may be required to check the documentation for the Linux distro that you are using. Not all Linux OSes store the boot files in that location.