Linux Commands for GRUB2

This web-page is part of a larger site giving examples of how to install Windows+Ubuntu Linux operating systems 'dual boot' in a computer.  Illustrated Dual Boot HomePage

Edited Tuesday, October 05 2010  Document made with KompoZer

GNU GRUB is a work in progress and the information in this website is incomplete and may be wrong and/or out of date. Please consult the official GNU GRUB 1.98-r2692 manual.


Page Index
By commands

grub-install - Install or refresh GRUB2, runs other commands

grub -mkconfig  - make a new grub.cfg file

grub-set-default - tell GRUB which OS title you want to be highlighted by default

grub-mkrescue - makes a bootable rescue CD or floppy disk image

grub-setup  - install the boot.img to a disk or partition

grub-mkfont - converts fonts for use by GRUB  - Karmic Koala and later

grub-mkdevicemap  - makes a new device.map file 

grub-mkimage  - makes a boot image -used by grub-install and grub-mkrescue

grub-probe  - a program to probe for device information, run by scripts


By How-To

How To Check Installed GRUB Version Number - with grub-install

How to install or refresh GRUB - with grub-install

How to make a Dedicated GRUB Partition - with grub-install

How to make a GRUB USB - with grub-install

How To Re-install GRUB from Live CD - with grub-setup

How to make a bootable GRUB2 CD-ROM - with grub-mkrescue

How to make a bootable GRUB2 Floppy Disc - with grub-mkrescue

How to Update the GRUB Menu - by running update-grub

How to Update the GRUB Menu - by running grub-mkconfig

How To Set The Default Boot Entry for GRUB - with grub-set-default

How To Install GRUB to a Partition Boot Sector - with grub-setup

How to chroot - use a Live CD or another Ubuntu installed in a hard disk or USB to fix your GRUB






grub-install - usr/sbin/grub-install

The grub-install command runs a script called usr/sbin/grub-install and is mainly used for writing GRUB to a device such as a hard disk drive, SSD or flash memory drive.

The grub-install command
  • creates a /boot/grub directory if needed,
  • copies GRUB *.mod,  *.lst and  *.img files files from /usr/lib/grub/i386-pc/ into the /boot/grub/ directory, to replace any missing files or overwrite existing files with fresh copies in case any of those files might have been corrupted somehow,
  • runs grub-probe to scan the computer and gather disc and partition information,
  • runs grub-mkimage which builds a new core.img,
  • calls grub_setup to write the grub boot.img to MBR and embeds the core.img to the first track of the device.
NOTE: The grub-install command does not run grub-mkconfig, which is the other important command that most people will want to know.  Refer to grub -mkconfig.

Run 'man grub-install' in a terminal in any Ubuntu operating system for more information.

NOTE: You need to have the operating system booted to run this command because it will only work from inside the running operating system.
If you can't get your operating system to boot the regular way, but you can get at least get GRUB, then you should be able to get it to boot by using GRUB2 CLI Mode Commands.
If you still can't get the operating system to boot up at all  you probably need to boot a Live CD and chroot into your operating system to run this command. How to chroot - use a Live CD or another Ubuntu installed in a hard disk or USB to fix your GRUB

EXAMPLES BELOW

How To Check Installed GRUB Version Number


grub-install -v



How to install or refresh GRUB

sudo grub-install /dev/sda
Where: /dev/sda is the disk you wish to install GRUB to, (boot.img to first hard disk MBR), otherwise use '/dev/sdb' for second hard disk or '/dev/sdc' for third hard disk.

Normally, most people will want to install GRUB to MBR in the first hard disk if Ubuntu is installed in a so-called 'internal' disk inside a computer. By an 'internal' disk I mean a disk that is more or less 'permanently' connected to the motherboard by IDE ribbon cables or SATA cables.
If your computer has more than one hard disk and you're not sure which hard disk's MBR you want to install GRUB2 to, it might be best to install GRUB to all of your disks, just to make sure.

If you have Ubuntu installed in a so-called 'removable' disk, such as a USB 'external' drive, or any disk that you intend to remove from the computer, you should probably install GRUB to the MBR of the same disk you have Ubuntu installed in only, meaning your USB external drive.

