20 February 2012

70. Installing Debian on a USB stick -- live usb vs a true and full installation

Update 17 March 2013: See this post for a faster, better way of creating a full install if you're already running Debian.

Original post:
Every now and again I get posts like this one, or this one, via google news. While those posts --describing the use of unetbootin to create a 'live' USB analogue to a live CD -- may be technically correct, there's just so much more you can do.

In 1  we do the old boring bog-standard 'write live cd to a usb' thingy that you see pasted all over the web, but using cat instead of unetbootin.
In 2  we make a real, bootable installation on a usb drive.

1.  Creating a USB version of a live CD -- the boring option
With the current 'hybrid' Debian iso's it couldn't be easier.

 --- START HERE ---

a. Download the iso
Using jigdo is a good option. Downloading a business-card/netinstall iso is another, if you'll have a working internet connection available.

b. Plug in and mount your USB device and  find out the device name of your USB drive.
If it's mounted you can use
df -h
rootfs                                                   93G   36G   54G  40% /
udev                                                    3.9G     0  3.9G   0% /dev
tmpfs                                                   801M  980K  800M   1% /run
/dev/disk/by-uuid/..-10a350f85687   93G   36G   54G  40% /
tmpfs                                                   5.0M     0  5.0M   0% /run/lock
tmpfs                                                   1.6G   52K  1.6G   1% /tmp
tmpfs                                                   1.6G  816K  1.6G   1% /run/shm
/dev/sda6                                               745G  183G  525G  26% /home
/dev/sdc1                                               2.0G  434M  1.5G  23% /media/XP-KOMKU


../dev/sdc1 on /media/XP-KOMKU type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0077,codepage=cp437,iocharset=utf8,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks)

to list mounted devices. In my case it's an old 2 G usb stick I used to create a Windows XP installation USB device on.

If you prefer a gui tool, start palimpsest (called Disk Utility in gnome)
It will be something akin to sdb1 or sdc1 etc. That means the device name is /dev/sdb or /dev/sdc, respectively. In our case, it's sdc.

c. Unmount but don't detach the device
You don't want anything else writing to it.

me@beryllium:~/Documents/screengrabs$ umount /dev/sdc1
me@beryllium:~/Documents/screengrabs$ df -h
Filesystem                                              Size  Used Avail Use% Mounted on
rootfs                                                   93G   36G   54G  40% /
udev                                                    3.9G     0  3.9G   0% /dev
tmpfs                                                   801M  976K  800M   1% /run
/dev/disk/by-uuid/..-10a350f85687   93G   36G   54G  40% /
tmpfs                                                   5.0M     0  5.0M   0% /run/lock
tmpfs                                                   1.6G   60K  1.6G   1% /tmp
tmpfs                                                   1.6G  820K  1.6G   1% /run/shm
/dev/sda6                                               745G  183G  525G  26% /home
d. Become root and use cat to write the iso to the USB device

sudo su

And write (assuming that the device name is sdc and you are using the businesscard iso)

cat debian-6.0.3-amd64-businesscard.iso > /dev/sdc


e. boot from your USB drive and go through the same steps as for a CD.  
Plug it in, then start your computer. Hit F12 (or F10 or del or F2) during the bios start-up to select boot medium. Choose to boot from usb. Older BIOSes can't boot from USB.

2. Really INSTALLING Debian on a USB
You can use either virtualbox or quemu for this. Here I show how to use virtualbox.

