Create USB installer to install Ubuntu from the command line?

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

  • pendrivelinux.com
  • a related question on the Linux Stackexchange
  • and a grub configuration example

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?


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


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.


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.


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.


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.