Mounting a USB drive can become a real headache and maybe a waste of time, especially if you are new on Raspberry Pi and Linux commands.
Today, I’ll give you all the exact information you need to mount a USB drive quickly and easily.
How to mount a USB drive on the Raspberry Pi?
Use the “mount” command to do this manually: sudo mount /dev/sda1 /mnt/usb.
The /etc/fstab file can also be edited for an automatic mount on startup.
There are plenty of options for both cases.
So, in this post I’ll explain how to connect your USB key or drive without having to remember 50 parameters 🙂
We’ll see the manual way (for temporary devices) and the automatic way for devices you want to often use on your Raspberry Pi (like your Retropie ROMs, movies or backup storage drive).
Get information about the USB drive
Before going further, we need to collect information about your hard drive, like the identifier and the file system type used on it.
We’ll also create a new folder to mount the drive in it.
Prepare your Raspberry Pi
There are not many requirements.
You can follow this tutorial with any Raspberry Pi OS version, and nearly any Linux distributions.
I’ll suppose you’re on Raspberry Pi OS Lite. If you have a Desktop, maybe you’ll get the help of the interface for some steps, but no big change.
Start by updating your system:
sudo apt update
sudo apt upgrade
And if you are working with NTFS drives, check that you have the required package installed (NTFS is mainly for Windows devices and may not be required in your case).
sudo apt install ntfs-3g
Plug your device
This step is easy 🙂
Plug your device to a free USB port.
For big external drives, you will need extra power to run the drive correctly (“Under-voltage detected!” will appear in the terminal).
Try to add a powered USB hub to the Raspberry Pi (check this one on Amazon for example if you don’t already have one). Your hard drive will not work without that (except if it has its power supply).
Plug the hub to the Raspberry Pi and your hard drive directly on the hub.
Collect more information
Once you plug the disk, we need to know more about this one before going further.
Fdisk is a tool to manage disks on Linux.
We’ll use it to display all disks and find your USB drive.
Start with this command:
sudo fdisk -l
At the end of the displaying lines, you should get something like this:
- First thing, be sure you’re checking the disk you want to mount
Mainly check the size of the drive to know if this is the good one (in this case I plugged a 8Go USB key, so I’m sure it is this one)
If not sure, unplug it and run the command again to see which one disappear 🙂
- Then remember two things:
- The filesystem format type: here it’s FAT32, it could be NTFS or EXT4 for example
- The device name: here it’s /dev/sda1, we’ll need this later
Another information that could help us later is the UUID.
When you format a disk, the system assign an ID to the disk.
We call this the UUID. This allows us to know this a known drive and do something specific when you plug it on your Raspberry Pi.
To get this UUID, run this command:
sudo ls -l /dev/disk/by-uuid/
You’ll get something like this:
Find the line corresponding to your drive name (sda1 for example).
Note the UUID just before the drive name (it could be longer depending on your disk).
Create the mount point
We are almost ready.
On Linux systems you need to create a new folder to mount the drive in it later.
Generally, we create it in /mnt or /media.
Create the directory:
sudo mkdir /mnt/usb
We are ready with prerequisites.
Now, we can mount the USB drive with two methods:
– manually: for fast access on temporary devices
– automatically: need more configuration to start, but this will be automatic next times
Manually mount the USB drive
In this part, we’ll see how to mount a USB drive quickly on the Raspberry Pi.
The mount command
The mount command allows us to mount a device on a specific folder.
In my case, I want to mount /dev/sda1 to /mnt/usb.
The command syntax is this:
sudo mount <DEVICE> <FOLDER> -o <OPTIONS>
So in my case:
sudo mount /dev/sda1 /mnt/usb -o uid=pi,gid=pi
Adapt this value to your system.
The uid and gid options allow pi to read and write files on the USB key
And then check you can see your files and create a new one:
ls -latr /mnt/usb
You’re ready to use it.
If you want to remove the USB key, you can dismount it with:
sudo umount /mnt/usb
Create a small script to save your preferences
Even if this was the manual way to mount a USB drive, I recommend saving this in a script if you are not familiar with this kind of command.
This will save you searching this page next time 🙂
Create a small script
- Create a new file
sudo nano /usr/local/bin/usb-connect.sh
- Paste these lines:
sudo mount /dev/sda1 /mnt/usb -o uid=pi,gid=pi echo "USB drive mounted successfully"
This is a basic script, adapt the values and add what you want
- Save and exit (CTRL+O, CTRL+X)
- Add execution permission:
sudo chmod +x /usr/local/bin/usb-connect.sh
Create an alias
- Edit your .bashrc file
- Add this line at the end of the file
- Save and exit
- Close the terminal or end your SSH connection
Next time you come into the terminal and plug your USB key, just use:
And then it’s done 🙂
No more mount command to remember.
Automatically mount the USB drive
You already know how to mount manually your drive each time you plug it, or boot your Raspberry Pi.
But if you use it a lot, or even let the drive plugged all the time, this is not the best way to do this.
You can configure your Raspberry Pi to auto-mount it on boot.
The /etc/fstab file
/etc/fstab is a configuration file to configure a mount point for each device.
We’ll save in this file all information needed to mount our USB drive to /mnt/usb.
Follow this procedure to add your USB drive in this file:
- Open /etc/fstab:
sudo nano /etc/fstab
- Add this line at the end:
UUID=2014-3D52 /mnt/usb vfat uid=pi,gid=pi 0 0
Replace the UUID by your own UUID you get in the prerequisites
Replace vfat by your file system if needed (ntfs or ext4 for example)
As you may notice, the options column with uid and gid play the same role as for the manual mount, we give access to the pi user with this
- Save and exit
- Reboot or try it directly with:
sudo mount -a
Your USB drive should now be available in the /mnt/usb folder
And Raspbian will mount it automatically at each boot
If you want to add it after the boot, just run mount -a again, or mount /mnt/usb
Using the UUID rather than the device name (/dev/sda1) allows us to be sure this is the correct device.
When you use multiple USB key for example, the first connected will be sda1 but you can’t know which one it is physically.
With the UUID, you’re sure this is the good one.
You can create a mount point for each device if you want (/mnt/big_drive, /mnt/kingston_key, …).
There is no partition on my USB key, so I’m not able to mount it, what should I do? The easiest way to create the first partition is to insert this key in a desktop OS (Windows or a Pi Desktop with Gparted for example). If you want to do this on a Raspbian Lite, use the mkfs command: sudo mkfs -t fat32 /dev/sda1. More information here.
You can now use USB drives on your Raspberry Pi, either manually (with mount) or automatically on the boot (with fstab).