[NOTE: there's a leaner, faster, better way described here: http://verahill.blogspot.com/2013/03/361-installing-debian-on-usb-stick-from.html ]

  •  Note that a tiny 2 Gb USB stick isn't suitable for a full Debian + Gnome installation. It may, however, work well with e.g. openbox and xmonad. Or just work in the terminal -- use mutt for email, mcabber for chat, elinks for http/www etc.
  • Here I show how to install using the businesscard iso, but I've done this with the regular Debian DVD version in the past.
  • Also, few USB devices operate at 480 Mbps -- so be prepared that installation via virtualbox onto a USB device will take considerably longer than a regular installation.
  • Solid state devices degrade with I/O, so use your USB stick for back-up use during e.g. travel etc. Don't trust the integrity of it. 

There are a few good reasons for installing a full linux version on a thumb drive or a regular but external harddrive:
*  a thumb drive will allow you too carry an independent OS with you, allowing you to e.g. temporarily borrow a computer from a friend but running your own system. An installation, as opposed to a 'live cd', allows you to customise the setup and install programs. USB 2 is quite slow, and many thumb drives are slower than that, so you're probably better off running a light system than a full on gnome-shell desktop. That's not so say that you can't -- I have.
* installing onto a usb-connected harddrive will allow you to setup and prepare headless boxes or systems without a direct internet connection.

First you need to install virtualbox, which is available in the debian repos (sudo apt-get install virtualbox virtualbox-guest-additions), and -- in order to enable USB 2 support --  the "VirtualBox 4.1.8 Oracle VM VirtualBox Extension Pack" from https://www.virtualbox.org/wiki/Downloads
Start virtualbox and go to  File/Preferences/Extensions to install the extension pack you downloaded.

 --- START HERE ---

Installing debian (or any os) on an external USB stick/drive using virtualbox
a. Start virtualbox and click on New.

b. Click through the Name, Operating System and Version questions, Select a memory size which is reasonable for the intended use and target machine. Give it at least 256 Mb. Don't fret about this step -- on each boot your system will autodetect the amount of available RAM. The next question is the key to the whole undertaking: DO NOT SET A START-UP DISK (or any virtual harddisk)

In the next dialogue box you'll be asked if you want to continue or go back - yes, you want to continue.

c. Your new machine is now ready to be set up. Select it and click on settings; Got to storage, IDE controller and choose a virtual CD/DVD disk file. Select the debian installation iso.

d. Next click on USB, enable USB 2 controller, and add a filter for your device by e.g. hitting alt+insert and selecting it from the list.

e. You're ready to install! Start the virtual machine, and the installation should start without you having to select boot device. Most of the setting are the same as in any normal install. Pay attention when you come to the Partition Disk dialogue though. Make sure that you're not doing something silly and accidentally deleting something you shouldn't be a-deleting, even though the risks of that are very slim.

Delete the existing partitions and create a new one. I added a tiny swap space too, hoping that this will make the difference between a freeze and a slow crawl on underpowered systems.
f. Continue with the installation as normal.

g. Postscript:
* /dev/sda1 vs UUID. Once you're done with the installation  and have shut down your virtualbox, mount the USB drive as a normal thumb drive and have a look at fstab in particular -- if you're using relative paths (e.g. sda) instead of absolute paths using UUID, you may run into problems at some point.

In the screengrab you see that

# / was on /dev/sda1 during installation
UUID=8c0c9c98-e73f-4f5a-b7d7-f91f95c9d8ca /               ext4    errors=remount-ro 0       1

Which means we're good.

You may want to comment out
/dev/sda1       /media/usb0     auto    rw,user,noauto  0       0
/dev/sda2       /media/usb1     auto    rw,user,noauto  0       0
/dev/sda5       /media/usb2     auto    rw,user,noauto  0       0

But I haven't in the past and all has been good.

* network interfaces -- you may find that no network interface is configured for you and that no interface shows up when you use ifconfig. Use ip addr to get a list over interfaces. If the interface is e.g. eth2, you may  want to edit /etc/network/interfaces and add

auto eth2
iface eth2 inet dhcp

or (the ip's are just examples)

auto eth2
iface eth2 inet static

And then run sudo service networking restart

That's it. You now have a full installation on either a 'removable' drive or on an HDD which you can install in a desktop/laptop.

Links to this page:


  1. I am trying to do same thing with VirtualBox running on Win7. Debian 6.0 installation starts from iso file but it does not see the installation drive (the USB stick) :(. Any comments

    1. I presume that you've install the virtualbox extension pack and have selected the USB device (settings/usb/add filter)?

      Google also suggest reading this: https://forums.virtualbox.org/viewtopic.php?f=2&t=47250

  2. Some people even try to change the .vdi format of Virtualbox to RAW format for a flash drive to install Debian on it..... It is old(tested on lenny which is no longer supported) but it may run on newer debian releases like wheezy.

  3. What a great technique!!!
    I've been trying to create a personalized self-booting USB rescue disk where I can add or remove software utilities as I need them. This works perfectly. It also introduced me to virtualbox which I had never used before. My next quest will be how to make the device recognize my laptop wireless card, at least for tinkering and tweaking the image. Thank you so much for this post

    1. Happy it worked out for you. As for your wireless card, it should be enough that you have the right drivers and firmware installed -- they should then be automatically loaded by udev when the hardware is present.
      Note that some broadcom cards can be annoying e.g. http://verahill.blogspot.com.au/2013/09/508-very-very-briefly-why-apt-get-purge.html

  4. Thanks so much for this, was struggling for an hour before I saw this...phew!

  5. Can it be done without virtual box?

  6. Can I install other program into the OS which installed in USB stick?

    1. See response on http://verahill.blogspot.com.au/2013/03/361-installing-debian-on-usb-stick-from.html?showComment=1411112395034#c1487397267946155751

  7. Nice aproach ... Never got the idea to use virtualbox, but on the other other hand? Why? To me it seems much easier set up partitions and then use debootstrap to install a really neat, clean and customized system. And - as its mobile and you are in great risk of losing it - it ist very easy to be completely encrypted with cryptsetup. Just edit crypttab, fstab an after updating initram - here you are.

    As rescue disk, I always used sysresccd. Install takes a few minutes and if you can't save your ass with that - anything else would take you much longer to fail ....

    Actually, I just see one purpose for using Virtualbox. Years ago, I made a USB-Bootstick capable booting more than 10 linux distributions. And I used vb a lot to test the MBR.

    Anyway, I like your blog .... and even more your attitude. If you do not claim the copyright, maybe I steal some phrases of the whoami. Especially "I don't really feel a pressing need to post pictures of my latest meal, or posting inspired but uninspiring rants about the latest idiocies" - that made my day.

    Cheers to Canberra ... AG13

    1. Thanks for your comment! Feel free to steal, copy, borrow, paraphrase the whoami text in part or wholesale.

      So, the VB approach. All your comments are valid. Personally, the main reason for looking at the VB/usb drive combination was to install debian on a headless server (and we're talking at 1U server with one of those directly attached flash memories. Since then, I've mainly used it as a way to install debian from a running system with internet access. It's been useful due to squeeze (deb 6.0) needing non-free drivers for some of my boxes, and because my employer requiring web-based login to get access to network services. I also can't afford to reboot my system very often since it's the gateway of my beowulf cluster and does a lot of the storage management. So I do think that installing from a running system using either VB (easy, but slow) or a chroot (requires a bit more familiarity with linux; http://verahill.blogspot.com.au/2013/03/361-installing-debian-on-usb-stick-from.html) can be justified.

      Finally, your comment re encrypting USB stick installations given the risk of losing them is an important one. I haven't yet written up how to do a fully encrypted install (nor have I explored how to do it), but it's something that people visiting this page should definitely consider looking into.

  8. Good morning,

    I'm from Brazil and my English isn't very good but I'd like ask you something.

    I installed the Debian 7 on my USB flash drive and it works perfectly only in one PC(used to install it). Why? It's wouldn't be like um OS portable? Please, send the answer to: danilo.lima89@yahoo.com

    Thank you very much


  9. Well im stuck on drive selection i did like u but it dont recognize drive...(i instelled extension pack, my os is win7) Install ask me for hdd driver to choose i have no idea which one choose

  10. Installing Debian 8 Jessie in VMware Workstation Video Tutorial

  11. It works to me for 3.16 kernel, but if I try to install 4.4 kernel from Debian Sid repository it doesn't boot :-/

  12. I have been trying this for two days. I have done everything as you posted. Everything including the postscript part, everything as you have posted.

    But when I try to boot the USB, I get a black screen with a blinking cursor. I tried mounting the USB on a live debian, I can access the files. But it just wont boot. Please please help. Thank you.

    1. My guess is that it's related to video card/driver. If you have an nvidia card, make sure that nouveau is loaded and there's nothing interfering.
      Does ctrl+alt+f1 work?

    2. I had debian installed in my hdd before. It used to work great, dual boot with windows 10. But I removed it a week ago, and wanted it on a usb, which is why I followed your post.

      I mounted the usb with debian, tried inserting nomodeset in grub. Did not work. I am not sure this has something to do with nvidia.

      The blank screen appears right after the bios boot logo. I shouldn't have said 'cursor', its a blinking prompt. Ctrl+Alt+F1 doesn't work either. In fact, nothing except for Ctrl+Alt+Del works on that blank screen.

      Please advice. Thank you so much for responding.

    3. i have the same problem.
      I can run the system using supergrub to run the partition but not with the system on the key alone.

    4. Ok i found the solution

      I had to edit /boot/grub/grub.cfg

      Actually, no matter is in the fstab, the grub conf had many entrances of /dev/sda1 that were disabling the boot from happening, as my usb key was recongnized as /dev/sdc1.
      I dont know how to simplify the grub file so it has only the UUID of the key, but as its working now.. Anyway, i guess ill have to change whenever ill change computer too...

  13. I was following -> Installing debian (or any os) on an external USB stick/drive using virtualbox

  14. I get a blackscreen when i try to boot. I used supergrub to bot the partition and it worked. But as is, it is no capable of booting the linux partition. I have one ext4 as / and one swap. fstab is pretty much the same, i tried commenting some of the lines but nothing changes. In supergrub, the UUID is the exact same as in fstab. But without supergrub, impossible to run the system.. Help please...

  15. Same problem here. Cltr f1 ou ctrl alt f1 isnt working. It just doesnt know how to access the partition.. Though have the right UUID and supergrub is alble to run the partition