Get to know Linux: Grub2 - gHacks Tech News

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"

Configuring Grub

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:
GRUB_DEFAULT=0

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:

sudo 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.

Final thoughts

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.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. jackd said on August 3, 2010 at 5:12 am
      Reply

      “Look through your Grub configuration file for the kernel line you want to be the default entry.”

      I don’t think you really explained which file that is? I assume that’s /boot/grub/grub.cfg?

    2. BalaC said on August 3, 2010 at 5:13 am
      Reply

      I would be happy to see a tutorial on installing grub in case it gets corrupted without installing the whole distribution.

    3. Dirty Sanchez said on August 3, 2010 at 5:33 am
      Reply

      I hate grub2. It is not compatible with any of the major distributions. Only idiot Ubuntu and anything based on Ubuntu uses Grub2.

    4. Dino said on August 3, 2010 at 6:40 am
      Reply

      @BalaC

      That is How you install grub in Redhat Linux

      1. Boot In Rescue Mode
      2. Mount Partition on /mnt/sysimage if not mounted automatically
      3. Chroot /mnt/sysimage
      4. grub-install /dev/hda
      Bingo

    5. Anonymous said on August 3, 2010 at 3:50 pm
      Reply

      lol, can’t stand critic?
      you are so pathetic.

    Leave a Reply