Linux – Create a bootable pen drive through Linux command line

command linelinuxshellUbuntuusb

Can I make a bootable pen drive from a ISO using the Linux command line without installing any extra software?

If yes, what are the commands?

Best Answer

It should be possible, yes.

  1. Obtain an "hybrid ISO", which is simultaneously a valid image for a non-CD drive. This will be the case for any recently produced bootable linux ISO.

    Here's how to verify this before you try:

    $ file -k lubuntu-12.10-alternate-i386.iso
    lubuntu-12.10-alternate-i386.iso: # ISO 9660 CD-ROM filesystem data 'Lubuntu 12.10 i386              ' (bootable)\012- x86 boot sector; partition 1: ID=0x17, active, starthead 2, startsector 64, 1319448 sectors, code offset 0xed
    

    The ISO bit tells you it's an ISO :). The x86 boot sector bit tells you that it's potentially bootable as a non-CD disk as well. (It seems to have come out all on one line for some reason). You need the -k option of file, or it'll stop after finding the ISO bit and won't tell you about the x86 boot sector.

    (You could do also do this on a partial download of, I guess, the first 4MB of the file. In case your bandwidth is an issue).

  2. Say goodbye to any data on the pen drive.

  3. Carefully identify the device node for the pen drive. Try plugging it in, and see what dmesg|tail calls it. If you have a GUI you could use that to mount it, and then have a look at the output of mount. You're looking for something like sdx. sda is probably your first hard drive - don't get confused, or you'll have to say goodbye to all the data on that.

  4. Now make sure you haven't mounted an existing filesystem on the pen drive. A modern GUI like GNOME may have mounted it when you inserted it. Look at the output of mount again and act accordingly e.g. umount /dev/sdx1.

  5. dd if=lubuntu-12.10-alternate-i386.iso of=/dev/sdx bs=4k. Replacing sdx with the device name determined in step 3 :).

Some people try to make it go faster with e.g. bs=1M. Personally I prefer to let the kernel sort that out. bs=4k is a sensible minimum though; it's usually faster than the default 512 bytes, and rarely slower.

Note that the latest upstream version of the gnome-disks GUI should have a "restore disk image" feature equivalent to the above dd command. This would probably be my recommendation going forward. It should show usb drives with a nice friendly icon, and it should avoid any chance of overwriting a hard drive containing mounted filesystem(s). (You'd still need to avoid your not-mounted Windows disk though).

Related Question