If you want to install GRUB2 to the boot sector of a partition for some strange reason, you may use something like /dev/sda1 or /dev/sda2 for writing GRUB's boot.img to a partition boot sector. The practice of installing GRUB2 to partition boot sectors is not encouraged. It reduces GRUB's reliability and it could be dangerous to other operating systems if users use the grub-install command carelessly or ill-informed and write GRUB to the wrong boot sector.

You need to have the operating system booted first, before you can use the command.
If the operating system you're trying to fix won't boot you need to chroot into it from another operating system such as a Live CD operating system before you can run grub-install. In that situation it might be easier to use grub-setup instead, please refer to How To Re-install GRUB from Live CD - with grub-setup.

The grub-install command doesn't run grub-mkconfig (or update-grub), so the user still needs to run one of those commands if a new grub.cfg is wanted. See grub -mkconfig  - make a new grub.cfg file.







How to make a Dedicated GRUB Partition

Why would anyone want a 'Dedicated GRUB 2 Partition?

When GRUB 2 is in its own dedicated partition it is 'operating system independant', so we can add or remove one or two operating systems without the inconvenience of losing the boot of the remaining operating systems.
Those of us who are multi-booting with more that two operating systems in their computer like to be able to set their own user friendly names for their operating systems.
It's okay to edit the grub.cfg directly when GRUB 2 isn't part of an operating system, making it easier to do what we like with GRUB and have fun with it and learn more GRUB commands and tricks.
  1. Choose an existing partition or create a new one and format it with a file system, you will need at least about 60 MiB of space in the partition for grub 2 files, but a little more room than that might be advisable.
  2. Format the partition with a file system and optionally give the file system a FILE SYSTEM LABEL.
  3. Mount the partition by clicking on its icon in the 'Places' menu.
  4. Run a grub-install command similar to the one shown below, 
    sudo grub-install --root-directory=/media/grub2 /dev/sda
Where: '/media/grub2' is the mount point for the file system I want to have GRUB files created in
Where:  I want to make a new /boot/grub directory and fill it with GRUB files.
Where: '/dev/sda' is the hard disk in which I want to write the stage1 code to MBR in

That command creates a new /boot and /boot/grub/ directory if one doesn't already exist, and creates or refreshes GRUB files in /boot/grub, all except for grub.cfg.
If you don't make a grub.cfg then  there will be no GRUB Menu and your computer will boot to a GRUB Command Line Interface,  see GRUB2 How To Boot From CLI Mode

If you want a GRUB Menu you need to make a grub.cfg file and copy it to /boot/grub.
You can make your own grub.cfg file and customize it in any way you like.
The proper way to do it would be to use the grub-mkconfig command, and alter the file path to make it point to the 'Dedicated'  /boot/grub/'. See How to Update the GRUB Menu .
Another way would be to make your own grub.cfg, if you need an example to help you get started, look here, grub.cfg - Grub Wiki.
Or, copy and paste this one, grub.cfg, to a blank text file, name it grub.cfg, edit it to your needs and paste it in your Dedicated /boot/grub.
Later, you can also make and add other files such as background images.

I had trouble getting my 'Dedicated GRUB 2' to display any background image at first.
I was getting 'error: No video mode activated', after trying the background_image command in CLI Mode.
I needed make a fonts folder in my 'Dedicated GRUB 2 Partition and copy the fonts from /usr/share/grub/unicode.pff and /usr/share/grub/ascii.pff into it. Then I had to correct the fonts path in my grub.cfg header area,
### BEGIN /etc/grub.d/00_header ###
set default=0
set timeout=5
set root=(hd0,2)
if font (hd0,2)/fonts/unicode.pff ; then
  set gfxmode=640x480
  insmod gfxterm
  insmod vbe
  terminal gfxterm
fi
### END /etc/grub.d/00_header ###

After that I was able to display my splashimage in my 'Dedicated GRUB 2 Partition'.

to do (Herman) ... revise and this how-to more user freindly.





How to make a GRUB_II USB


