Skip to content

Instantly share code, notes, and snippets.

@chasgames
Last active February 11, 2021 17:10
Show Gist options
  • Save chasgames/7e11d5cf9a2f44c435e5b11c419b6621 to your computer and use it in GitHub Desktop.
Save chasgames/7e11d5cf9a2f44c435e5b11c419b6621 to your computer and use it in GitHub Desktop.
Auto mount USB drive using systemd (Ubuntu/Debian)

Auto mount USB using systemd (Ubuntu/Debian)

NO fstab, just plain ol systemd. (systemd.mount systemd.automount features)

Note, if your USB is formatted with FAT32 and NTFS then everything will be locked to root, chown and chmod will not work. Format the USB with Ext4 otherwise to play nicely with linux users.

Get name of USB drive

lsblk

Get UUID of USB drive

Now you know the name , we need to get the UUID of the USB , which doesn't change unless re-formatted.

blkid

Create systemd mount file

This filename MUST match the location you specify for it in the "Where=" bit. Don't ask me why.

e.g name the file mnt-rrdusb.mount if you are mounting to /mnt/rrdusb

Insert your UUID into the file below.

/etc/systemd/system/mnt-rrdusb.mount

[Unit]
Description=Mount RRDusb

[Mount]
What=/dev/disk/by-uuid/PASTE-YOUR-UUID-HERE
Where=/mnt/rrdusb
Type=auto
Options=defaults

[Install]
WantedBy=multi-user.target

Create systemd automount file

/etc/systemd/system/mnt-rrdusb.automount

[Unit]
Description=Automount usb

[Automount]
Where=/mnt/rrdusb

[Install]
WantedBy=multi-user.target

Lets Go! Mount & Reboot proof

systemctl daemon-reload
systemctl enable --now mnt-rrdusb.mount mnt-rrdusb.automount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment