From afecebc9521775a3bf2cdd055035d3d0a3377554 Mon Sep 17 00:00:00 2001 From: subh Date: Wed, 15 Apr 2026 18:32:58 +0530 Subject: revamp --- stuff/usb-notif-linux.html | 193 +++++++++++++++++++++++++++------------------ 1 file changed, 116 insertions(+), 77 deletions(-) (limited to 'stuff/usb-notif-linux.html') diff --git a/stuff/usb-notif-linux.html b/stuff/usb-notif-linux.html index 4acc27b..2a03886 100644 --- a/stuff/usb-notif-linux.html +++ b/stuff/usb-notif-linux.html @@ -3,138 +3,177 @@ - subh.space + USB Notifications - subh.space -

Configuring USB notifications on Linux

- -

If you want to receive a small notification as soon as a USB storage device is plugged in or plugged out. You're in the right place

- -

Your system detects a USB device upon connection or disconnection using an utility named udev. udev allows for defining rules which can allow a user to perform a specific task when a USB device is connected or disconnected. This can be combined with notify-send to send notifications to a user as soon as a USB device is connected or disconnected

- -

1. Creating a udev rule

- -

Below is a rule file, which will trigger two scripts depending upon the connection or disconnection of a USB device

+
+ home + git + github + notes + hackthebox +
-
ACTION=="add", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk",  RUN+="/usr/local/bin/usb-notify-add.sh '$env{ID_MODEL}'"
-ACTION=="remove", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk",  RUN+="/usr/local/bin/usb-notify-rem.sh '$env{ID_MODEL}'"
-
+
+

Configuring USB notifications on Linux

+ +

If you want to receive a small notification as soon as a USB storage device is plugged in or plugged out, you're in the right place.

-

save this as 99-usb-notify.rules file under /etc/udev/rules.d/.

+

Your system detects a USB device upon connection or disconnection using a utility named udev. It allows for defining rules to perform specific tasks. Combined with notify-send, you can trigger desktop notifications instantly.

-

2. Configuring the scripts

+

1. Creating a udev rule

+

Save the following as 99-usb-notify.rules under /etc/udev/rules.d/

-

Below are the scripts:

+
ACTION=="add", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", RUN+="/usr/local/bin/usb-notify-add.sh '$env{ID_MODEL}'"
+ACTION=="remove", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", RUN+="/usr/local/bin/usb-notify-rem.sh '$env{ID_MODEL}'"
- +

2. Configuring the scripts

+

Save both scripts under /usr/local/bin/ and make them executable

-
#!/bin/bash
+        usb-notify-add.sh
+        
#!/bin/bash
 DEVICE_NAME=${1:-"Unknown USB Device"}
 
 USER_ID=$(id -u <your-username>)
 export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USER_ID/bus
 
-sudo -u <your-username> DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS /usr/bin/notify-send "USB Connected" "Device: $DEVICE_NAME"
-
+sudo -u <your-username> DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS /usr/bin/notify-send "USB Connected" "Device: $DEVICE_NAME"
- - -
#!/bin/bash
+        usb-notify-rem.sh
+        
#!/bin/bash
 DEVICE_NAME=${1:-"Unknown USB Device"}
 
 USER_ID=$(id -u <your-username>)
 export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USER_ID/bus
 
-sudo -u <your-username> DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS /usr/bin/notify-send "USB Disconnected" "Device: $DEVICE_NAME"
-
- -

Save both the scripts under /usr/local/bin and make them executable with chmod

+sudo -u <your-username> DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS /usr/bin/notify-send "USB Disconnected" "Device: $DEVICE_NAME"
-

3. Reloading the udev rules

+

3. Reloading the udev rules

+

Once the files are saved, reload the udev configuration to apply the changes:

+
sudo udevadm control --reload-rules
+sudo udevadm trigger
-

Once all the files have been saved. Reload udev rules as follows:

+

And just like that, you've setup USB notifications. Give it a try! Plug in a device and check if your system sends a notification with the device name.

+
-
sudo udevadm control --reload-rules
-sudo udevadm trigger
-
- -

And just like that, you've setup USB notifications. Give it a try! Plug in a USB device and check whether your system sends you a notification with the name of the USB device.

+ + -- cgit v1.2.3