Linux – How to identify USB webcam by serial number from the Linux command line

command linelinuxUbuntuusbwebcam

How do you get a USB webcam’s serial number from the Linux command line?

I have multiple USB webcams connected to an Ubuntu machine. They all show up as /dev/video0, /dev/video1, /dev/video2, etc, but I can't seem to find any way to programmatically tell which is which. Several of the cameras are the same model, so just getting a model name isn't enough.

Note, I'm not asking how to use lsusb. Yes, I know you can use lsusb or libusb to get device serial number, product ID, manufacturer name, etc. But as far as I can tell, nothing shown by lsusb can be cross-referenced with a /dev/video* path.

e.g. If someone plugs in two identical webcams and they show up as /dev/video1 and /dev/video2 and then unplugs them and replugs them into completely different ports, so that /dev/video2 becomes /dev/video3 and /dev/video1 becomes /dev/video4, I can immediately know that the current /dev/video3 "used to be" /dev/video2.

The only similar questions I've found only suggest hacks like unplugging and and replugging it while scanning dmesg. I'm looking for a pure-programmatic solution that requires no hardware manipulation to identify the webcams.

Best Answer

It is possible to identify all cameras. The command

 $ sudo lsusb -v -d 046d:082d | grep -i serial
   iSerial                 1 05C6D16F

returns the Serial number of my camera (a Logitech HD Pro Webcam, used as an example with the correct Vendor:Product codes obtained from a previous use of lsusb). Please notice that the use of sudo is absolutely necessary: an unprivileged user does not get access to all info available thru the command.

The serial number is often, but not always, unique. If it is not unique (just compare the output of the above commands for two devices with the same Vendor:Product codes), you can set them so that they are distinct. There are guides all over Google for doing that, I will merely point to a couple of them, for the sake of thoroughness: here and here. But remember, this is a device-dependent procedure, so you will have to find out how to do it for your very own camera.

Now the command

$ sudo udevadm info --query=all /dev/video1 | grep 'VENDOR_ID\|MODEL_ID\|SERIAL_SHORT'
  E: ID_MODEL_ID=082d
  E: ID_VENDOR_ID=046d

returns the appropriate codes for this particular camera. Trial and error with all /dev/videoX devices allows pigeon-holing all of them.