Create a symlink in /dev using /etc/udev/rules

35,526

Few points

  • Used 75-, the rules run in order. There is a chance that your rule may overridden by next rules from /lib/udev/rules.d/, prefer to use 99- ~ 90-

  • SUBSYSTEM=="usb", SYMLINK+="myusb" too generic

    It match all event actions add, remove & change. Beside all USB devices. May be you should make it more strict.

    Check using lsusb

    Bus 003 Device 003: ID 0461:4d81 Primax Electronics, Ltd Dell N889 Optical Mouse
    

    and add them:

    ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0461", ATTRS{idProduct}=="4d81", SYMLINK+="myusb"
    
  • Not all devices create a node in /dev. (Could you mention what type of devices you have used)

  • Bonus :), even so the rule still broad concerning tree device nodes. When you plug camera there is not only one node created but many from tree root to the leaf (leaf is in most cases the wanted one). Check:

    udevadm info -a /dev/video0
    

    The previous modified rule may match two last nodes (not the leaf).

Share:
35,526

Related videos on Youtube

Mokum
Author by

Mokum

I am a ubuntu enthousiast, learning every day how linux works. Also interested in grub 2 to use different or more distros all to learn more about linux.regards Harry.

Updated on September 18, 2022

Comments

  • Mokum
    Mokum over 1 year

    I am studying for a Linux certification from the Linux foundation but I have some questions about one lab. Here is the lab:

    1. Create and implement a rule on your system that will create a symlink called myusb when a USB device is plugged in.
    2. Plug in a USB device to your system. It can be a pendrive, mouse, webcam, etc. Note: If you are running a virtual machine under a hypervisor, you will have to make sure the USB device is seen by the guest, which usually is just a mouse click which also disconnects it from the host.
    3. Get a listing of the /dev directory and see if your symlink was created.
    4. Remove the USB device. (If it is a drive you should always umount it first for safety.)
    5. See if your symbolic link still exists in /dev.

    what i did in terminal;

    1. Create a file named /etc/udev/rules.d/75-myusb.rules

      sudo vim /etc/udev/rules.d/75-myusb.rules
      
    2. and have it include just one line of content:

      SUBSYSTEM=="usb", SYMLINK+="myusb"
      
    3. restart udev service and check

      sudo /etc/init.d/udev restart    
      ls -lF /dev | grep myusb
      

    But than no output from last command, no listing at all.

    How can I check if the symlink was created?

    • steeldriver
      steeldriver almost 8 years
      I suggest you review the learning material for the module, or search here for similar questions e.g. SYMLINK in udev rule
    • Mokum
      Mokum almost 8 years
      thanx but i found no answer .
    • Mokum
      Mokum almost 8 years
      I found this on Hackaday, hackaday.com/2009/09/18/how-to-write-udev-rules/#comments udev does not run these programs on any active terminal, and it does not execute them under the context of a shell. Be sure to ensure your program is marked executable, if it is a shell script ensure it starts with an appropriate shebang (e.g. #!/bin/sh), and do not expect any standard output to appear on your terminal. hmmm still confusing I would appreciate some help.
    • steeldriver
      steeldriver almost 8 years
      It's right there in the link I posted (probably by someone doing the exact same assignment as you): SUBSYSTEM=="usb", SYMLINK+="myusb"
    • Mokum
      Mokum almost 8 years
      ok thanx steeldriver
    • user.dz
      user.dz almost 8 years
      @user23324, I saw your deleted entry below, could you edit your question and update it.
    • Mokum
      Mokum almost 8 years
      @Sneetsher i tried deleting the answer , i did not work.
    • user.dz
      user.dz almost 8 years
      @user23324, it's ok , it already deleted, it can seen only by you and 10K users.
  • Mokum
    Mokum almost 8 years
    Ok thats very helpfull, i used :Bus 001 Device 005: ID 0471:0325 Philips (or NXP) SPC 200NC PC Camera
  • user.dz
    user.dz almost 8 years
    @user23324, yes web cameras create node in /dev check ls -l /dev/video* your symlink will point to one of results. For easy testing plug & unplug run this on terminal and watch : watch "ls -l /dev/video* /dev/myusb "
  • Mokum
    Mokum almost 8 years
    thanx that worked fine! @Sneetsher
  • Mokum
    Mokum almost 8 years
    yes off course just did that. Thanx again !