GRUB 2 could be very handy in a USB. You can use it for an emergency boot disk.
You can play with it and use it for  doing interesting experiments with to learn more about GRUB without compromising your real GRUB. It's okay to edit the grub.cfg directly when GRUB 2 isn't part of an operating system.

1. Choose an USB drive with at least about 60 MiB of space in the partition for grub 2 files, but a little more room than that might be advisable. It can be formated with any file system, it doesn't mattter if there are already some files in it.

2. Plug in your USB drive  while Ubuntu is running, it should be automatically mounted in a few seconds and an icon should appear for it on your desktop.

3.  Find out what is the real mount point name, (if you don't already know), this step is important because the actual mount point in /media can have another name than the name you see for the icon on your Desktop.
Code:
ls /media/

example output
cdrom  cdrom0  Verbatim
The resulting feedback shows that my USB drive's file system is mounted as /media/Verbatim, because it has 'Verbatim' set as the freindly file system label.
If yours comes up as a file system UUID number, I recommend you set your own user freindly file system label to make things easier for yourself now and in the future. How To Set File System Labels With GParted.

5. Find out what is the /dev/number for the file system (or partition),
sudo blkid

example output
sudo blkid
/dev/sda1: LABEL="ACER" UUID="320D-180E" TYPE="vfat"
/dev/sda2: LABEL="ACERDATA" UUID="04A6-B8C3" TYPE="vfat"
/dev/sda5: LABEL="KARMIC" UUID="53640b23-7f97-46a7-b939-b5aedd5097c1" TYPE="ext4"
/dev/sda6: UUID="88115692-4578-47b5-89ae-dcc3720d95e4" TYPE="swap"
/dev/sdb1: LABEL="Verbatim" UUID="1fdc26cf-43ad-40b9-95fa-6d64196513f2" TYPE="reiserfs"
Here you can see that the target file system in my USB disk is called 'Verbatim' and it is listed as /dev/sdb1.

6. Run grub-install ,
sudo grub-install --root-directory=/media/verbatim /dev/sdb
Where: the mount point is /media/verbatim
Where: The MBR to install GRUB to is /dev/sdb (the USB drive).
NOTE: I didn't install GRUB to /dev/sdb1, because that's the partition boot sector. I need GRUB in the USB disk's MBR, which is called /dev/sdb in this case, since the partition is /dev/sdb1.

Watch the thumb drive's LED blink for a few seconds.

Now you have a bootable GRUB 2 USB Rescue disk.

But it has no grub.cfg

7. Run grub-mkconfig
sudo grub-mkconfig -o /media/Verbatim/boot/grub/grub.cfg

8. Relax the file permissions to make it easily editable from GUI mode, (user freindly).
Let's make it editable so we can play with it.
sudo chmod 777 -R /media/verbatim /boot

Now we can open it in GUI mode even if we want and edit the grub.cfg file to our heart's content.


Links:
Use cases - Ubuntu 8.10 on USB device - GRUB Wiki

How to add GRUB to your USB thumb drive - (GRUB Legacy)





update-grub

The update-grub command runs a script called /usr/sbin/update-grub ,which just runs another script called /usr/sbin/grub-mkconfig, see grub-mkconfig, (the next item, below).

Here's what /usr/sbin/update-grub looks like,
#!/bin/sh -e
exec grub-mkconfig -o /boot/grub/grub.cfg "$@"
See? All it does is run grub-mkconfig with the standard option.

The only reason this command still exists is because it's easy for new users to remember and it's simple. I think running a command that doesn't have any options is boring. My advice is to skip this command and start getting used to using the grub-mkconfig command instead. It's not a big step and only requires a wee bit more intelligence, you can do it if you try!



grub-mkconfig

The grub-mkconfig command runs a script called /usr/sbin/grub-mkconfig, which runs other scripts and programs such as grub-mkdevice.map and grub-probe and then generates a new grub.cfg file.

As far as I know this command was supposed to replace update-grub in Karmic Koala and later versions of Ubuntu. This command is more versatile than update-grub because we can use the -o option to have the results printed to a any filename and file path we like. That's a useful feature.

Examples,

sudo grub-mkconfig 
The grub-config command by itself doesn't do anything much. It prints the results to stdout, (displays the results in the terminal screen), and you can see what it would have wrtten to a file if you had used grub-mkconfig with the -o option.

You need to use the -o option and specify a filepath and file for grub-mkconfig to write to if you want grub-mkconfig to do something useful,
sudo grub-mkconfig -o /boot/grub/grub.cfg
When you you use the -o option, you can write the changes to a file.
You can make up any file path and file name here, but most of the time people will probably want to write to their /boot/grub/grub.cfg if they are serious about actually updating the file.

We could use the -o option to direct the output of grub-mkconfig to some other file if we want to do something interesting with GRUB. We could have more grub.cfg files by making new ones with names other than 'grub.cfg'.
For example, we could have /boot/grub/grub.cfg and /boot/grub/grub.cfg2 and /boot/grub/grub.cfg3 and so on if we wanted to.
We might want to do that and use the 'configfile' command to change from one GRUB menu to another at boot time. The configfile commands can be added as a custom command in 40_custom, or any other script we like.

For another example, we might want to direct our /boot/grub/grub.cfg to some other mounted partition, such as a 'Dedicated GRUB Partition', or GRUB in a USB drive. We can do that just by changing the file path after the -o option.




grub-set-default
- Script to set a default boot entry for GRUB.

The grub-set-default command runs a script in /usr/sbin called grub-set-default.
That script sets the boot entry in the grub menu to be selected by default and to be booted automatically if no keys are pressed and the timer is allowed to run out.

Before that will work, the top line in the /etc/default/grub file that looks like 'GRUB_DEFAULT=0' needs to be changed to 'GRUB_DEFAULT=saved'. See Editing /etc/default/grub. Make sure you remember to run 'sudo grub-mkconfig -o /boot/grub/grub.cfg after editing yout /etc/default/grub file, for the changes to take effect.

From now on you can run 'sudo grub-set-default' any time you like,
sudo grub-set-default 2






grub-mkrescue

The grub-mkrescue command runs a script called usr/bin/grub-mkrescue. This can make either a bootable floppy disc image or run genisoimage to make an .iso file for burning to disc to make a bootable rescue CD.

Bootable cdrom images use cdboot.img loader, which are capable of accessing the ISO9990 cdrom to load extra modules (see the (8) genisoimage -b option).


How To Make Your Own GRUB2RESCUE CD-ROM

1st example for grub-mkrescue,  (Karmic Koala and earlier),
grub-mkrescue --overlay=/boot/grub GRUB2RESCUE.iso
Where: I want to include files such as my own personalised grub.cfg from /boot/grub
Where: I want the program to name the file 'GRUB2CD.iso'
  • if you're making a CD, you don't have to specify the 'type', because CD is the default
  • --overlay=DIR would be an important option if you want a menu based GRUB2 CD. You can make an .iso file with your own familiar GRUB Menu in it. If you don't want to do that you might instead want to make up a special grub.cfg for some special purpose or experiment you want to try. If you don't use this option you'll still have a bootable GRUB2 CD, but it will only have the Command Line Interface, see GRUB2 CLI Mode Commands.
Lucid Lynx version of grub-mkrescue, (GNU GRUB 1.98-1ubuntu5), thanks to moon_raker for letting me know.
grub-mkrescue --output=rescue.iso /boot/grub
  • for  the --output=rescue.iso part of the command, you can replace the word 'rescue' with any name you  like for your new .iso file.
  • in the /boot/grub part of the command, you can specify some other file path if you are creating your own GRUB2 CD in some other directory, but for most people, /boot/grub will probably be right.

Expected Results -
  1. The grub-mkrescue program should make an .iso file in your /home/username directory with your own operating system's GRUB files inside it.
  2. You need to burn the .iso file to a blank CD as an .iso file, not as data, so it will be bootable.
  3. Your GRUB2RESCUE CD should boot to a GNU GRUB Command Line, see GRUB2 How To Boot From CLI Mode -  NEW!  Rescue your System  
  4. To see your own personalised GRUB Menu, just run the command: configfile /grub.cfg
Also see How To Boot From CLI Mode - Rescue your Operating System.



How To Make Your Own GRUB2 Floppy Disc
The grub-mkrescue command for making a floppy disc is the same as the command for making the .iso file but you need to add the option: --image-type=floppy.
Probably it would be best to use a different filename extension too.
grub-mkrescue --overlay=/boot/grub --image-type=floppy grub_two.dsk
This should make you a new floppy disc image file.

dd if=grub_two.dsk of=/dev/fd0 bs=512 count=2880
This is how to copy your floppy disk file to your floppy disk.

example thread: Installing grub2 bootloader in fd0

The old fashioned way to make a GRUB CD was to copy GRUB files to a folder and then run the genisoimage command on your folder to create and .iso image. After that you can burn the .iso image to disc.
Link:
How to make your own personalized GRUB CD-RW - (GRUB Legacy)

Link:
How to make your own personalized GRUB Floppy Disk.- (GRUB Legacy)

We can still probably use the old method with GRUB 2 if we want to, but the grub-mkrescue command does it a lot easier and faster and with far fewer commands than the old way of doing things.
The grub-mkimage command gets the thumbs up from me.

GRUB2 How To Boot From CLI Mode -  NEW!  Rescue your System 




grub-setup
- usr/sbin/grub-setup.exe  (writes images to device, used by grub-install)

grub-setup [device]  

Either install the boot.img file to a MBR plus the core.img install just   to the sectors after the MBR, or the boot.img file to a  partition boot sector.

NAME
       grub-setup - manual page for grub-setup (GRUB) 1.96

SYNOPSIS
       grub-setup [OPTION]... DEVICE

DESCRIPTION
       Set up images to boot from DEVICE.  DEVICE must be a GRUB device (e.g. ‘‘(hd0,1)’’).

       -b, --boot-image=FILE
              use FILE as the boot image [default=boot.img]

       -c, --core-image=FILE
              use FILE as the core image [default=core.img]

       -d, --directory=DIR
              use GRUB files in the directory DIR [default=/boot/grub]

       -m, --device-map=FILE
              use FILE as the device map [default=/boot/grub/device.map]

       -r, --root-device=DEV
              use DEV as the root device [default=guessed]

       -h, --help
              display this message and exit

       -V, --version
              print version information and exit

       -v, --verbose
              print verbose messages

(8)grub-setup

Examples,


How To Re-install GRUB

The following two commands will work from withing a running Ubuntu operating system
grub-setup /dev/sdb
- this example installs the IPL for GRUB to the second hard disk's MBR  


How To Install GRUB to a Partition Boot Sector
grub-setup --force /dev/sda2
- this example installs /boot/grub/boot.img  to the boot sector of /dev/sda2
(The GRUB from the operating system you're working in will be installed to /dev/sda2).
NOTE: GRUB 1.96 didn't seem to like having it's IPL installed to a partition boot sector, it complained and required the use of --force to get it to work.

TIP: If you can't boot Ubuntu in the normal way you can  use your GRUB2RESCUE CD and see the following link,  GRUB2 How To Boot From CLI Mode -  NEW!  Rescue your System.
Alternatively, you could  use your Ubuntu Live CD and mount the operating system concerned and chroot into it to run these commands.



How To Re-install GRUB 2 From A Live CD without the need to chroot.

1) Boot your Ubuntu Karmic Koala or later 'Desktop' Live CD, or another Karmic installation such as an operating system in a USB device or whatever you have to use as a as a rescue disk.

Tip: It will make your Ubuntu life a lot easier if you take a few minutes to set a nice user-freindly file system label in your linux file systems, How To Set File System Labels With GParted.

2) The operating system you want to fix needs to be mounted.
Just go 'Places'-->'Removable Media' or just 'Places' and look under 'Computer' for the disk or partition you want to mount and click on it. - also see Click-Icon Mounting.
You should see an icon for it on your desktop, but what you may not see is the 'mount point', which will normally be located in your /media directory. We will need to know the file path and name of the mount point.

