Configuring udev rules for cameras - robotique-udes/rover_udes GitHub Wiki

Why do it

When you plug a camera in a Linux device, it is automatically assigned a /dev/videoX device symlink. But this device will probably change if you unplug the camera and plug it again, or if you reboot the Linux device.

By configuring a udev rule, you can make sure that a given physical device gets assigned to a given device symlink, every time.

Getting the required information for a USB camera

First, list the USB devices plugged on your computer.

lsusb

This will give you a list of the form:

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 008: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 004: ID 8087:0aaa Intel Corp. 
Bus 001 Device 003: ID 5986:211c Acer, Inc HD Webcam
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

You should also list your cameras:

v4l2-ctl --list-devices

This will give you a list of the form (if you have a camera connected; here, this is the webcam of my laptop):

HD Webcam: HD Webcam (usb-0000:00:14.0-11):
	/dev/video0
	/dev/video1

Now, plug in your camera. Find the new entry that is your camera. It might look a little like this:

Bus 001 Device 012: ID 0c45:6366 Microdia USB 2.0 Camera

You can also list the cameras again. You should see a new entry:

USB 2.0 Camera: USB Camera (usb-0000:00:14.0-4):
	/dev/video2
	/dev/video3
	/dev/video4
	/dev/video5

Next, we want to get the vendor ID and product ID for the camera. Use the first entry given by the command above in this next command. In this case, it is /dev/video2.

udevadm info -a /dev/video2 | grep -m1 "ATTRS{idVendor}"
udevadm info -a /dev/video2 | grep -m1 "ATTRS{idProduct}"

These should match the two values that showed when you listed the USB devices before (Bus 001 Device 012: ID 0c45:6366 Microdia USB 2.0 Camera).

Creating the udev rule file

Create a file called anywhere 99-cameras-rovus.rules. Choose a name for your symlink, and use the two values that you got before. For example, if you want to give it the /dev/waterproof_camera device symlink, your file would look like this:

SUBSYSTEM=="video4linux", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0c45", ATTRS{idProduct}=="6366", ATTR{index}=="0", SYMLINK+="waterproof_camera"

Finally, add this file to your /etc/udev/rules.d folder (you will need sudo), and reload your rules with the following command:

sudo udevadm control --reload-rules && udevadm trigger