This is an overview of how I configured my system to play a sound when a USB device (such as a flash drive) is plugged in.
- Linux install with
udev
andsystemd
. - Root access.
- A utility to play sounds (I used
aplay
). - Sounds to play.
Using the tool aplay
(found in the Arch package alsa-utils
), it is easy to play an audio file: aplay insert.wav
Our goal is to run this command when a USB device is added or removed.
One way to create triggerable events is using Systemd services. I created the following service at /etc/systemd/system/usb-insert.service
:
[Unit]
Description=Play USB sound
[Service]
User=username
Type=oneshot
Environment="XDG_RUNTIME_DIR=/run/user/1000"
ExecStart=/usr/bin/aplay /etc/sounds/insert.wav
Make sure to change the User key to your username! |
---|
The environment variable is needed for aplay
to work, as systemd services don't have the same environment variables defined as your user session does.
Run sudo systemctl daemon-reload
to have it register the new service.
Having placed the sound in that location, running sudo systemctl start usb-insert
should now play the sound! At this point, I also created usb-remove.service
that just plays a different sound.
udev
is a nifty tool that manages devices under /dev
. It provides the ability to set up rules and run commands based on them.
You can use sudo udevadm monitor -u
to see all the events that pass through it, and you should see ones marked "add" and "remove" when you add/remove devices respectively. We're going to filter them to run a command once when a USB device is added/removed.
You can then use udevadm info -a --path=/sys{/devices path from monitor}
to view properties of the device. We need to filter events relating to USB devices specifically.
By editing /etc/udev/rules.d/100-usb.rules
, we can add said filters. This is the configuration that worked best for me:
ACTION=="add", SUBSYSTEM=="usb", KERNEL=="*:1.0", RUN+="/bin/systemctl start usb-insert"
ACTION=="remove", SUBSYSTEM=="usb", KERNEL=="*:1.0", RUN+="/bin/systemctl start usb-remove"
If you wish to also receive notifications for SD card events, add another pair of rules for
SUBSYSTEM=="block"
.
Make sure to apply your changes with sudo systemctl restart systemd-udevd
!
And with that, you should have working sound notifications!
If you have any suggestions to improve this, please leave them as a comment and I will incorporate them. ❤️
Hmm... Could I use udev to make myself a "hardware logon token"?
Would that work?