Automatically Mount an External USB Hard Drive on Raspberry Pi OS Lite

Photo of a couple of Raspberry Pi devices with an external USB drive for storage.
Photo by Jainath Ponnala / Unsplash

The Raspberry Pi has evolved significantly since its debut. I recently bought a Raspberry Pi 5 kit to revisit home lab projects and electronics, having previously used a Pi 2 for small server experiments. Now, I'm exploring Docker’s advantages on my 8GB Pi 5, but the main drawback is being limited by my 128GB SD card running Raspbian OS Lite, which restricts storage for future Docker projects.

After getting a Synology NAS, I had some spare USB drives. For my first Pi 5 project, I set up a 4TB NTFS formatted external drive to auto-mount at boot as extra storage for large data, especially Docker projects. I kept the NTFS formatted file system so that I can unplug the drive and connect it to my Windows PC anytime I wanted to connect the drive for any reason. Here’s how I did it:

  1. Connect the USB storage drive to the USB 3 port on the Pi 5.
  2. Connect to my Pi 5 via SSH.
  3. On the terminal run the command id to identify your uid and gid numbers.
CLI image output of the "id" command with annotation around uid and gid which shows 1000.
In this example, both the uid and gid is 1000
  1. Run the command sudo blkid and identified the UUID of the device you wish to auto-mount at boot.
A cli terminal with an annotation around the UUID.
The UUID is unique to your external drive.

3.      Create a new folder under /mnt to set the mount point for the USB drive.

sudo mkdir -p /mnt/wd4tb

4.      Edit the /etc/fstab file with my entry to auto-mount the drive at boot.

sudo nano /etc/fstab

Then, within the file, I added the following entry to the end:

UUID=####-#### /mnt/wd4tb ntfs default,nofail,x-systemd.device-timeout=30s,uid=1000,gid=1000 0 0

  • UUID: this is the information obtained when you ran the sudo blkid, replace the values with the UUID of your device. This allows the system to identify the device by its unique ID instead of device mount name (ex. /dev/sda2 or /dev/sda1, etc.)
  • /mnt/wd4tb: this is the directory we created where we would like to mount our drive.
  • ntfs: This is the formatted drive type and requires you have the ntfs-3g package to have full read/write support.
  • default: This is the standard mount options which includes rw, suid, dev, exec, auto, nouser, and async. These are safe and typical flags for most use cases.
  • nofail: Provides boot resiliency and prevents the system from failing to boot if this drive is missing or not ready. Essential for removable or external drives.
  • x-systemd.device-timeout=30s: This is the systemd-specific wait time. It tells systemd to wait up to 30 seconds for the device to become available before giving up. Useful for slow-to-initialize USB drives.
  • uid=1000,gid=1000: Ownership mapping. Ensures the mounted files are owned by the user with UID 1000 and GID 1000 (usually the default user). This is crucial for NTFS and FAT32, which don’t support native Unix permissions.
  • First 0: Means dump flag and controls whether the filesystem should be backed up by the dump utility. 0 means no.
  • Second 0: Means fsck order. Controls the order in which filesystems are checked at boot. 0 means don’t check this filesystem (common for NTFS and external drives)
  1. Once, you are ready to save the file, use Ctrl-X to save, then type “y” to confirm the save.
  2. Mount the drive using the following command:

sudo mount -a

  • This will mount your drive to the set mount point at /mnt/wd4tb. Data can now be written and read from this location and can be used for Docker volumes and storage.
  1. Reboot the device for good measure.

sudo reboot

Once the device has rebooted, you can now use your automatically mounted external hard drive.


❤️Enjoyed this content, why not support me!

All support is appreciated! 😁