In this guide we’re going to use a Raspberry Pi to mount a USB drive. In this guide we show you both how Raspbian automatically mounts a drive and how to do it manually.
If you’re looking to have this drive accessible over your network, then the Raspberry Pi samba server is better suited for your needs.
If you want to see how this is done visually then be sure to check out my video below. It will take you through all the necessary steps to getting your USB drive mounted correctly. If you do like the video then be sure to subscribe to stay up to date on all my projects, guides and much more.
You will need some basic equipment for setting up a Raspberry Pi mounted USB drive.
Note: If you plan on using an external hard drive, then it is highly likely you will need a powered USB hub. This is because the Raspberry Pi is unable to output enough power via the USB ports to power the drive.
Raspberry Pi: Mount a USB Drive Automatically
In the latest version of Raspbian (Jessie) your USB drives should be automatically mounted when it is connected to the Pi. It is important to know if you do upgrade to Jessie from wheezy there might be compatibility problems with older projects & tutorials.
If you want to check where your drive has been mounted to you can simply use the following command:
sudo cat /proc/mounts
This will output quite a bit of text. Any USB drives are typically at the bottom of the text as shown in the image below.
As you can see my drive located at
/dev/sda1 has been automatically mounted to
The automatic mounting done by Raspbian will be fine for most projects and just normal use. It will retain its mount location whenever you remove and re-insert the drive since it is uses the UUID of the drive for the mount folder name. You can also find out the UUID by using the following command:
ls -l /dev/disk/by-uuid.
You might come across problems if you wish to allow access to the drive to a specific user that isn’t the default user. In our next step we will mount the drive using the fstab file and force permissions of a given user and group.
Raspberry Pi: Mount a USB Drive Manually
If you want to permanently mount the drive with a specific user, then we will need to setup the drive in the fstab file.
- Firstly install the following package if you’re using a NFTS drive.
sudo apt-get install ntfs-3g
- Now if you don’t know the drive UUID then we will need to get it. You can get a list of drives and their UUID that are currently connected to the pi by running the following command.
ls -l /dev/disk/by-uuid
- Now there is likely to be quite a few drives listed. Simply look for any drive that has an address of /sda* where * is a number. In my example it is 1 so we will use the UUID of CA1C-06BC to mount to.
- Now it is important that we create a clean directory to mount the USB to. To create the directory, enter the following:
sudo mkdir /media/usb1
- Next we will need to obtain both the uid and the gid. These numbers are important as we will need them to set the correct permissions for the USB drive. To get the gid enter the following command. (If you’re using a different user than pi then mare sure you update the username in the following commands).
id -g pi
- Get the uid enter the following command.
id -u pi
- Next we need to make an edit to the fstab file. This is the file that is called on boot up to setup the drives. To edit this file simply enter the following command:
sudo nano /etc/fstab
- Now add the following line to the bottom of this file. Remember to update the line with all the relevant details for your USB drive. (For example your drive will have a different UUID)
UUID=CA1C-06BC /media/usb1 auto nofail,uid=enter_uid_here,gid=enter_gid_here,noatime 0 0
- Now since the Pi automatically mounts the drive we will need to unmount the drive. A simple way to do this is to use the following command (Replace /dev/sda1 with the address relevant to your Pi).
sudo umount /dev/sda1
- Now you can use the following command and your drives should become mounted.
sudo mount -a
- If you want to make sure the drives are restored after the Pi has been shut down then simply run the following command:
- The drives should be automatically mounted after the Raspberry Pi has finished rebooting.
Hopefully you have your drive mounted to the Raspberry Pi now. If you have any troubles be sure to check out the troubleshooting guide below.
These following issues will most likely arise only when you’re manually mount the drive.
One of the biggest problems that you will come across with mounting a drive is permissions. If you’re finding that you can’t read/write files to the mounted drive than it is likely the drive is mounted as the wrong user or group.
Another problem you may come across is the drive not being mounted on boot. There have been some changes to Raspbian and the Raspberry Pi that might cause issues with mount the drive in time. The best way to work around this would be to add the following lines before the exit 0 line in the /etc/rc.local file.
sleep 20 sudo mount -a
Hopefully you are now able to setup a Raspberry Pi mount USB drive. If you are having issues with the mount, I missed something or anything else then feel free to drop a comment below. We also have plenty of Raspberry Pi beginner projects that you should take a look at if you’re looking to do doing something cool with your Pi.