how to mount usb drive

How to mount a USB drive on the Raspberry Pi?

If you are new on Linux and especially on Raspberry Pi, mounting an USB drive could become a real headache and maybe a waste of time
Today, I’ll give you all the need informations to mount an USB drive quickly and easily

How to mount a USB drive on the Raspberry Pi?
To mount a drive on the Raspberry Pi, you have two choices:
– Manually mount it with the /mount command each time you want to use it
– Automatically mount it, by adding it in the /etc/fstab file with all the parameters needed
There are a lot of options, and it’s not intuitive for a beginner

So in this post I’ll explain you step by step 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 your want to use often on your Raspberry Pi (like your Retropie roms, movies or backup storage drive)

Things to know about your USB drive

Before going further, we need to collect informations about your hard drive, like the identifier and the filesystem type used on it
We’ll also create a new folder to mount the drive in it

Prepare your Raspberry Pi

There are not a lot of requirements
You can follow this tutorial with any Raspbian version, and nearly any Linux distributions

I’ll suppose you’re on Raspbian Lite, if you have a Raspbian with 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 needed 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 may 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)
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

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:
fdisk command output

  • 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

UUID

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 allow 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:
get disk uuid

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 could 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 your USB drive

In this part, we’ll see how to mount an 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 base 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 own 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
touch /mnt/usb/test

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
    #!/bin/bash
    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
    nano ~/.bashrc
  • Add this line at the end of the file
    alias usbmount='/usr/local/bin/usb-connect.sh'
  • Save and exit
  • Close the terminal or end your SSH connection

Easy mount

Next time you come into the terminal and plug you USB key, just use:

usbmount

And then it’s done 🙂
No more mount command to remember

Automatically mount your 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 manager mount point for 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 own 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 good 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 could create a mount point for each device if you want  (/mnt/big_drive, /mnt/kingston_key, …)

Related questions

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

Conclusion

You are now able to use USB drives on your Raspberry Pi, either manually (with /mount) or automatically on the boot (with fstab)

 

Leave a Comment