Using a USB Drive with Your Raspberry Pi: Tips and Tricks

A USB drive is an easy way to give yourself some extra storage space on your Raspberry Pi, since the micro SD card often does not have enough space to hold all the necessary files we need to keep. Using a USB drive is also a handy way to quickly transfer files to your Raspberry Pi from another device, or vice versa. It is often necessary to do a little configuration before using a USB drive, such as formatting the device, mounting it, etc. In this tutorial, we will go over some tips and trick for using a USB drive with a Raspberry Pi.

In this tutorial you will learn:

  • Can I use USB 3.0 on a Raspberry Pi?
  • How to format a USB drive on Raspberry Pi
  • How to mount a USB drivev on Raspberry Pi
  • How to set USB drive to auto mount on Raspberry Pi
Using a USB Drive with Your Raspberry Pi: Tips and Tricks
Using a USB Drive with Your Raspberry Pi: Tips and Tricks
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Tips and tricks for using a USB drive on Raspberry Pi

Newer Raspberry Pi models come with USB 3 and USB 2 slots. It is well worth it to take advantage of these USB 3 slots if you plan to use USB storage with your Raspberry Pi. This will result in much faster file transfer speeds, and it is a big performance increase from using a USB 2 drive or the USB 2 slots.

Below we will go through the first stages of setting up a USB drive to work with our Raspberry Pi. We will assume that the USB drive has not yet been formatted and you need to configure it for use on the Raspberry Pi.

  1. Once your USB drive is inserted in the Raspberry Pi, let’s figure out how to identify the USB drive that we wish to format. The name should start with /dev/sd and then a letter. Type the following command in terminal to see:
    $ sudo fdisk -l
  2. In our example, the USB disk has been assigned /dev/sdX. To create a new partition on the empty USB flash drive, we will provide it as an argument to the gdisk utility:
    $ sudo gdisk /dev/sdX
  3. The gdisk utility awaits our commands. We would like to create a new partition, so we press n.

    Command (? for help): n
  4. This will be the first partition that we are creating on this USB flash drive, so the answer to the next question is 1.
    Partition number (1-128, default 1): 1
  5. The next questions are about first and last sector, which will determine the actual size of the partition. In our example we are creating a single partition that will cover the entire USB flash drive, and default values are first partition, first available sector to start, and last sector to end with, which is just what we need. So we will accept the defaults for these questions by simply pressing the Enter key.
    First sector (34-6291455966, default = 2048) or {+-}size{KMGTP}: 
    Last sector (2048-6291455966, default = 6291455966) or {+-}size{KMGTP}:
  6. The next question asks us what kind of file system this partition will be for. We need to enter a hex code that corresponds to our selection. However, the default response is ‘Linux filesystem’ which is exactly what we need. We will once again press the Enter key to accept this default value.
    Current type is 8300 (Linux filesystem)
    Hex code or GUID (L to show codes, Enter = 8300):
    Changed type of partition to 'Linux filesystem'
  7. The partition is now complete, but as the utility points out on start, the changes are in memory only until we write them out to the USB disk. This is on purpose and the warning is in place for a good reason: by writing out the changes to the flash drive, we destroy anything that resided on the sector range we cover with our new partition. We are sure there will be no data loss, so we write the changes to disk with the w command:
    Command (? for help): w
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/sdX.
    The operation has completed successfully.

    You will also need to reply Y to the confirmation prompt to verify that you indeed want to write these changes to the disk, and overwrite existing data.

  8. Since our block device is /dev/sdX, and we just created partition number 1 on the USB drive, that means our new partition is accessible under the path /dev/sdX1. Next, we still need to add a file system to our USB drive. Use the mkfs command to format the flash drive with the ext4 file system.

    $ sudo mkfs -t ext4 /dev/sdX1
  9. We will now use the mount command to mount the newly formatted partition on our Raspberry Pi. We will mount our flash disk drive to the /media/flashdrive directory.
    $ sudo mkdir -p /media/flashdrive
    $ sudo mount /dev/sdb1 /media/flashdrive

    You can now access your newly formatted flash drive under the /mount/flashdrive directory or where ever you decided to mount it.

  10. To make the USB drive get mounted automatically on your Raspberry Pi, see our guide on configuring the /etc/fstab file.

Closing Thoughts

In this tutorial, we saw some tips and tricks for using a USB drive on a Raspberry Pi. This included highlighting the differences between USB 2.0 and USB 3.0 for the Raspberry Pi, how to partition a USB drive, how to format and add a file system to the drive, how to mount it, and how to mount it automatically when the Raspberry Pi first starts up.

Comments and Discussions
Linux Forum