3) Find the name of the mount point,
ls /media
The file path that is returned from the above command will be needed for making up the next command. The name of the mount point might be a file system LABEL or UUID number. For my example I'll just use the word 'disk' for short.

4)  Run the grub-setup command, inserting the -d option and specifying the path to the /boot/grub directory of the operating system you're trying to fix,
sudo grub-setup -d /media/disk/boot/grub /dev/sda
The -d option tells GRUB to use files from the specified directory.
Please substitute the word 'disk' with the name of your own mount point as found in step 3)
The '/dev/sda' part tells grub-setup to install GRUB to MBR in the first hard disk, which is called '/dev/sda'.
You may use the same command to install GRUB in any other disks in your computer by replacing the /dev/sda part of the command with /dev/sdb or /dev/sdc and so on.

- If the command fails with feedback about not being able to access a device.map file, you might need to try again and specify the exact device.map file to use with the -m option.
sudo grub-setup -d /media/disk/boot/grub -m /media/disk/boot/grub/device.map /dev/sda
The -m option tells GRUB what device.map you want to use.

 example thread:  Computer won't boot to 9.10; No Grub Menu

=============

More to do with the grub-setup command,
sudo grub-setup -b /media/disc/boot/grub/boot.img -c /media/disc/boot/grub/core.img -m /media/disk/device.map /dev/sda 
With the -b option you can specify the exact file path and file name for a specific boot image you want to install somewhere.
With the -c option you can also specify the file path and file name for a specific core image you want to install to an new partition.
I wasn't able to get these two working the last time I tried.
 



