How to format and mount a USB drive on Raspberry Pi?

format and mount usb key on raspberry pi

Mounting and formatting USB drives on Linux are not the easiest things to do
Especially if you are on a console only operating system, like Raspbian Lite
Today, I’ll show you how to manage this on your Raspberry Pi, whether in terminal or graphic mode

How to format and mount a USB drive on Raspberry Pi?
Working with USB drives on Linux in a 3 steps process :

  • Identifying your device properties (file system, location, partitions)
  • If needed, formatting the drive the way you want (file system especially)
  • Mounting the device with the desktop or a command line

In this guide, I’ll explain all the things you need to know, no knowledge required for now
I’ll start with a file system introduction, and then show you how to do the 3 steps above

File system theory

Let’s start with a short introduction about things you need to understand before going further

File systems

Basically, file systems are the way your files will be saved on the device
No matter if it’s a USB drive, a hard disk or a SD card
It’s a convention on how files are stored on the disk if you want

Each operating system has its favorite file system, but Linux, and so Raspbian, can read and write any file system
Sometimes it may require an additional package, but it is possible

Here are the most used file systems right now :

  • Linux: EXT3 and EXT4
  • Windows: NTFS and FAT32
  • Apple: APFS, HFS+

Each one has different advantages and disadvantages, but it’s not the goal of the post today
Most of the time you will use EXT4 to keep your USB drive on Linux / Raspbian, and one of the others if you want to make it readable on another platform

Partitions

Another thing to understand is “partition”
As the name says, a partition is a specific part on the disk

On Windows, you can have C: and D: for example
On Linux, you generally have a /boot, / and swap partition

For a USB key you will have only one most of the time
But it’s something to know if you try to read a hard drive from an old computer for example
You can also have one partition for Linux and another for Windows on the same USB drive (FAT32 and EXT4 for example)

Devices management on Linux

On a desktop operating system, managing internal or external hard drive is easy
Everything is plug and play, or you have a tool to do all the complicated stuff for you

But without desktop, you need to know how it works behind 🙂
When you plug a device on your Raspberry Pi, the system will assign it to a virtual disk, in the /dev location, for example /dev/sdb
The partitions of this disk will be accessible on /dev/sdb1, /dev/sdb2, etc
(for a USB drive you probably have only one)

But you can’t access files directly here, it’s a virtual drive
Before to use it, you need to mount the drive in another location
Most of the time it’s in /media : /media/usb for example

In this guide I’ll show you how to do this, but you need to understand how it works before trying something

Format the USB drive from the Raspberry Pi

There isn’t really a right order in the way to show you format and mount
So if your drive is correctly formatted and you just want to mount it on the Raspberry Pi, you can skip this part and go directly to the mounting step

If you are still here, I’ll show you how to format a USB drive from both environment (Desktop or Lite)

Raspbian Desktop and Full

The graphic way is the easiest way to deal with this
You have a tool named “gparted” that do all the work for you :

  • If needed, install “gparted” from the main menu > Preferences > Add / Remove software
    I have it on Raspbian Full, but it may not be available on Desktop, I don’t know
  • Then, open the main menu again, and go to System tools > GParted
  • Enter your password, and you’ll get a window like this:
    gparted partition manager
  • Then right click on the partition and choose “Format to” to select the file system you want to use
  • Click on the green tick on top to confirm and apply changes

That’s it, you know how to format a USB drive on Raspbian Desktop 🙂
If you don’t already have a partition on your drive, it’s the same process (as simple as right-click > new)

Raspbian Lite and command line

As usual, the Raspbian Lite way is less straightforward 🙂

Find the virtual drive name

Before doing anything else, you need to find the device name
Follow this steps to find it :

  • We’ll use “fdisk” to list the current devices on the Raspberry Pi
  • Use the command below :
    sudo fdisk -l
  • A list will appear, and most of the time your USB drive is the last item, so it’s easy to read
    In my case I can see this :
    fdisk raspberry pi
  • Take note of the partition name (under Device on the last line)
    If no partition, just remember the Disk name (/dev/sda here)

We can now move to the format part

Format or reformat the entire disk

If you need to create a partition, or want to rebuild a new partition table, you have to use fdisk again

