Install and run Raspbian from a USB Flash Drive

In this tutorial, I’m going to talk you through running Raspbian from a USB connected drive instead of from an SD card. Running from a USB connected Flash or Hard Drive has many advantages, the biggest being speed and reliability.

SD Cards have a limited read/write cycle, and when hosting a site with a MySQL database from a SD card, it won’t take long before you start getting corruptions and failures. USB Flash drives provide a cheap and reliable alternative. I’ve tested several USB Flash drives, and found Sandisk and Corsair to be the best for speed and reliability. This site is run off a 16GB Corsair Voyager 3 USB Flash drive.

Assumptions before we begin

I’m going to assume that you know your way around Terminal, and are using a Mac to perform these steps. You will still need an SD card to store the boot instructions to tell the Raspberry Pi to launch the OS from the USB; the Raspberry Pi’s can’t (yet) boot directly from a USB storage device.

Step 1 – Download Raspbian from Raspberry Pi

You will need the standard Raspbian OS image, you can download this from the official Raspberry Pi website. Once you’ve downloaded it, unzip it. It’s around 400mb in size, so should only take a couple of minutes over a broadband connection.

Step 2 – Install the Raspbian OS to your USB Flash drive

Plug in your USB stick and launch Terminal. The first thing we’re going to do is get the device identifier for your USB Flash drive. To do this run the following command:

diskutil list

The list of attached disks will show up with their identifiers. Important – make a note of the correct identifier, you can do some serious damage by choosing the wrong one!

In the screen shot above, I can see that /dev/disk2 is the correct identifier for my Sandisk USB Flash Drive. Yours may be different so change to suit your configuration. Next we’re going to unmount the USB Flash Drive. To do this enter the following command:

diskutil unmountDisk /dev/disk2

Yet again, be really careful to change disk2 to whatever your computer identifies the USB Flash Drive as. You will get a message saying

“Unmount of all volumes on disk2 was successful”.

Now we can begin the copy. For ease, I’ve changed the directory in Terminal to where the Raspbian image is located, which in this case is my downloads folder. If you’ve downloaded and unzipped the disk image to your downloads folder, running this command should take you there:

cd ~/Downloads/

Now run this command to begin the copy:

sudo dd bs=1m if=2013-02-09-wheezy-raspbian.img of=/dev/disk2

As with before, make sure you change disk2 to whatever your computer identifies as being the USB Flash Drive, and change 2013-02-09-wheezy-raspbian.img to whatever your image file is called. The blocks will now begin moving to your USB Flash Drive from the Raspbian OS image. This takes anything from 5 to 20 mins depending on the speed of your USB Flash Drive. Go stick the kettle on and have a brew!

Eventually, you will see something similar to the above, and it probably took a while too. The next step is to configure your SD Card to give the correct boot instruction to start the OS from the USB Flash Drive

Step 3 – Configure your SD card

Using Disk Utility, format your SD Card using FAT32. It’s dead easy, choose your SD Card from the devices listed on the right, then click on ‘Erase’, choose FAT32 in the Formats list and click on ‘Erase’.

Once you’ve done this, you’ll notice on your Desktop, there are two mounted volumes; one is your USB Flash Drive, the other is your SD Card.

Open up the USB Flash Drive volume and copy all the files from that onto your SD card. This copies the all-important files and instructions to tell your Raspberry Pi to boot from the USB Flash Drive. We’re almost done at this point, only one more step to go.

Step 4 – Change the boot path on your SD card

Once you’ve completed step 3, you need to change the default boot path to tell the Raspberry Pi to boot from your USB drive. Open a new Finder window and go to your SD card. Open up the file called cmdline.txt in TextEdit or similar and amend the line which reads:

root=/dev/mmcblk0p2

To this:

root=/dev/sda2

This will instruct your Raspberry Pi to boot from the USB Flash Drive instead of from the SD card. Save the cmdline.txt file and close the Finder window. We’re almost done!

Step 4 – Boot from your USB Flash Drive on your Raspberry Pi

