Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to edit the USB MSD, to show a message whenever i plug a device (like a flash drive) to my computer. How do i do it? Which file exactly is to be edited and how?

share|improve this question
UsbDriveDoSomethingHowto – Achu Mar 19 '14 at 5:11
Did you ever try my solution below? – terdon Mar 19 '14 at 12:46
yes i did! But what i wanna know is, editing the code of class driver of a particular device, so that when it is attached to the system, the console should show a message. That's what my manager has told me to do. – akshay Mar 20 '14 at 5:10
I read somewhere that i can use /linux/drivers/usb/storage But i exactly dont know. – akshay Mar 21 '14 at 4:37

The driver has nothing to do with this, it just manages the device and allows the kernel to communicate with it. What you want is a much higher lever function.

The way to do this is using udev, the device manager for the Linux kernel:

  1. Create a script that will send the notifications. Save the following lines in a file in your home directory, for example ~/

    export DISPLAY=":0"
    notify-send "New device plugged in: $@"

    Make the script executable by running chmod +x ~/

  2. Create a new file called /etc/udev/rules.d/95-usbnotify.rules with the following contents (adapted from here):

    KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
    ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"
    # Get label
    PROGRAM=="/sbin/blkid -o value -s LABEL %N", ENV{dir_name}="%c"
    # use basename to correctly handle labels such as ../mnt/foo
    PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
    ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"
    ACTION=="add", ENV{dir_name}!="", RUN+="/home/akshay/ %c", GOTO="media_by_label_auto_mount_end"
    # Exit

    Make sure to use the correct path to the script, I used /home/akshay/ but edit to point to your home directory.

Save the script and that's it. You should now receive a notification for every device that you plug in that is mounted as a drive. This will probably not work for cameras and the like but any storage device that is attached as /dev/sd* should work.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.