22

I'm trying to create a bootable USB image to install Ubuntu on a new computer.

I have done this before following the "create USB drive" instructions for Ubuntu desktop, but I don't have an Ubuntu desktop available.

How can I do the same using only the command line?

Things I've tried:

  • Create bootable USB on Mac OS X following the ubuntu.com "create USB drive" instructions for Mac: Doesn't boot.
  • usb-creator: According to apt-cache search usb-creator and Wikipedia usb-creator only exists as a graphical tool.
  • "Create manually" instructions at help.ubuntu.com: None of the files and directories described (e.g. casper, filesystem.manifest, menu.lst) exist in the ISO image, and I don't know what has replaced them.
  • unetbootin scripting: Requires X server (graphics support) to run, even when fully scripted. (The command sudo unetbootin lang=en method=diskimage isofile=~/ubuntu-10.10-server-amd64.iso installtype=USB targetdrive=/dev/sdg1 autoinstall=yes gives an error message unetbootin: cannot connect to X server.)

Update

Also tried GRUB fiddling: Merging information from

I was able to get halfway there - it booted from USB, displayed the grub menu and started the installation, but the installation did not complete.

For reference, this is the closest I got:

sudo su
  # mount USB pen
mount /dev/sd[X]1 /media/usb
  # install GRUB
grub-install --force --no-floppy --root-directory=/media/usb /dev/sd[X]
  # copy ISO image to USB
cp ~/ubuntu-10.10-server-amd64.iso /media/usb
  # mount ISO image, copy existing grub.cfg
mount ~/ubuntu-10.10-server-amd64.iso /media/iso/ -o loop
cp /media/iso/boot/grub/grub.cfg /media/usb/boot/grub/

I then edited /media/usb/boot/grub.cfg to add an .iso loopback, example grub entry:

menuentry "Install Ubuntu Server" {
  set gfxpayload=keep
  loopback loop /ubuntu-10.10-server-amd64.iso  
  linux (loop)/install/vmlinuz  file=(loop)/preseed/ubuntu-server.seed iso-scan/filename=/ubuntu-10.10-server-amd64.iso quiet --
  initrd (loop)/install/initrd.gz
}

When booting from USB, this would give me the Grub boot menu and start the installer, but the installer gave up after a couple of screens complaining that it couldn't find the CD-ROM drive. (Naturally, as the box I'm installing on doesn't have an optical drive.)

I resolved this particular issue by giving up and doing the "create USB drive" routine using the Ubuntu Live desktop CD (on a computer that does have an optical drive), then the USB install works.

But I expect that there is some way to do this from the command line of an Ubuntu system without X server and without an optical drive, so the question still stands.

Does anyone know how?

7
  • try unetbootin.
    – RolandiXor
    Jan 13, 2011 at 1:33
  • @Roland unetbootin: cannot connect to X server - seems to require X even when fully scripted (unetbootin lang=en method=diskimage isofile=/srv/share/ubuntu-10.10-server-amd64.iso installtype=USB targetdrive=/dev/sdg1 autoinstall=yes), and I'd prefer to remain headless. Thanks for the tip, though; I may install X if I run out of other options. Jan 13, 2011 at 2:02
  • These look promising: pendrivelinux.com/install-grub2-on-usb-from-ubuntu-linux and unix.stackexchange.com/questions/665/… Needs some tweaking, but will try it out. Jan 13, 2011 at 4:06
  • @j-g-faustus : If things were so complicated, i would've abondoned linux, maybe. use dd. Its a gem. cant say enough. And every linux distro has dd; so you have a universal solution, with nothing to script/write/configure. just one beautiful line.
    – Mahesh
    May 26, 2012 at 18:50
  • That grub.cfg entry needs to say file=/cdrom/preseed/ubuntu-server.seed on the linux line (and not file=(loop)/preseed/ubuntu-server.seed), to avoid a fatal 'Failed to retrieve the preconfiguration file' error once the CD image has successfully been detected. Apr 30, 2014 at 14:49

7 Answers 7

10

Have you tried with # dd if=path/to/image.iso of=/dev/sd# ?

3
  • This one is a gem. The best one. works on any linux distro. but you need to be extra careful. adding bs=20M or any other suitable value speeds things up. for details, refer man dd
    – Mahesh
    May 26, 2012 at 18:47
  • 1
    @Mahesh No, some linux distro do not work even with this command, One of them is OpenSUSE
    – Anwar
    Oct 8, 2012 at 17:56
  • or MeeGo and derivates those images are called hybrid ones
    – rzr
    Nov 17, 2013 at 17:31
9

