Skip to main content

How to Run Raspberry Pi 4 or 3 Off an SSD or Flash Drive

By default, Raspberry Pi boots up and stores all of its programs on a microSD memory card, which has rather limited bandwidth. On the Raspberry Pi 4, the memory card slot can achieve a theoretical maximum of 50 MBps, which is double the 25 MBps maximum on the Pi 3B+ (and other 3 series). you get more speed if you attach an external drive to one of the Raspberry Pi's USB ports, especially with the Pi 4 offering USB 3.0 connections that have a theoretical maximum of 660 MBps. While we got around a third of that maximum in our real-life tests of a Raspberry Pi 4 with SSD, we were still impressed with how much faster app opens, web serving and file copying took place.

Unfortunately, at this time, you can't boot a Raspberry Pi 4 off an external drive. But you can force the Raspbian OS to use an external drive for its "root" partition, which holds all of its programs and data. So, in effect, you have a small boot partition on a microSD card but the meat of the entire operating system would still live on your speedy SSD or Flash Drive. A future firmware update will allow the Pi 4 to boot off of external drives, no microSD card required, The instructions directly below work on a current-day Pi 4 or an earlier model, but if you want to boot your Pi 3 off of an external drive scroll down to the next section of this page.

How to Run a Raspberry Pi 4 Off an SSD

The first thing you need to do is prepare your external drive so that all of the appropriate data is on it. To get started:

1. Connect your external drive to the Pi. Make sure to attach your drive to one of the blue-colored USB 3.0 ports.

2. Launch a terminal window on the Pi 4. You can do this either by clicking the terminal icon on the desktop, by hitting CTRL + ALT + t or by connecting to the Pi from another computer using SSH.

3. Use the fdisk -l command to see a list of all your drives.

sudo fdisk -l

You'll see a list of available partitions. Take note of the second partition on your microSD card; that's where your data is stored and, in my case, it is called /dev/mmcblk0p2, though you may not need this information. Also, remember the name assigned to your external drive, which is probably /dev/sda but could be different if you have more than one drive attached.

4. Launch fdisk, targeting your external drive.

sudo fdisk /dev/sda

A prompt will appear, offering you the change to see a list of fdisk commands.  

5. Type p at the prompt to see a list of current partitions on the drive.


You'll probably only see one partition here, but if there are multiples, you may want to delete them all.

6. Type d to delete the primary partition.  If you have many partitions you want to delete, you'll need to repeat this step.


7. Type n to create a new partition, hit p to make it a primary partition and type 1 so that it's partition 1 and hit Enter twice to select default beginning and last sections.

8. Type w to write your changes to the drive.

If you get an error message saying something like "Device or resource busy," reboot your Pi and try again. It could be that another process is using the external drive, preventing it from being partitioned.

9. Format the new partition in the ext4 file format. Note that the partition name will be the drive name with a 1 at the end (ex: /dev/sda1).

sudo mkfs.ext4 /dev/sda1

This may take a few minutes.

10. Create a new folder called /media/newdrive, which you will use to mount your external drive.

sudo mkdir /media/newdrive

11. Mount the new partition as /media/newdrive. Note that your partition name will have a 1 after it. So it will be /dev/sda1, rather than just /dev/sda.

sudo mount /dev/sda1 /media/newdrive

12. Copy all the files from your root folder using the rsync -avx command.

sudo rsync -avx / /media/newdrive

This process could take as long as a hour if you have a lot of data.

13. Open the /boot/cmdline.txt file for editing

sudo nano /boot/cmdlinetxt

You'll see cmdline.txt open in a text editor in your terminal. 

14. Paste the following text at the end of the first (and likely only) line of cmdline.txt.

root=/dev/sda1 rootfstype=ext4 rootwait

This text tells the Raspberry Pi to look at /dev/sda1 (provided that that is your external drive partition) as your primary storage drive. To boot, you'll need both the microSD card and your external drive connected. The Raspberry Pi cannot boot if the external drive is missing and this text is in the cmdline.txt file.

Hit CTRL + X to exit and Y (when prompted to save) then Enter to confirm your change.

If you ever want to comment this text out so you can boot and run off a single microSD card, you can just put the code on a second line with the comment # in front of it.

15. Reboot your Raspberry Pi 4. It won't boot unless the external drive is attached.

How to Boot Your Raspberry Pi Off a USB Drive

You can boot Raspberry Pis that are older than the Pi 4 directly off of USB. When you're done with the process, you won't even need to have a microSD card in the Pi's slot.

1. Create your external boot drive. There are two main ways to do this.

  • Use Etcher to "burn" the Raspbian OS to your external drive, a process we cover in our article on how to get started with Raspberry Pi. This works great, but doesn't copy over any data from an existing build.

  • Clone your current microSD card to the external drive using Raspbian's built-in SD Card Copier, which is loced  under the Accessories menu.