Now unmount both your USB Flash Drive and your SD card and pop them both into your Raspberry Pi and switch it on. If all goes well, it should boot from your USB Flash Drive which you’ll find substantially quicker than your SD Card.

Step 5 – Expand the Raspbian partition on your USB Flash drive to fill it

Finally, we’re just going to do a little housekeeping to utilise all the available space on your USB Flash drive as the method using raspi-config doesn’t work on USB Flash drives. This isn’t essential, but if you have the extra space on a USB Flash Drive, why not use it all?

From your Raspberry Pi, type the following command to start FDisk:

sudo fdisk /dev/sda

Then press p and enter to see the partitions. There should only be 2. What we’re going to do now is delete the Linux partition, but before we do this, we make a note of the start position for the linux partition sda2. Press d and then when prompted type 2 and then hit enter. This will delete the partition.

Now we’re going to create a new partition, and make it large enough for the OS to occupy the full space available on the USB Flash Drive. To do this type n to create a new partition, when prompted to give the partition type, press p for primary. Then it will as for a partition number, press 2 and hit enter.

You will be asked for a first sector, set this as the start of partition 2 as noted earlier. In my case this as 12280 but this is likely to be different for you.

After this it will ask for an end position, hit enter to use the default which is end of disk. Now type w to commit the changes. You will see a message about the Kernel using some table yaddah yaddah, just ignore this. Type the following to reboot:

sudo reboot

Once your Raspberry Pi has rebooted, we need to resize the partition. To do this type the following command:

sudo resize2fs /dev/sda2

Be patient, this will take some time. Once it’s done reboot again. Then type:

df -h

This will show the partitions and the space, you’ll see the full USB Flash Disk has all the space available now. That’s it, all done!