Here are the steps to follow to create a new partition:

  • In this case, I have an empty USB key, without partition (fdisk -l)

    So I can’t mount it or format it directly, I first need to create one
  • Start fdisk
    sudo fdisk /dev/sda
  • Use the following commands shortcuts in fdisk:
    • Create a new partition table : g (for GPT, use help for other format)
    • Create a new partition : n
      You can keep the default values for a single partition
    • Confirm with Y to remove the signature
    • And finally write and exit fdisk: w
  • When you run fdisk -l again, you’ll see a new partition
    You can follow the next part to format it the way you want

Format the partition

If you already have a partition on the drive, and just want to erase it, or maybe change the file system type, here is what you need to do:

  • The command format is easy, the syntax is the following:
    sudo mkfs.FILETYPE DEVICE
    Just replace FILETYPE and DEVICE by what you want
  • Here are the most common examples:
    sudo mkfs.ext4 /dev/sda1
    sudo mkfs.vfat /dev/sda1
    sudo mkfs.ntfs /dev/sda1

    That’s all

Mount your USB drive

Let’s see now how to mount a USB drive on your Raspberry Pi 🙂

Raspbian Desktop and Full

On Raspbian Desktop (or Full), it’s straightforward
The operating system manages everything for you 🙂

  • Plug your device to a free USB port
  • Wait a few seconds, a window shows up asking if you want to open the file manager to access it
    insert usb drive raspberry pi
    The device is already mounted
  • Click “OK” to open the file manager
    An icon is also available on the desktop to access it later

Note: I’m doing this tutorial with Raspbian Full, if you have any issue with Raspbian Desktop, check the prerequisites in the following part

Raspbian Lite and command line

On a Lite version, it’s not the same thing
You can plug your drive, and nothing happens 🙂
There are a few steps to follow to access it

Find the virtual drive name

Note: skip this if you already learn how to do it in the format part 🙂

Before doing anything else, you need to find the current name of the drive
Once you plug it, Raspbian will detect the drive and partitions, and assign a name to it
Follow this procedure to find it :

  • We’ll use “fdisk” to list the current devices on the Raspberry Pi
  • Use the command below :
    sudo fdisk -l
  • A list will appear, and most of the time your USB drive is the last item, so it’s easy to read
    In my case, I have this :
    fdisk raspberry pi
  • You have two things to note (squared in red):
    • The partition name given by Raspbian: /dev/sda1 here
    • The file system type: FAT32 here
  • Once you know this, you have everything you need to move forward

If you are not sure of which one is your drive, the disk size may help to select the good one (7.5G in my case)
My SD card is 32G and is always identified as /dev/mmcblk0

Mount the drive

Now that you know the device and partition name, use the following procedure to mount it :

  • Create a new folder in /media
    We’ll mount the USB drive in this folder, but you need to create it before
    sudo mkdir /media/usb
  • Mount the drive to this place with this simple command:
    sudo mount /dev/sda1 /media/usb -o umask=000
    Don’t forget to replace parameters if needed
    The syntax is “mount [PARTITION] [FOLDER]”
    The -o allows you to add extra options
    Most of the time you will need to add the umask option to allow the “pi” user to write on the device

There are many other options you can use
Check the “man mount” command or the online help here to learn more about this

Unmount the drive

To update the drive, you have to use “umount” like this:
sudo umount /media/usb

It’s the correct way to unplug your device, to be sure everything will be saved

Automatic mount on boot

As you’ll quickly see, you need to use the mount command each time you reboot your Raspberry Pi

But there is something you can do to mount it automatically on boot:

  • Get the partition id of your device
    sudo blkid
  • You’ll get something like that

    The PARTUUID is the value square in red, on the /dev/sdb1 line
  • Open the /etc/fstab file
    sudo nano /etc/fstab
  • Paste this line, or something similar depending on your values :
    PARTUUID=b951dfde-01 /media/usb ntfs defaults,umask=000 0 0
  • Fstab is a file to store drives and options related to it
    And if the drive is plugged on boot, it will mount it directly
  • Save and exit (CTRL+O, CTRL+X)

Now you can reboot your system and check that everything works fine

Conclusion

That’s it, you know everything you need to manager USB drives on your Raspberry Pi
And it’s the same commands on Linux, to manager any disks (external or internal), so great work!

Do you have other tips to share to manage USB drives on Raspberry Pi?

Leave a Reply

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