How to Install Ubuntu Linux on a USB Flash Drive

Posted on 04 April 2009

Being able to run Linux straight out of a USB drive is a great way to enjoy the liveCD experience, letting you run Linux from any computer you want. And it saves you from the trouble of carrying a CD along all the time, with your USB drive easily tucked away in your pocket.



Here’s a simple tutorial that shows you how to install Ubuntu on a USB drive. Even though this tutorial uses Ubuntu as its base distribution, you can virtually use any type of Linux liveCD distribution.

What you need

In order to be able to execute this simple tutorial, you will need:


Step 1: Finding the device

First of all, plug in your USB drive and check which device it is associated under. To find out that out, run:

$ sudo fdisk –l

On my system, the device appears as being /dev/sdb; therefore, we’ll be using /dev/sdb as a reference for this tutorial. Please make the replacement according to your system (for example, sda, sdc, etc)

Once the device has been located, the next step is creating the partitions.

Using the wrong device name might be fatal to your system partitions, so make sure you double check.

Step 2: Creating the partitions

Before you continue, make sure that you un-mount ALL your mounted partitions. To do so, run:

$sudo umount /dev/sdb1
Now launch fdisk, a tool to edit partition under Linux.

sudo fdisk /dev/sdb
What we are going to do now is delete all the partitions, and then create 2 new partitions: one FAT partition of 750 MB to host the files from the liveCD iso, and the other for the rest of the files.

At fdisk prompt, type d x, where x denotes the partition number (you can simply type d if you have only one partition).

Then:

  • n to create a new partition
  • p to make it primary
  • 1 so that it is the first primary partition

Accept the default or type 1 to start from the first cylinder

  • +750M to make it 750 Meg big
  • a to toggle the partition active for boot
  • 1 to choose the 1 partition
  • 6 to set it to FAT16

Our first partition is now set up. Let’s move onto the next one:

  • n to create a new partition
  • p to make it primary
  • 2 to be the second partition
  • Accept the default by hitting Enter
  • Accept the default to make your partition as big as possible
  • Finally, type w to write the change to your USB drive
  • Partition creation process is now complete. The next step is formatting them.

Step 3: Formatting the partitions

We are going to format the first partition as a FAT file system of size 16 and we are then going to attribute it the label “liveusb”.

$ sudo mkfs.vfat -F 16 -n liveusb /dev/sdb1
The second partition is going to be of type ext2, with a blocksize of 4096 bytes and the label casper-rw. Make sure that the label is casper-rw; otherwise the tutorial will not work.

$ sudo mkfs.ext2 -b 4096 -L casper-rw /dev/sdb2
And now, our USB drive is ready to host the liveCD image. The next step is copying the files to the USB stick.

Step 4: Mounting Ubuntu liveCD image

First off, we need to mount our Ubuntu iso. Depending on you having the .iso file or the CD, there are two different ways of doing it.

Mounting from the CD:

People using Ubuntu or any other user-friendly distribution might just have to stick the CD in, and it will be mounted automatically. If that doesn’t happen, the following command should mount it:

$ sudo mount /media/cdrom

Mounting from an .iso image file:

You will have to create a temporary directory, for example let’s use /tmp/ubuntu-livecd, and then mount your iso. (We will be using a feisty fawn iso).

  • $ mkdir /tmp/ubuntu-livecd
  • $ sudo mount -o loop /path/to/feisty-desktop-i386.iso /tmp/ubuntu-livecd

Once the CD image is ready, it is time to mount the newly created USB stick partitions.

Step 5: Mounting the USB stick partitions

Like we experienced with mounting the Ubuntu liveCD image directly from the CD, you might be able to get both partitions by simply re-plugging the USB drive. Partitions might appear as /media/liveusb and /media/casper-rw. If that doesn’t happen, you will have to mount them manually:

$ mkdir /tmp/liveusb
$ sudo mount /dev/sdb1 /tmp/liveusb

All the partitions we need are now mounted. Now lets move on to the next step.

Step 6: Copying the files to the USB stick

Access the CD image directory (/tmp/ubuntu-livecd in my case, but it might be /media/cdrom) and copy at the root of your USB first partition:

the directories: “casper”, “disctree”, “dists”, “install”, “pics”, “pool”, “preseed”, “.disk”
The content of directory “isolinux”
and files “md5sum.txt”, “README.diskdefines”, “ubuntu.ico”
as well as files: “casper/vmlinuz”, “casper/initrd.gz” and “install/mt86plus”
$ cd /tmp/ubuntu-livecd
$ sudo cp -rf casper disctree dists install pics pool preseed .disk isolinux/* md5sum.txt README.diskdefines ubuntu.ico casper/vmlinuz casper/initrd.gz install/mt86plus /tmp/liveusb/
Ignore any complaints your screen shows about symbolic links not being able to create.

Now let’s go to the first partition of your USB drive and rename isolinux.cfg to syslinux.cfg

$ cd /tmp/liveusb
$ sudo mv isolinux.cfg syslinux.cfg

Change /tmp/liveusb according to your settings.

Edit syslinux.cfg so that it looks like this:

DEFAULT persistent
GFXBOOT bootlogo
GFXBOOT-BACKGROUND 0xB6875A
APPEND  file=preseed/ubuntu.seed boot=casper initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
LABEL persistent
menu label ^Start Ubuntu in persistent mode
kernel vmlinuz
append  file=preseed/ubuntu.seed boot=casper persistent initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
LABEL live
menu label ^Start or install Ubuntu
kernel vmlinuz
append  file=preseed/ubuntu.seed boot=casper initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
LABEL xforcevesa
menu label Start Ubuntu in safe ^graphics mode
kernel vmlinuz
append  file=preseed/ubuntu.seed boot=casper xforcevesa initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
LABEL check
menu label ^Check CD for defects
kernel vmlinuz
append  boot=casper integrity-check initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash –
LABEL memtest
menu label ^Memory test
kernel mt86plus
append -
LABEL hd
menu label ^Boot from first hard disk
localboot 0×80
append -
DISPLAY isolinux.txt
TIMEOUT 300
PROMPT 1
F1 f1.txt
F2 f2.txt
F3 f3.txt
F4 f4.txt
F5 f5.txt
F6 f6.txt
F7 f7.txt
F8 f8.txt
F9 f9.txt
F0 f10.txt

Finally, we have our USB drive almost usable. Which brings us to our final step.

Step: 6: Making the USB stick bootable

In order to do this, we need to install syslinux and mtools:

$ sudo apt-get install syslinux mtools
And finally, unmount /dev/sdb1 and make it bootable:

$ cd
$ sudo umount /tmp/liveusb

$ sudo syslinux -f /dev/sdb1

There you are!

Reboot, set your BIOS to boot from the USB drive and enjoy Ubuntu Linux from a flash drive.

Troubleshooting

If you run into trouble booting on the USB stick, this could be due to your MBR being corrupted. In order to fix this, you can use lilo.

$ lilo -M /dev/sdb
This should fix the MBR on device /dev/sdb.

Not what you are looking for? Try Google Search!





People who liked this Post also read

14 Responses to “How to Install Ubuntu Linux on a USB Flash Drive”

  1. Than you for your installation review… it really help…

  2. OKPowerball says:

    Very good thanks for the guide.

  3. thanks for share it!!}

  4. Is easy!! thanks for share it.

  5. DakotaSioux says:

    great!!!, I was looking for this, thanks alot.

  6. donlaughlins says:

    Yeahhh follow all the steps!! victory

  7. donlaughlins says:

    hey watched the video

  8. So great!!! thanks for the help

  9. Good, thanks for the information.

  10. very good, thanks alot for this.

  11. Book Reader says:

    Excellent! thank you very kindly.

  12. Ed Podesta says:

    Cheers! This is a great walk through – here goes with the re-boot!

  13. Mike says:

    I tried running Ubuntu on a USB stick, but it ran really slow. I don’t know if I did something wrong, but it seemed to run even slower than booting from a CD.
    Mike´s last blog ..Connect an Ubuntu computer to a Network Server at Login


Calendar

April 2009
M T W T F S S
« Mar   May »
  1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30