When booting from USB, this would give me the Grub boot menu and start the installer, but the installer gave up after a couple of screens complaining that it couldn't find the CD-ROM drive.

This is a known bug with ubuntu server iso (desktop iso works just fine), but there is a fix to get the packages from the iso on the usb drive: once on the installer, Ctrl+Alt+f2 to open a tty and in the terminal:

mount -t vfat /dev/sdX1 /mnt
ln -sf /mnt/ubuntu-server.iso /dev/sr0

Then switch back to installer and retry to scan cd for packages. It should work.

2
  • I don't have a system to try it out on at the moment, but yeah - it sounds like it should work :) Thanks! Mar 26, 2011 at 16:33
  • I found that the "Detect and mount CD-ROM" menu option would revert /dev/sr0, undoing the ln and returning it to its original (broken) state. However, if I waited at the "Retry mounting the CD-ROM?" prompt, did the ln in tty2, and then returned to press "Yes", it did work. (That "Retry?" dialog would only appear once the mount command had been given, though.) Apr 30, 2014 at 14:46
5

Let us assume you have nothing but a prompt before you. If you have a GUI, you can open a terminal and do everything described here. Or you may use a tty.

WARNING:
To avoid potential damage, follow the instructions very very carefully.

  • Switch to a root prompt by typing sudo su

    Detect what device the usb is.

  • tail -f /var/log/syslog

You are now having a live view at syslog. Plug in your usb drive. You should see some messages on screen. Something like this.

May 27 00:35:07 Mahesh kernel: [ 5054.646585] usb 2-1.1: new high-speed USB device number 5 using ehci_hcd
May 27 00:35:07 Mahesh kernel: [ 5054.741437] scsi8 : usb-storage 2-1.1:1.0
May 27 00:35:07 Mahesh mtp-probe: checking bus 2, device 5: "/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1"
May 27 00:35:07 Mahesh mtp-probe: bus: 2, device: 5 was not an MTP device
May 27 00:35:08 Mahesh kernel: [ 5055.739177] scsi 8:0:0:0: Direct-Access     JetFlash TS2GJFV30        8.07 PQ: 0 ANSI: 2
May 27 00:35:08 Mahesh kernel: [ 5055.740198] sd 8:0:0:0: Attached scsi generic sg3 type 0
May 27 00:35:08 Mahesh kernel: [ 5055.741593] sd 8:0:0:0: [sdc] 4005888 512-byte logical blocks: (2.05 GB/1.91 GiB)
May 27 00:35:08 Mahesh kernel: [ 5055.742214] sd 8:0:0:0: [sdc] Write Protect is off
May 27 00:35:08 Mahesh kernel: [ 5055.742218] sd 8:0:0:0: [sdc] Mode Sense: 03 00 00 00
May 27 00:35:08 Mahesh kernel: [ 5055.742712] sd 8:0:0:0: [sdc] No Caching mode page present
May 27 00:35:08 Mahesh kernel: [ 5055.742715] sd 8:0:0:0: [sdc] Assuming drive cache: write through
May 27 00:35:08 Mahesh kernel: [ 5055.745326] sd 8:0:0:0: [sdc] No Caching mode page present
May 27 00:35:08 Mahesh kernel: [ 5055.745329] sd 8:0:0:0: [sdc] Assuming drive cache: write through
May 27 00:35:08 Mahesh kernel: [ 5055.781564]  sdc: sdc1
May 27 00:35:08 Mahesh kernel: [ 5055.784191] sd 8:0:0:0: [sdc] No Caching mode page present
May 27 00:35:08 Mahesh kernel: [ 5055.784196] sd 8:0:0:0: [sdc] Assuming drive cache: write through
May 27 00:35:08 Mahesh kernel: [ 5055.784200] sd 8:0:0:0: [sdc] Attached SCSI removable disk

look at it, and find one thing in square brackets repeating? sdc in this case. Note it down on a piece of paper. Type Ctrl+C to get to the prompt.

Locate the iso file to burn onto usb.

If the iso file is at /path/to/iso/somecd.iso ; note this path on a piece of paper.

Write onto USB

Warning:

This will erase the entire USB disk. Backup before proceed.

  • dd if=/path/to/iso/somecd.iso of=/dev/sdc ibs=20M obs=20M

Replace /path/to/iso/somecd.iso with the actual path to your iso file.

Replace /dev/sdc with whatever you noted down earlier. If the repeating part is sdb ; use /dev/sdb and so on.

Warning:

Typing a drive letter wrong will result in permanent data loss on a different drive. Pay extra attention while issuing dd command.

