Plug a USB drive in and it is mounted automatically under /media/usb-share/<label>, then shared
on the LAN via a guest-visible Samba share. A udev rule triggers a systemd template service on
every hotplug event; the mount/unmount logic lives in a short bash script.
| File | Purpose |
|---|---|
99-usb-mount.rules |
udev rule: fires usb-mount@ for any USB block device with a filesystem |
usb-mount@.service |
systemd oneshot: calls the mount script |
usb-mount.sh |
Bash: mounts on add, unmounts & cleans up on remove |
samba_snippet.conf |
Samba [USB-Share] block to paste into smb.conf |
# Install files to the right locations
sudo cp usb-mount.sh /usr/local/bin/usb-mount.sh
sudo chmod +x /usr/local/bin/usb-mount.sh
sudo cp 99-usb-mount.rules /etc/udev/rules.d/
sudo cp usb-mount@.service /etc/systemd/system/
sudo udevadm control --reload-rules
# Merge the Samba share into your smb.conf
# (edit path / force user to match your system)
cat samba_snippet.conf >> /etc/samba/smb.conf
sudo systemctl restart smbd- A USB drive with a recognised filesystem appears → udev fires
usb-mount@<dev>.service. usb-mount.sh add <dev>creates/media/usb-share/<label>, picks the right mount options (FAT/NTFS/exFAT get explicit uid/gid/umask for Samba compatibility), and mounts it.- On removal the script unmounts, deletes empty subdirectories, and cleans up.
- Samba makes the root of
/media/usb-sharereadable (and writable) to guest users.
The included Samba snippet uses guest ok = yes for easy LAN sharing — tighten this for
public or remote-facing servers. The mount options nosuid,nodev,noatime are applied to all
filesystems to prevent privilege-escalation via SUID binaries on the drive.
- Short description:
Zero-touch USB auto-mount + Samba LAN share for Linux servers. - Suggested topics:
samba,usb,udev,systemd,linux,network-share,homelab,automation - License recommendation: MIT.