I want to hot plug an external hard drive, have it mount and automatically run the Unison File Synchronizer.

I have made a udev rule called /etc/udev/rules.d/100-sync.rules

ACTION=="add", SUBSYSTEMS=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="wxyz", ATTRS{idProduct}=="wxyz", RUN+="/home/william/sync.sh"

It triggers this script:

mkdir /media/william/Backup
mount -U <my_drives_uuid> /media/william/Backup
su william -c 'notify-send "Unison" "Starting Sync"'
su william -c 'unison Desktop_Ext_local_connection.prf -silent > /home/william/unison_errors.log'
su william -c 'notify-send "Unison" "Finished Sync"'
umount /media/william/Backup
rmdir /media/william/Backup

Running the script from a root shell works as expected, however when it is triggered by udev, the drive does not mount. (I still get system notifications and directory creation/destruction)

Based on this, I think it doesn't work because udev uses a different namespace. I've tried creating a file /etc/systemd/system/systemd-udevd.service with the contents

MountFlags=shared *(and alternatively PrivateMounts=no)*

However this does not change the behaviour - it still doesn't mount. (yes I restarted the daemon)

How can I get the drive to mount using udev?

After my script is done, Ubuntu automatically mounts the drive. Is there a work around which lets Ubuntu mount the drive before udev runs my script?

I cannot see any udev rule which Ubuntu uses to mount the drive, the contents of /etc/udev/rules.d are just

100-sync.rules  70-snap.snapd.rules  70-snap.snap-store.rules

So how is Ubuntu mounting the drive after my script is done, and how can I change the order so that they are mounted first?

I am using Ubuntu 20.04.3 LTS, the same issue occurs on my laptop running 21.04

  • I've found a work around where I have 2 scripts. The first script starts the second using nohup then exits, allowing Ubuntu to mount the drive. The second waits 10 seconds to allow time for the mount then runs Unison. I do not find this work around very satisfying. Dec 1, 2021 at 7:46
  • 1
    I commented a little to soon without fully testing... It doesn't work, but scheduling the second script does. e.g. at now + 1 minute -f sync2.sh Dec 1, 2021 at 8:04
  • Yeah, Scheduling (pulling) is one way to go.
    – user.dz
    Dec 1, 2021 at 8:07

1 Answer 1


AFAIK, all udev scripts that include FS mounting are old few years ago. I believe it had been changed around the time systemd emerged.

Here is a reference note from udev_237 - man udev (Ubuntu_18.04)



    Note that running programs that access the network or mount/unmount filesystems is not allowed inside of udev rules, due to the default sandbox that is enforced on systemd-udevd.service.

Better to go with solutions that use libs like udev, udisks, ... Or an external script that keep watching for that device.

BTW, 100- is too early even for other applications (udev rules files are processed in lexical order), wise to change it into a high 2 digit number. If you want last use 99-

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .