A storage device is in the block
subsystem, so you'll want SUBSYSTEM=="block"
in your rule, like this:
ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", \
RUN+="/path/to/script"
If you're using systemd
, you could run a systemd
unit each time a USB storage device is added. Create the unit file, e.g. /etc/systemd/system/my-usb-rule.service
:
[Service]
Type=oneshot
ExecStart=/path/to/script
and the rule, e.g. /etc/udev/rules.d/85-my-usb-rule.rules
:
ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", \
ENV{SYSTEMD_WANTS}="my-usb-rule.service"
Now udev
will trigger my-usb-rule.service
(which in turn will execute your script) on any usb storage device add event.
Don't forget to reload the configuration after you edit the rules/units:
udevadm control --reload
systemctl daemon-reload
SUBSYSTEM=="block", SUBSYSTEMS=="usb"
(note the difference with the S). I doubt it can always work withENV{ID_BUS}=="usb"
in the original answer, because I see the value scsi here. But I am working on a slightly different problem than originally asked, I want any USB stick (pendrive), but no external drives. – Uwe Geuder Jan 16 '17 at 20:14ENV{ID_BUS}
value is not the same on all systems. Yesterday I was working on an older system with udev 210 (I believe. Cannot check now because the machine is not on the internet), There the value was scsi. Now, I am on newer machine with udev 228 and the value is usb. For the same USB stick, I am carrying it with me. (not sure whether the udev version is the relevant factor here, could also be the kernel or any other package installing udev rules, e,g, udisks2) StillSUBSYSTEM=="block", SUBSYSTEMS=="usb"
(note S) might be portable and safe. – Uwe Geuder Jan 17 '17 at 10:21