Get to know Linux: Grub2
Most PC users have no idea what a boot loader is. Yet all PCs have them. Most Linux users, by nature, do know what a boot loader is because they have had to deal with them one on one for a long time. Of course times have changed and gone are the days when you really had to monkey around with your boot loader. Back when I first started working with Linux, the boot loader was lilo (Linux Loader). Then I migrated to a different distribution and was using Grub. Now, Grub has moved to Grub2 and things are going swimmingly. So swimmingly in fact that I rarely have to even think about using the boot loader for anything - other than to let it do its thing.
But there are times when that trust old application needs to have a bit of a tweaking and when that time comes, you're going to need to know exactly what to tweak and what to do post-tweak. In this Ghacks article I am going to introduce you to some of the fundamentals of the Grub2 bootloader.
What the bootloader does
In a nutshell, the bootloader instructs the kernel how it is supposed to boot. Without the bootloader the kernel wouldn't get it's initialization instructions (which kernel is default, any switches that are passed at startup, etc). Of course this is a vast over-simplification of what the boot loader does, but for the newer user, it's explanation enough.
Handy Grub switches
There are plenty of Grub command line switches that you can use upon boot. But first you have to get to the Grub command prompt. To do this hit the "e" key when you see the kernel listing at boot followed by <Ctrl>x. This will land you in the Grub command prompt, at which point you can run any of the following:
- grub> CTRL + X: Boots any arguments you enter into the commandline.
- grub> halt: Shuts down the computer
- grub> reboot: Reboot the computer
- grub> help: :ists all available commands (which is quite a few)
- grub> help a: Lists all available commands that start with the letter a
- grub> ls: Lists all available partitions on your disk(s)
- grub>ls (hdx,x): Lists all files on that partition
- grub>cat (hdx,x)file: Outputs the contents of "file"
I am going to demonstrate how to make a change to Grub and then make that change take effect. There are a few files that might seem like the one you want to edit. The real file you should edit is /etc/default/grub. When you open this file take notice of the following entry:
That line instructs which kernel is the default to boot. You may find any number of entries in your Grub configuration file. For each kernel version you will find a standard entry and a recovery mode entry. The are numbered 0,1,2,3,4, etc. Look through your Grub configuration file for the kernel line you want to be the default entry. Remember, numbering starts at 0 not 1. So the third entry will actually be 2. Change that GRUB_DEFAULT= line to reflect the entry you want to serve as the default, save and close the file, and then run the following command update Grub:
Now when you reboot your machine, it will default to the kernel you have chosen as default. This is always helpful when you are experimenting with kernels or you get an upgraded kernel that breaks a feature.
The boot loader for your machine is crucial. Take good care of this tool and it will do the same for you. But do use caution when undertaking any task that involves the boot loader, else you render your machine unbootable. Of course Grub is much more than what you have been shown here. We'll get into more about this powerful boot loader later.Advertisement