grub-mkfont
- /usr/bin/grub-mkfont - a program for changing fonts to make then suitable for use by GRUB. For working with graphical GRUB menus.

I don't know much about this subject yet ... sorry for any inconvenience.
See the GNU GRUB Wiki: gfxterm: Help for graphical terminal.




grub-mkdevicemap - usr/sbin/grub-mkdevicemap.exe (generates a new device.map file)
Users would rarely need to concern themselves with this command, it's for use by other programs or scripts.
grub-mkdevicemap automatically generates a new device.map file for grub (the file containing the mapping between the GRUB device names, and the system device names.
That file is used at installation time but not at boot time.

NAME
       grub-mkdevicemap - manual page for grub-mkdevicemap (GRUB) 1.96

SYNOPSIS
       grub-mkdevicemap [OPTION]...

DESCRIPTION
       Generate a device map file automatically.

       -n, --no-floppy
              do not probe any floppy drive

       -s, --probe-second-floppy probe the second floppy drive

       -m, --device-map=FILE
              use FILE as the device map [default=/boot/grub/device.map]

       -h, --help
              display this message and exit

       -V, --version
              print version information and exit

       -v, --verbose
              print verbose messages
(8)grub-mkdevicemap, - (5)grub_device.map.





grub-mkimage - usr/bin/grub-mkimage.exe (Makes a new  boot image, used by grub-install/mkrescue)
Makes a new core.img (boot image), that's the image is typically copied on the first sectors of a hard disk after the MBR.
grub mkimage isn't a script that users need to concern themselves with, it's just supplied to be used by  grub-install. and grub-mkrescue.

grub-mkimage makes a bootable image of GRUB, compressed with lzo [usually named core.img].
NAME
       grub-mkimage - manual page for grub-mkimage (GRUB) 1.96

SYNOPSIS
       grub-mkimage [OPTION]... [MODULES]

DESCRIPTION
       Make a bootable image of GRUB.

       -d, --directory=DIR
              use images and modules under DIR [default=/usr/lib/grub/i386-pc]

       -p, --prefix=DIR
              set grub_prefix directory [default=/boot/grub]

       -m, --memdisk=FILE
              embed FILE as a memdisk image

       -o, --output=FILE
              output a generated image to FILE [default=stdout]

       -h, --help
              display this message and exit

       -V, --version
              print version information and exit

       -v, --verbose
              print verbose messages

 grub mkimage - (8)grub-mkimage




grub-probe
- usr/sbin/grub-probe.exe  (Probes for device info, used by scripts)

grub-probe isn't a script that users need to concern themselves with, it's just supplied to be used by other programs. grub_probe detects the required partmap (pc partition table, disklabel...) and abstraction (lvm, raid) modules. The program that invoked grub-probe can gather the information. Normally, grub-probe is automatically invoked by grub-install or  grub-mkconfig or update-grub.

NAME
       grub-probe - manual page for grub-probe (GRUB) 1.98-1ubuntu7

SYNOPSIS
       grub-probe [OPTION]... [PATH|DEVICE]

DESCRIPTION
       Probe device information for a given path (or device, if the -d option is given).

       -d, --device
              given argument is a system device, not a path

       -m, --device-map=FILE
              use FILE as the device map [default=/boot/grub/device.map]

       -t, --target=(fs|fs_uuid|drive|device|partmap|abstraction)
              print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]

       -h, --help
              display this message and exit

       -V, --version
              print version information and exit

       -v, --verbose
              print verbose messages