3
  • 2
    In order to avoid data loss by selecting wrong disk it is advised to use /dev/disk/by-label/LABEL instead of /dev/sdc
    – totti
    Feb 1, 2013 at 8:22
  • 1
    Sensible suggestion. But when you are not sure of the label, or you have a ton of 'New volume' s, it becomes a pain. I always prefer the sdX one, but that is a personal choice.
    – Mahesh
    Feb 1, 2013 at 8:28
  • 1
    Best answer. One suggested improvement is to use pv on the input stream to dd, so the user gets progress feedback (i.e. pv reads the iso, streams it to dd, and displays progress on stderr).
    – Stabledog
    Mar 23, 2014 at 15:36
3

Creating a bootable Ubuntu USB flash drive from terminal

  • Mount the ubuntu.iso file.For this run the below commands in terminal,

    sudo mkdir /media/iso
    sudo mount -o loop /path/to/ubuntu.iso /media/iso
    
  • Then copy all the files from /media/iso to your mounted usb flash drive folder in /media.

    Insert your usb flash drive.

    It will automatically mounted,whenever you insert it.Your usb partition must be mounted inside /media.Let us assume your usb drive mounted inside /media/xxxx folder.Then run the below command,

    cp -a /media/iso/. /media/xxx
    

    Run lsblk command to know the usb device partition.

  • Then paste the ldlinux.sys file inside your USB flash drive partition(In my case /dev/sdd1) to make the usb bootable .For this run the below commands,

    sudo apt-get install syslinux mtools
    syslinux -s /dev/sdd1
    
  • Go into the /media/xxx folder and rename the isolinux directory to syslinux.Then go into the renamed syslinux folder and rename the file isolinux.cfg to syslinux.cfg.

       mv /media/xxx/isolinux /media/xxx/syslinux
       mv /media/xxx/syslinux/isolinux.cfg /media/xxx/syslinux/syslinux.cfg
    
  • Reboot your pc and change the boot-order in bios to USB.Now your ubuntu usb flash drive will booted up and you can install it.

This method will works for any linux distributions,not only ubuntu.

1

I solved this particular problem by using the Ubuntu Live desktop CD.

By booting the Live CD on a computer with an optical drive, I could use the Ubuntu desktop instructions to create the USB pen installer without touching the existing installation.

1

You can hack usb-creator to do this.

You should already have a single vfat partition as partition 1 on the usb device (the erase disk step of usb-creator-gtk does this) and it should be marked bootable.

Next, we will get usb-creator python code to assist us.

$ sudo apt-get install usb-creator

or

$ bzr branch lp:usb-creator

create a file with this content and name it usb-creator-cli, if you use bzr in the last step, place this file in the usb-creator directory (root of bzr branch)

#!/usr/bin/env python
from __future__ import print_function
from usbcreator.misc import sane_path, setup_gettext, setup_logging, text_type

from usbcreator.install import install

sane_path()
setup_logging()
setup_gettext()

#/dev/sdb1 should be mounted on /mnt
#iso should be mounted to /iso
dev = '/dev/sdb1'
source = '/iso'
target = '/mnt' 
ugh = install(source, target, False, device=dev)
ugh.success = print
ugh.failure = print
ugh.progress = print
ugh.progress_message = print
ugh.progress_pulse = print
ugh.progress_pulse_stop = print
ugh.retry = print
ugh.run()

Make it executeable

$ chmod +x usb-creator-cli

Now mount your iso to /iso and mount your usb device to /mnt

$ sudo mkdir /iso ; sudo mount ubuntu-server-12.10-amd64.iso /iso
$ sudo mount /dev/sdb1 /mnt

If you want very verbose messages at your console you can tail ~/.cache/usb-creator.log

$ tail -f ~/.cache/usb-creator.log &

Now run that usb-creator-cli script

$ sudo ./usb-creator-cli

And watch all the messages scroll by.

When you return to the prompt, don't forget to unmount /mnt before you yank your usb storage device.

-2

You can install a command line (CLI) Ubuntu using the Alternate Install or Mini iso (which should fit on your 512mb stick)

Then install Crunchbang using the alternate method: http://crunchbanglinux.org/forums/topic/26/crunchbang-linux-81001-alternative-installation/

You could also try the Lite version of CrunchBang, which is less than 512mb.

You could use Unetbootin to prepare the USB stick.

3
  • crunchbang? haha?
    – RolandiXor
    Jan 13, 2011 at 3:16
  • I don't have a problem fitting the ISO on the stick (it's 16GB), it's more a question of how I can create a USB stick that boots into the ISO in the first place, and creating it using nothing but the CLI. Jan 13, 2011 at 3:48
  • And unetbootin doesn't work, unfortunately - it seems to require graphics support even when you run it from the command line. Jan 13, 2011 at 4:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .