Setup auto-mounting USB flash drive on Raspberry Pi

This tip is useful if you have scripts running that copy files to specific locations and you want the mounts to remain static. Although this is aimed at the raspberry pi it will also work on Debian etc you’ll just need to change the UID, GID etc.

I use ntfs drives so I can use them both on linux and windows so the first step was to install ntfs-3g
sudo apt-get install ntfs-3g

Insert the usb drive into a free port and run the following command to find out it’s UUID
ls -l /dev/disk/by-uuid/

My test drive showed this
lrwxrwxrwx 1 root root 10 Nov 16 20:52 DE64414D64412A1B -> ../../sda1

Make a note of the UUID – DE64414D64412A1B and the drive location /sda1

Create a mount point ie
sudo mkdir /man/usbflash

Set permissions
sudo chmod 770 /mnt/usbflash

Obtain the userid (UID) and groupid (GID) of the pi user
id

mine returned
uid=1000(pi) gid=1000(pi)

Mount the flash drive (note i’m using ntfs-3g for ntfs. Use vfat for FAT32 and ext4 for ext4)
sudo mount -t ntfs-3g -o uid=1000,gid=1000,umask=007 /dev/sda1 /mnt/usbflash

To make this persistant we update fstab but before doing so it’s a good idea to make a backup
sudo cp /etc/fstab /etc/fstab.backup
sudo nano /etc/fstab

Add the following to fstab (replace UUID UID GID and mount point with yours)
UUID=DE64414D64412A1B /mnt/usbflash ntfs-3g uid=1000,gid=1000,umask=007 0 0

Before mine looked like this
proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that

and after

proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
UUID=DE64414D64412A1B /mnt/usbflash ntfs-3g uid=1000,gid=1000,umask=007 0 0
# a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that

Reboot
sudo reboot

That’s it you should find you drive automatically mounted under the folder you specified ie /mnt/usbflash

You may also like

Leave a Reply

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