rss logo

Bash script to automated Email Notification upon USB Device Connection

Linux terminal logo

On a Debian GNU/Linux system, I have worked on a solution to notify users via email when a USB hard disk drive is connected. The objective is to inform the system administrator that the USB drive is successfully connected.

Installing and configuring ssmtp

Example here with ssmtp which will send emails to relay on the 465 SSL port.

  • Install ssmtp:
root@host:~# apt update && apt install ssmtp
  • Edit /etc/ssmtp/ssmtp.conf:
# # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. #root=postmaster # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=backup.std.local # Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address #FromLineOverride=YES root=root@backup.std.local TLS_CA_FILE=/etc/ssl/certs/ca-certificates.crt UseTLS=Yes UseSTARTTLS=No FromLineOverride=yes


udev is a device manager for GNU/Linux systems that dynamically manages devices and their corresponding device nodes in the system. It stands for "userspace device" and operates in the userspace rather than the kernel space. It will allow us to execute a script when a USB disk device will be plugged in.

Note: let's say the disk we want to monitor is /dev/sda
  • Just for information we can print all attributes of the /dev/sda device:
root@host:~# udevadm info --attribute-walk --path=$(udevadm info --query=path --name=/dev/sda)
  • Create a /etc/udev/rules.d/10_usb-disk.rules file:
ACTION=="add", SUBSYSTEM=="block", SUBSYSTEMS=="usb", RUN{program}+="/usr/local/sbin/"
  • Reload the udev rules and configuration:
root@host:~# udevadm control --reload

Bash script

  • Create /usr/local/sbin/ file:
root@host:~# touch /usr/local/sbin/ && chmod +x /usr/local/sbin/
  • Edit /usr/local/sbin/ file:
I've noticed that the script is executed twice when a USB device is plugged in. To prevent sending two emails, I create a /tmp/udev_time file and compare its creation time to the current date. If the time difference is greater than 10 seconds, the email will be sent.
Edit 2023/07/01: Haven't tested but some says that it could be resolved by adding ENV{DEVTYPE}=="usb_device" in the rule. #! /bin/sh MAIL_ADDRESS="" DATE=$(/bin/date "+%Y-%m-%dT%H:%M:%S") UDEV_TIME="$(/bin/date --date="$(stat /tmp/udev_time | grep Modif | sed 's/Modif.*: //')" +%s)" #get /tmp/udev_time creation time touch /tmp/udev_time CUR_TIME="$(/bin/date +%s)" #difference between current time and /tmp/udev_time creation time TIME_DIFF=$(( (CUR_TIME - UDEV_TIME) )) #Function mailto () { LANG=C rm -f /tmp/mail_usb_disk.header echo "From: $MAIL_ADDRESS" >> /tmp/mail_usb_disk.header echo "To: $MAIL_ADDRESS" >> /tmp/mail_usb_disk.header echo "Date: "`/bin/date -R` >> /tmp/mail_usb_disk.header echo "Subject: Disk has been plugged in" >> /tmp/mail_usb_disk.header echo "Content-type: text/plain; charset=utf-8" >> /tmp/mail_usb_disk.header echo "" >> /tmp/mail_usb_disk.header echo "Disk has been plugged in" >> /tmp/mail_usb_disk.header cat /tmp/mail_usb_disk.header | /usr/sbin/sendmail -f "$MAIL_ADDRESS" "$MAIL_CLIENT" rm -f /tmp/mail_usb_disk.header } #Send email if time difference is greater than 10s if [ "$TIME_DIFF" -gt 10 ]; then mailto fi
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address