How to install an operating system to a USB drive

Sometimes, maybe for maintenance or data rescue reasons, you may want to have a USB thumbdrive with a GNU/Linux distribution installed. I’m not referring to writing a live ISO to a USB drive; I really mean having the distribution installed like it would be on an hard drive. An easy way to achieve this is using the virtualization software Qemu. Example:

sudo qemu-system-x86_64 -boot d -cdrom void-live-x86_64-20171007.iso -hda /dev/sdb -m 800

In this example, the -hda /dev/sdb part tells Qemu that the device /dev/sdb (the USB drive) must be used as an hard drive.

If you prefer Ubuntu, Fedora, Arch or any other distribution, you can install them this way too. There may be a need to adjust QEMU arguments, but in that case Google is your friend.

This also works with other operating systems that are not Linux based. OpenBSD, version 6.3, can be installed in a USB drive using the same parameters and booted after that.