diff options
| -rw-r--r-- | stuff/index.html | 4 | ||||
| -rw-r--r-- | stuff/usb-notif-linux.html | 140 |
2 files changed, 144 insertions, 0 deletions
diff --git a/stuff/index.html b/stuff/index.html index 6e73ad8..21d70eb 100644 --- a/stuff/index.html +++ b/stuff/index.html @@ -94,6 +94,10 @@ <a href="cloudflare-dot.html">DNS over TLS (DoT) with Cloudflare</a> <span class="date">2026-04-14</span> </li> + <li> + <a href="usb-notif-linux.html">USB notifications with udev</a> + <span class="date">2026-04-14</span> + </li> </ul> </nav> </main> diff --git a/stuff/usb-notif-linux.html b/stuff/usb-notif-linux.html new file mode 100644 index 0000000..1f35212 --- /dev/null +++ b/stuff/usb-notif-linux.html @@ -0,0 +1,140 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>subh.space</title> + <style> + :root { + --bg0: #282828; + --bg1: #3c3836; + --fg: #ebdbb2; + --gray: #928374; + --yellow: #fabd2f; + --green: #b8bb26; + --orange: #fe8019; + --aqua: #8ec07c; + } + + body { + font-family: 'Iosevka Nerd Font Propo'; + line-height: 1.7; + color: var(--fg); + background-color: var(--bg0); + max-width: 780px; + margin: 40px auto; + padding: 0 20px; + -webkit-font-smoothing: antialiased; + } + + h1 { + font-size: 2.2em; + color: var(--yellow); + border-bottom: 2px solid var(--bg1); + padding-bottom: 15px; + margin-bottom: 30px; + } + + h2 { + font-size: 1.5em; + color: var(--aqua); + margin-top: 35px; + margin-bottom: 15px; + font-weight: 600; + } + + p { margin-bottom: 1.2em; } + + code { + font-family: 'Fira Code', 'JetBrains Mono', 'Courier New', monospace; + background-color: var(--bg1); + color: var(--orange); + padding: 3px 6px; + border-radius: 4px; + font-size: 0.9em; + } + + pre { + background-color: #1d2021; + padding: 20px; + border-radius: 8px; + overflow-x: auto; + border: 1px solid var(--bg1); + margin-bottom: 1.5em; + } + + pre code { + background-color: transparent; + padding: 0; + color: var(--fg); + color-scheme: dark; + } + + ol, ul { margin-bottom: 1.5em; padding-left: 25px; } + li { margin-bottom: 0.8em; } + li pre { margin-top: 10px; margin-bottom: 10px; } + + </style> +</head> +<body> + +<h1>Configuring USB notifications on Linux</h1> + +<p>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</p> + +<p>Your system detects a USB device upon connection or disconnection using an utility named <code>udev</code>. 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 <code>notify-send</code> to send notifications to a user as soon as a USB device is connected or disconnected</p> + +<h2>1. Creating a udev rule</h2> + +<p>Below is a rule file, which will trigger two scripts depending upon the connection or disconnection of a USB device</p> + +<pre><code class="language-shell">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}'" +</code></pre> + +<p>save this in a <code>.rules</code> file under <code>/etc/udev/rules.d/</code>.</p> + +<h2>2. Configuring the scripts</h2> + +<p>Below are the scripts:</p> + +<ul> + <li>usb-notify-add.sh</li> +</ul> + +<pre><code class="language-shell">#!/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" +</code></pre> + +<ul> + <li>usb-notify-rem.sh</li> +</ul> + +<pre><code class="language-shell">#!/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" +</code></pre> + +<p>Save both the scripts under <code>/usr/local/bin</code> and make them executable with <code>chmod</code></p> + +<h2>3. Reloading the udev rules</h2> + +<p>Once all the files have been saved. Reload udev rules as follows:</p> + +<pre><code class="language-shell">sudo udevadm control --reload-rules +sudo udevadm trigger +</code></pre> + +<p>And just like that, you've setup USB notifications. Give it a try! Plug in a USB device and check whether your system sends your a notification with the name of the USB device.</p> + +</body> +</html> |