REPORTING BUGS
       Report bugs to <bug-grub@gnu.org>.


(8)grub-probe



chroot
chroot from a Linux LiveCD
This is useful an emergency method for working on a Linux operating system , even when your Linux system is unbootable, and even if there is no boot loader at all installed in the operating system.

1) Boot a Ubuntu Live CD, Ubuntu in a USB, or another installed Ubuntu.

NOTE:
If you have a PC with a dual core processor, you could have either an i386 or amd64 (dual core) Ubuntu installation. You will need the Live CD or other Ubuntu installation with the same architecture as your installed Ubuntu (the one you want to operate on), in order to chroot successfully.

2) Mount your Ubuntu File System
You can use just go 'Places'-->'Removable Media', and click on the icon representing your desired partition for mounting now in all modern versions of Ubuntu.

TIP: If your icon appears with a name something like 'XY GB Filesystem', then you might want to take a little time out right now to go and set yourself some nice user friendly file system labels on all of your file systems.
See How To Set File System Labels With GParted - recommended.

For example, my Ubuntu Lucid Lynx file system is simply labelled as: LYNX

3) Open a terminal - 'Applications' --> 'Accessories' -->'Terminal',

 Check to see what the name of the mount point for your Ubuntu file system is in /media,
ls /media

If you haven't set a user friendly file system label, the mountpoint folder in /media will be named after the file system's UUID number instead, which is not so user  friendly.
For me, my mountpoint will be named LYNX, because that's what I set for the file system label.
From now on, please replace the word 'LYNX' with whatever the actual name of your own mount point is. (Yours might be different than mine).

