Automounting A USB Drive In Raspbian

So you’ve decided that you would like more storage for your Raspberry Pi. Perhaps you are using it as a NAS, a Plex server, or you have some large libraries you are using for programming, and your SD card just doesn’t the space. Luckily you have a spare USB thumb drive or hard drive lying around. You plug it in…wait for it…huh…Read on to learn how to get your USB drive to automount everytime your Pi boots.

Initial Steps

Consider the Format

The first thing you should consider is how your drive is formatted. If the drive will be used with your Pi in addition to other non-Linux machines, FAT is probably your best bet (and likely how a thumb drive is already formatted). If you are going to use the drive exclusively with your Pi and other Linux machines, you may want to use a native format, e.g., ext4. Make the decision and (re)format your drive now, if necessary, because it will make a difference later.

Create a Mountpoint

Now choose a location at which to mount the drive. You can simply create a new directory in your home folder.

mkdir <directoryName>

Or you can follow convention and create a mountpoint in the /media/ directory. This requires a bit of extra work, since you have to have root to create the directory, and then change ownership of the directory to your user.

sudo mkdir <directoryName>
sudo chown <userName>:<usersGroupName> <directoryName>
Find the USB Drive’s UUID

Next you need to find the drive’s UUID. You may already be familiar with how your system identifies drives via the /dev/sdX naming scheme. While that designation is technically sufficient information for mounting your drive, it can change (especially if you are using multiple drives and moving them between different systems). If it changes (e.g., /dev/sdb becomes /dev/sda), your drive won’t mount, or the drive(s) may mount in (an) unexpected place(s). Let’s skip the possible frustration and find your drive’s UUID with the blkid command.

sudo blkid

Now just find your drive in the output and take note of two things: the UUID and the TYPE (drive’s filesystem). Below is sample output from my Pi.

/dev/mmcblk0: PTUUID="XXXXXXXX" PTTYPE="dos"
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="XXXX-XXXX" TYPE="vfat" PARTUUID="XXX"
/dev/mmcblk0p2: UUID="XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX" TYPE="ext4" PARTUUID="XXX"
/dev/sda1: LABEL="usbDrive" UUID="XXXX-XXXX" TYPE="vfat" PARTUUID="XXX"

Edit Your Filesystem Table

The filesystem table, located at /etc/fstab, consists of a single line per drive to be mounted. Each line has six fields that are separated by a single space. The fields (1) identify the drive, (2) tell the system where to mount it, (3) describe the filesystem type, (4) list the mount options, (5) specify whether the filesystem needs to be dumped and (6) specify whether and in what order fsck needs to run on the drive, respectively. For more information, see man fstab.

Now you are ready to edit /etc/fstab to direct your system to automount the USB drive every time the system boots. Remember when I said the filesystem would make a difference? Add the line below that corresponds to the filesystem on your drive (i.e., the TYPE listed in blkid).

Automount an EXT Drive
sudo nano /etc/fstab

Now add the following line to the file:

UUID=<driveUUID> <mountpoint> <TYPE> auto,nosuid,nodev,nofail 0 0
Automount a FAT (e.g., vfat, xfat) Drive

A FAT drive requires different options, because by default Raspbian will mount FAT partitions with root as the owner, and other users will not have the ability to write to files on the drive. You can get around this by overriding the default and making your user account and group the owner of the mounted partition. This requires you to know your username and group name. While you can use the alphanumeric names, it may be better to use the number that Linux assigns to your user and group. To find them run the following command and look for uid= and gid= (the default for both in Raspbian is 1000).

id <userName>

sudo nano /etc/fstab

Now add the following line to the file:

UUID=<driveUUID> <mountpoint> <TYPE> auto,uid=<uid>,gid=<gid>,nodev,nofail 0 0

Final Steps

Make sure to save your changes to /etc/fstab prior to exiting. Then you can test the changes you made with the following command:

sudo mount -a

If you don’t get any errors, you can reboot and your drive should be ready to go once your system comes back up:

sudo shutdown -r 0

Appendix

Here is a bit of information about the options being utilized when mounting the drive. For more information, and to confirm that this information is up to date, see man mount.

  • auto: allows the drive to be mounted with sudo mount -a
  • nodev: does not interpret character or block special devices on filesystem
  • nofail: if the device is not found or otherwise fails to mount, the system ignores it and moves on rather than reporting an error
  • nosuid: ignores set-user-identifier or set-group-identifier bits (essentially allows any user to mount the drive)
  • uid=, guid=: sets the owner (user and group) for the mounted partition (and its files) when mounting a FAT drive
comments powered by Disqus