Imagine this situation. You have attached an external USB or SSD to your system and using it as an additional storage media. This is a common use case specially if you have set up a media server on Raspberry Pi or any Linux system.The good thing is that you can solve this problem by ensuring that the external disk automounts.If you want the external drive to mount only when it’s plugged in, udev
rules and autofs
provide a dynamic approach. ACTION=="add", KERNEL=="sd*", ENV{DEVTYPE}=="partition", ENV{ID_BUS}=="usb",
SYMLINK+="usbdisks/%k", MODE:="0660",
RUN+="/bin/ln -sf /media/hdd/%k /media/usb-sticks/%k"
ACTION=="remove", KERNEL=="sd*", ENV{DEVTYPE}=="partition", ENV{ID_BUS}=="usb",
RUN+="/bin/rm /media/usb-sticks/%k"
Let’s see them one by one.
- Configuring the
fstab
file for startup mounting - Using
udev
andautofs
for more dynamic setups
/dev/sdb2: UUID="12ab345cd-1234-4166-8539-ff4ff3ff2ff1" TYPE="ntfs"
- Edit the
/etc/fstab
file with your preferred text editor (you’ll need super-user rights):
sudo nano /etc/udev/rules.d/usb_auto_mount.rules
The approach you choose between fstab and udev depends on your specific needs whether you prefer the drive to be ready at boot or only when plugged in.
Steps to Automount via fstab
:
- First, connect your USB drive and run the following command to identify the device name and UUID:
💬 Do let me know if you have questions or face issues while automounting the external disks in Linux.UUID=12ab345cd-1234-4166-8539-ff4ff3ff2ff1 /media/hdd auto defaults,nofail,x-systemd.automount 0 2
With these steps, you can ensure that your external storage is always accessible without manual intervention.
- Reload systemd to apply the changes:
On Linux, you normally have to click on the mount option in the file explorer or use the mount command to start using the external drive.
Method 2: Automount Using Udev Rules and autofs
This method is reliable for drives that remain connected during system boot and I mostly use this method for my homelab.sudo blkid
I was backing up crucial files and using the drive for remote access. Everything was going smoothly until a reboot occurred. #!/bin/bash
fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/)
if [ "${fstype}" = "vfat" ] ; then
echo "-fstype=ntfs,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/${key}"
exit 0
fi
exit 1
Automounting the drives can significantly streamline your workflow. This I realized with my personal experience.sudo udevadm control --reload-rules
- Edit the
auto.master
file to instructautofs
to mount the USB drive:
💡/media/hdd /etc/auto_mount.usb --timeout=60
sudo nano /etc/auto_mount.usb
sudo systemctl daemon-reload
Save and exit the file. The drive will now automatically mount in /media/hdd/
when connected and unmount after 60 seconds of inactivity. It was frustrating because the whole point was to make the system run seamlessly without constant supervision. I experimented with automounting the drives and decided to share my experience with you.This method is more suitable if you frequently swap USB drives or prefer not to have them mounted at startup.The same method can also be utilized when you are using a dual boot system and you want to automount the Windows partition(s).This is an incontinence and it could also be problematic in situation where you require the disk to be mounted like internal disks when the system starts. Imagine the media server not showing any media because the media was stored on external disk and it was not mounted.A few days ago, while setting up an external drive as part of my DIY NAS (Network Attached Storage) on a Raspberry Pi, I encountered a frustrating issue.If you want the external drive to automatically mount whenever your system starts, you can configure it using fstab
(File Systems Table).