For LVM encrypted installations, your mount point may be arbitrarily called: luksvolume
You need to install  lvm2 cryptsetup and run sudo modprobe dm-crypt and then you should  be able to mount
and chroot the same as everyone else.

4) chroot into the Ubuntu install

New: chroot method now updated based on the following Ubuntu Web Forums thread: how to chroot, simple and fast, by taavikko

sudo mount -o bind /proc /media/LYNX/proc

sudo mount -o bind /dev  /media/LYNX/dev

sudo mount -o bind /dev/pts  /media/LYNX/dev/pts

sudo mount -o bind /sys  /media/LYNX/sys

sudo cp /etc/resolv.conf  /media/LYNX/etc/resolv.conf

sudo chroot  /media/LYNX /bin/bash

root@ubuntu:/#

Now you can run whatever commands you need inside your chrooted file system to fix your Ubuntu installation.
NOTE: You don't need to type 'sudo' in front of your commands because you have a root shell, so be careful !
Some commands I have found useful are grub-install and grub-mkconfig, apt-get update and apt-get upgrade. You should be able to apt-get install software packages and run any other commands you think might be helpful.

When you're through, type 'exit',
root@ubuntu:/# exit
This exits the chroot to hard disk and returns you to a normal shell in the LiveCD.

It's considered good practice to umount, (at least I think it's probably a good idea),
sudo umount /media/LYNX/sys

sudo umount /media/LYNX/dev/pts

sudo umount /media/LYNX/dev

sudo umount /media/LYNX/proc