59 thoughts on “Install and run Raspbian from a USB Flash Drive

    1. Why thank you! Sorry it’s taken so long for me to write this. Next up – install WordPress without having to install a FTP server.

  1. Thank you so much for this! I use the Pi as an offline wireless server for meetings and events. It will be better to serve the web apps out of USB.

    1. Hi glad you found it useful, sorry its taken so long to write up! How have you found the raspberry pi for acting as a router performance wise?

    2. @Ebullient: How is that working for you? I’m considering using my pi for the same function. A web/file server for meetings where we need to share files or engineers need to download the latest releases, etc.

      1. I have used the wireless media server — not connected to the Internet — at various events and most recently with the Pi, to serve a website as a program guide to theatre attendees at an arts festival.

        That was using Lighttpd, but currently I use the somewhat simpler-to-configure Pancake webserver for Pi from pancakehttp.net.

        Also hostapd which enables use of a USB Wi-Fi dongle connected to Pi. (The dongle must be capable of acting as an access point — not all such devices are capable).

        Attendees tell me they prefer this over printed materials. It has amazed me how quickly they discover the site on their phones and tablets, even before announcements about it..

        All connections are redirected to the internal webserver. Going forward I am working on a control panel that enables the admin to change the Wi-Fi SSID, transmission channel, see a list of connected users and edit the website that is broadcast. And — shut down the Pi properly without SSH or access to the desktop GUI.

        Such servers could be used in situations where content and interactive apps should be broadcast in the immediate area, and Wi-Fi Internet connectivity is not provided, often for cost reasons. Other projects such as LibraryBox, PirateBox, Subnod.es, Byzantium Linux and OccupyHere are working on various pieces of this puzzle from other approaches.

        Thank You to the Dingleberry editor for focus on servers.

  2. This is a great guide, which I used and it worked really well. The idea behind this is to avoid wearing your SD card out from the IO required to run the OS, so you offload that to the USB stick instead – so it’s ironic how my USB stick died on me only 2 weeks later :-(

    For anyone else attempting this – I’d suggest you avoid Kingston DataTraveler Micro’s. They are really small and convenient for this purpose, but have a shorter lifespan than an SD card!

    1. Choosing the right USB Flash is important, I tried a few and found the Corsair Voyager III to be the best. They were quick and rock solid. The Sandisk flash are reliable, but not as quick.

      I’d say its really worth spending a little more on getting the Corsair for this purpose.

  3. Awesome, thanks for the great write-up. I can see a huge performance increase using a USB flashdrive for my OS. Question though. Can we delete the old boot, FAT32 partition and take advantage of the whole disk?

    1. Yea, you could do, there’s no reason not to! I think I should include this step in the tutorial. Glad you found it worked for you to get performance gains from your Pi

  4. First off, I would like to say that this site was the final push that got me to buy and set up a Pi to replace an older PC I was using as a personal Linux server. Bought it, configured it, and love it.

    Second off, thanks for this guide. I didn’t have a Mac but the steps are similar enough that I was able to use Ubuntu to do it.

    So here’s my current challenge. I have two USB disks plugged into my Pi: a USB flash drive from which the OS is run (per this guide) and a USB hard drive to store mass quantities of data. I find that it is impossible to say consistently which device will be sda and sdb when the Pi boots. I would love to be able to say “root=[UUID]” in cmdline.txt however Raspbian’s boot loader doesn’t work that way.

    Has anybody else encountered this challenge, or come up with a good solution?

    Thanks!

    1. Jesse,

      I haven’t tested this yet as I’ve just followed this tutorial myself, but I wonder if it would be possible to use a udev rule to create a symlink to the correct device on /dev.

      More information regarding udev rules on debian can be found on the Debian Wiki.

      I know in the past I’ve used a udev rule to make sure that any flash drive plugged in was automatically mounted to a specific position.

      /etc/udev/rules.d/81-flashmount.rules
      KERNEL=="sd*", SUBSYSTEMS=="usb", SYMLINK+="customer_drive"

      ACTION=="add", SUBSYSTEM=="block", RUN+="/usr/bin/flash_mount.sh add"

      ACTION=="remove", SUBSYSTEM=="block", RUN+="/usr/bin/flash_mount.sh remove"

      /usr/bin/flash_mount.sh
      #!/bin/bash
      #Accepts the input "add" or "remove" and accordingly mounts the
      #customers flash drive to /media/customer_drive or unmounts it.
      #The remove option may not be needed, but it has been kept in case
      #we decide to prompt the customer to remove it.

      usb_add() {
      mkdir /media/customer_drive
      mount -o uid=kiosk,gid=kiosk /dev/customer_drive /media/customer_drive
      }

      usb_remove() {

      umount /dev/customer_drive /media/customer_drive
      rm -rf /media/customer_drive
      }

      case "$1" in

      'add')

      usb_add

      ;;

      'remove')

      usb_remove

      ;;

      *)

      echo -e "usage $0 add|remove"

      esac

      exit 0

      In addition, I’ve also used this one-liner to make sure that a specific device (in this instance, a USB MagStrip reader) is always symlinked to the same position:

      /etc/udev/rules.d/50-usb-cardswipe.rules
      KERNEL=="hidraw*", SUBSYSTEMS=="usb", ATTRS{manufacturer}=="IDTECH", ATTRS{product}=="IDTECH MiniMag II USB-HID Reader", SYMLINK+="cardreader", MODE="0764"

      Hope that helps,
      -Luke

  5. Thanks.

    I think there may be a typo

    “sudo dd bs=1m if=2013-02-09-wheezy-raspbian.img of=/dev/disk2″

    barfs..I used “sudo dd bs=1M…”. Seems the m needs a cap. (dd:invalid number “1m”)

    1. Don’t quote me on this but I think that if the USB hub was a powered one, this may work. Never tried it though, but will give it a blast some time.

  6. Hi, great tutorial. Just one thing: Why is dd image so slow? Normal copy speed ~30MB/S for my usb but dd bs=1m only transfer at 1.6MB/s stable. How to fix it?

  7. do you need to change /etc/fstab at all for this to work properly? it appears to but i’m a sucker for doing things the linux way

  8. My Raspberry Pi (which I’ve only had for about a month) has an external USB hard disk – but it never occurred to me that I could make it boot from a USB device.

    I’d question the logic based on SD cards’ limited write cycle life time – that’s quite true, but I think a USB flash drive uses exactly the same technology and therefore also has limited write cycle lifetime. Are you sure you’re any better off from the reliability point of view?

    I’ve mounted my external HD on /home, also put a small swap partition on it, and intend to put any frequently written directories on it, so the SD card won’t be written to much – mostly log files. If it breaks, I’ll consider putting the whole system on HD, especially after reading this article.
    The HD data transfer is nearly twice as fast as the SD card, which makes such a change quite tempting.

  9. I agree with Anahata on this as aren’t they they same memory and tech on both the SD and USB stick? My thoughts however were on this matter as I have 1 coming and another being delivered a week after that. Would I be able to remove to SD card once the USB stick has booted and is running ? (This to start the process on a second pi ) I think this is a good question as in theory you could run multiple pi’s with only the one sd card for initial start-ups. I also wonder why the pi isn’t looking for a start-up disk on start up but that’s another already asked discussion I’m sure . Thanks for the tasty condiments to go with my pi.

    1. You’re both absolutely right, they are based on the same technology, except are intended to be used differently. USB Flash drives tend to be more reliable inherently as they are intended for regular data movement in a way that’s more irregular in size than SD. With SD, generally it tends to be the reading that lets them down, they’re designed for fast write due to their use predominantly in digital cameras.

      In my own tests on the Raspberry Pi, USB flash drives are considerably quicker. I tried several high-end SD cards, and found nothing to be a patch on the Corsair Voyager USB flash, and it didn’t take long for MySQL corruptions to occur on the SD cards either.

      As for not being able to directly boot from USB instead of having to rely on SD for the instruction, I haven’t a clue why this is, but I see it as a slight oversight of the Pi foundation, surely more people have a spare USB flash floating around than a SD? card!

  10. How would you go about removing the unnecessary FAT32 /dev/sda1 and expand /dev/sda2 to take over the space? I know it’s only, what, 58MB? Out of 32GB in my new USB flash drive… But it’s 58MB that could be in my main filesystem and instead is cluttering the partition table.

    I suppose we could install to a 2GB SD card temporarily, create a new blank partition on the USB drive, and dd it over. But that means starting over – I’m using a 128MB card for boot currently.

    For those who get confused about how to run the commands when the initial text-based setup menu is displayed: Alt-F2 and use the default pi login. Alt-F1 to go back to the menu.

  11. Yeah, I have to wonder about the requirement for booting from SD also. Maybe there’s a chipset-enforced rule. Would make more sense to me if they dropped the SD slot entirely and populated the physical space with another pair of USB ports.

  12. Ste,

    Great tutorial! Very well thought out and written.

    Just wanted to let you know that I was able to follow your directions to also install Pidora, merely replacing the Raspbian image with the Pidora one.

    -Luke

  13. Ste,
    Thanks for this great tutorial, its exactly what i was looking for !
    So due the fact that the Raspbian OS is on the USB flash drive, if i am using the command “install” to install let say PHP or something else, its going to be installed on the USB flash drive and not the SD card ? Or i need to use a more complex command line ?
    My concern is that i would like to install PHP and MySQL on the USB flash drive not the SD card.

    Thanks again.

  14. Help ! But I hope I help first with my shortcut if indeed it is. My error was tiny but my questions after are huge thats why I’d like a little help. I hope this shortcut is as good as I think. I had another sd card of wheezy so let that be a warning for fellow new guys if you try this easy approach. I opened the cmdline.txt in leaf pad in a root folder from the wheezy desktop and changed it to /dev/sda1 (by mistake the first try ! It needs to be 2 as 1 is the boot ) and saved it. I then put my other working sd card in a reader and it worked. I can edit that cmdline.txt file to go back to the original sd card later if I edit it. I did try a raspbmc install after I dd ‘d the image to a usb flash drive but had a fatal error after user pi set up. My thoughts are to try again with a dd’d sd to usb drive. Any thoughts though ? Can I have a drive partitioned say with multiple OS ‘s ? I did notice python not starting up on the raspbmc sd card I have through the a usb reader but it did run well as did wheezy. Any thoughts on all this guys ? I hope my questioning why not try this is a shortcut but is it problematic ?

  15. I haven’t tried it yet. However I got a question: Would you need the SD card any ways? I mean, even if you boot from USB flash drive? Tks.

  16. I wonder what size flash drive would be big enough to hold Raspbian comfortably and allow for future expansion. I plan on having all the operating system files on the flash drive and then using a hard drive attached to the Pi as storage to share over the network. Do you think 8GB would be enough or would 16GB or 32GB be better?

  17. @fboteroh,
    The presence of an SD card is required in order for the Pi to boot. This is just an inherent nature of the Pi’s design. As long as the SD contains the appropriate boot configurations, Raspbian can then be loaded off of anything, such as a USB drive as in this tutorial.

    @Megan M,
    I was able to get by just fine using an 8GB USB drive. With all said and done I have the OS occupying only 3GB of that. Because I use a USB hard drive to store the bulk of the data that my Pi serves (same as what you are describing), 8GB is plenty of breathing room for the OS.

  18. Very nice! Thanks a bunch! My Pi is rebooting as I type this after the last step and I’m as happy as can be! Super nice tutorial! Easy to follow and bang on!

    Thanks!

  19. nice!! but i think that berry boot is better as it helps to multi boot from a single source be it a usb drive or hard disk or sd card so u don’t need to do this

  20. I plan to build a music server with the Pi and a 1TB hard drive. Could I install the OS on this hard drive, or can you think of any reason why it would be better to use a second drive, a small USB stick? I will make regular backups of the 1TB drive either way.

  21. Thanks for the tutorial- I used an external 2,5″ HDD enclosure and a powered USB hub.
    With a 64GB Hard disk thePi works really nice, boot time is much shorter than from the SD card. I now use a 512MB SD card for booting (It could be even much smaller but this was the smallest one on the scrap pile).
    Under Linux if you have a working install of your raspi you can simply do the following:
    Take an USB external HDD, create 1 partition on it, mkfs.ext4 this partition and then copy everything from the Data partition of your SD card onto the USB harddisk.
    Change the cmdline.txt on the SD and the /etc/fstab on the disk and it simply works :-)

  22. TX very much for this tutorial. I followed step by step and its works like a charm. I am running Redmine on it and use a 64GB USB Sandisk FlashDrive. I am now looking to find a proper backup solution.

  23. Fantastic tutorial. Went swimmingly for me. Been battling sd card corruption for months now, on one of the Raspi’s. Really looking forward to not having to reinstall Raspbian every two weeks, here on out. Thanks!

  24. I have followed the instructions carefully, but neither the SD card nor USB flash drive (Verbatim Store-n-Go, 16gb) would boot (only the red pwr light ever shows up). When I restore cmdline.txt to original, the SD card boots fine and the USB drive appears fine also, although not mounted (of course). I do notice, however, that when the USB dive is plugged into my Win7 laptop, the file system shows as EXT3, not EXT4 as in the fstab config file. Could this be a problem?

    I have tried with various SD cards and USB flash drives and instructions from other websites, but all with same non-results. Any clues or things to look at such as log files, config files, etc.?

    1. I had no success for the first half dozen attempts. The way I finally made it work was to format my flash drive using gparted under Ubuntu. Delete all existing partitions from your usb stick, and then format the entire thing as one partition to ext4. Then follow the guide on this website verbatim – again using Ubuntu Linux – to ‘burn’ the .iso image of Raspbian to the usb stick. Most of the commands in the bash terminal in Ubuntu are very similar to the ones for Macs (or at least I gather from this article, I’ve never owned a Mac). Once the image is on the usb stick, the rest proceeded as advertised in this guide. Just change the cmdline.txt file per the instructions and away it goes.

  25. Great guide. This is the only one on the web I was able to make work. The one thing missing that you might want to include is that the USB stick should be formatted to ext4, because most of them come formatted xfat. I used gparted in Ubuntu to prepare the usb stick and it worked great.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>