2. Create or find a bootable microSD card. If you already have a bootable microSD card, you can use it.

3. Connect both drives to your Raspberry Pi and boot it.

4. Open the the /boot/config.txt file for editing

sudo nano /boot/config.txt

5. Add program_usb_boot_mode=1 to the bottom of the file and save it.

program_usb_boot_mode=1

6. Reboot the Raspberry Pi with the microSD card inside and external drive connected. When it boots, the Pi will write a bit to the firmware, informing it to boot off the USB drive. You only need to do this once and you can remove program_usb_boot_mode=1 to from the config.txt file, because the change is now permanently part of the Pi's firmware.

7. Reboot the Pi again with the microSD card removed. It should now boot off of the external drive.

From now on, your Pi will boot off of its external drive, unless there's no external drive attached. If there's no bootable external drive, the Pi will boot off its microSD card.

  • rpi4_user
    I tried this verbatim and it doesn't seem like it was tested: the mounted /media/newdisk gets copied onto itself recursively. I think the rsync command needs an exclude. Also, most of us will be doing this with a fresh install, wouldn't it be a lot faster to flash the SSD with the buster zip file?
    Reply
  • TBoy2
    So now that I followed the instructions exactly, I have the same problem as rpi4_user. I am not an experienced Linux user so the comment "the rsync command needs an exclude" is probably correct but it doesn't help me. Is there going to be an update on the original post to fix the problem? Is there a simple method to recover from the infinite loop my rpi4 is now in? Thanks.
    Reply
  • mickwombat
    I have tried this also exactly and it doesn't work for me. The pi4 just hangs trying to start various services and failing. I did change the fstab entry also and still no luck.
    Thoughts?
    Reply
  • bmichaelb
    rpi4_user said:
    I tried this verbatim and it doesn't seem like it was tested: the mounted /media/newdisk gets copied onto itself recursively. I think the rsync command needs an exclude. Also, most of us will be doing this with a fresh install, wouldn't it be a lot faster to flash the SSD with the buster zip file?

    I just tried it, and it worked fine for me. It doesn't 'copy onto itself'...it's mounting the external drive into a folder, so you can drag and drop files into it by placing them into the folder. This is how you modify iso images. The only thing he seems to have forgotten, was to unmount the /media/newdisk after copying everything, but I suspect that happens anyway when you unmount the drive. Not sure...I unmounted manually...'sudo umount /media/newdisk'. The empty folder newdisk in /media still exists though, it would have been better to just 'mkdir ~/newdisk', then just delete it from the home directory once you're done.

    Other than that...the only other thing I changed, was to use the actual PARTUUID for the cmdline.txt file, as just using the /dev/sd* can cause issues if you plug another usb drive in later on, causing the /dev/sd* to change. So... run 'blkid' from the terminal, and look for your device's /dev/sd*, then find the PARTUUID at the end of the info. ' /dev/sdb1: LABEL="usbboot" UUID="57ea0cd6-c9ce-48b8-82f5-02076eeccd7e" TYPE="ext4" PARTUUID="b9c26a96-01" ' So, I put 'root=PARTUUID=b9c26a96-01'. You should also add it to the fstab, but I haven't gotten there yet. :p

    The only other thing I did...was to install and use gparted to delete,create,format the usb to ext4. So much easier than doing by command prompts. :p

    After all that though, the easiest and fastest way to do this...is to write the original image to both the sdcard, and the usb device. Then edit the /boot/cmdline.txt of the sdcard to use the /dev/sda2 partition. Then once booted and the initial install/configuration's done, you can edit the cmdline.txt file with the proper PARTUUID info, as well as add the info to fstab. That's how I did it with the RPi2B a few years ago. The original image was small enough that I could get away with just using a 2GB sdcard for the boot disk. :)
    Reply
  • ginandbacon
    Just wanted to add that I got this to work. A little trial and error but I got it working. I used this adapter with this drive. Updated the /etc/fstab accordingly. Only difference is I am using PARTUUID in fstab to set /root external drive and PARTUUID in cmdline.txt. The biggest difference I have found is extremely increased network speeds for transferring large files from the Pi to a network location or mounted drive with full Gigabit throughput (over 100MB/s). I also recommend using gparted to format the drive as I had sector alignment issues when using fdisk

    Also, I confirmed MSATA on my other Pi 4 using this adapter and this drive

    HDPARM MSATA results:
    pi@pihole:~ $ sudo hdparm -t /dev/sda1

    /dev/sda1: (msata drive)
    Timing buffered disk reads: 1064 MB in 3.00 seconds = 354.66 MB/sec

    pi@pihole:~ $ sudo hdparm -t /dev/mmcblk0

    /dev/mmcblk0: (sd card)
    Timing buffered disk reads: 130 MB in 3.01 seconds = 43.17 MB/sec

    pi@raspberrypi:~ $ sudo hdparm -Tt /dev/sda1

    /dev/sda1: (SSD)
    Timing buffered disk reads: 1070 MB in 3.00 seconds = 356.20 MB/sec

    /dev/mmcblk0: (sd card)
    Timing buffered disk reads: 130 MB in 3.03 seconds = 42.90 MB/sec
    Reply
  • stretch9x
    admin said:
    You can achieve faster storage speeds on a Raspberry Pi by putting your programs on an external drive. Here's how.

    How to Run Raspberry Pi 4 or 3 Off an SSD or Flash Drive : Read more
    Does not work on RPI4. Found typo
    admin said:
    You can achieve faster storage speeds on a Raspberry Pi by putting your programs on an external drive. Here's how.

    How to Run Raspberry Pi 4 or 3 Off an SSD or Flash Drive : Read more
    Does not work well on my 4gig pi4b. After spotting a typo on line 13 that left the period out of /boot/cmdline.txt, I finally managed to make the mod install but throughout the entire effort, I was plagued with this: Having a ssd on either USB3 port makes my usb2 wirelesss mouse/keyboard dongle very irratic. Tried a wired usb mouse/kb combo to no avail. Tried a usb2 SSD HAT ssd adapter with a different SSD but have the same issue. Spent a couple frustrating days trying to find a solution to this issue and gave up. The same SSD, adapter and mouse/kb combo works perfectly on my rpi3b+ after applying the "boot from usb" mod. I suppose there will be a lot of unhappy pi users that waited so long for the appropriate firmware update, Installed the 64 bit firmware upgrade today, Big improvement over the 32bit version on 64bit apps.
    Reply
  • stretch9x
    stretch9x said:
    Does not work on RPI4. Found typo

    Does not work well on my 4gig pi4b. After spotting a typo on line 13 that left the period out of /boot/cmdline.txt, I finally managed to make the mod install but throughout the entire effort, I was plagued with this: Having a ssd on either USB3 port makes my usb2 wirelesss mouse/keyboard dongle very irratic. Tried a wired usb mouse/kb combo to no avail. Tried a usb2 SSD HAT ssd adapter with a different SSD but have the same issue. Spent a couple frustrating days trying to find a solution to this issue and gave up. The same SSD, adapter and mouse/kb combo works perfectly on my rpi3b+ after applying the "boot from usb" mod. I suppose there will be a lot of unhappy pi users that waited so long for the appropriate firmware update, Installed the 64 bit firmware upgrade today, Big improvement over the 32bit version on 64bit apps.
    (solved it) : I did this:
    echo 2 sudo tee /proc/irq/55/affinity
    No trouble now when the SSD is in a USB3 port. All is good now. My PI-4B is now using my SSD for storage and my wireless mouse/kb combo is no longer dropping out.
    Reply
  • alevans51
    Since the update to Raspbian in September 2019 and other updates this routine no longer works. I recommend the following procedure: https://jamesachambers.com/raspberry-pi-4-usb-boot-config-guide-for-ssd-flash-drives/ and use gparted to expand and resize the second partition on the hard disk. BTW, I had to use the StarTech USB3 to SATA3 adapter to complete the project. Keep in mind you still need a microSD in the pi just for the boot, but then the hard disk does everything -EVERYTHING else. Nice.
    Reply
  • ogolyx
    rpi4_user said:
    I tried this verbatim and it doesn't seem like it was tested: the mounted /media/newdisk gets copied onto itself recursively. I think the rsync command needs an exclude. Also, most of us will be doing this with a fresh install, wouldn't it be a lot faster to flash the SSD with the buster zip file?

    The rsync command worked for me as shown without problems. The 'x' option in "rsync -avx ..." prevents the recursive copy into itself. -x is the short form for --one-file-system ("don't cross filesystem boundaries"). Note 'x' must be lower case; upper case 'X' has a different meaning ("preserve extended attributes").
    Reply
  • ogolyx
    I ran into a problem, because /boot/cmdline.txt did not exist, and creating a new one did not work.

    Turns out in the install image I started with (64-bit Ubuntu server from https://ubuntu.com/download/raspberry-pi), various boot configuration files are found in /boot/firmware/*.txt. /boot/firmware/README explains what they are and which are read when. For the procedure here to work, I needed to edit /boot/firmware/nobtcmd.txt instead of /boot/cmdline.txt.
    Reply