Определение usb портов linux

command to determine ports of a device (like /dev/ttyUSB0)

I have a question regarding the ports in Linux. If I connect my device via USB and want to check its port I can’t do it using the command lsusb, which only specifies bus number and device number on this bus:

[ziga@Ziga-PC ~]$ lsusb Bus 003 Device 007: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC 

Is there a command that tells me the port the device is connected to directly? Only way to do this until now was to disconect and reconnect and using the command:

[ziga@Ziga-PC ~]$ dmesg | grep tty [ 0.000000] console [tty0] enabled [ 0.929510] 00:09: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A [ 4.378109] systemd[1]: Starting system-getty.slice. [ 4.378543] systemd[1]: Created slice system-getty.slice. [ 8.786474] usb 3-4.4: FTDI USB Serial Device converter now attached to ttyUSB0 

What are you trying to accomplish? Do you want to associate a device in /dev with an entry in lsusb ? Or do you want to list all devices in /dev that are derived from a physical USB device? Or are you just wanting ls /dev/ttyUSB* ?

I just need a command which will give me port of a device and will not push me to disconnect and reconnect my devices. ls /dev/ttyUSB* will only list maybee 10 ports but from this list I cannot tell which one is for my device.

I use this Python 3 script all the time, it works perfectly well on Linux, macOS and Raspberry Pi OS: gist.github.com/NicHub/d86d34f2292da017ac20ca1e6a7cb76d

7 Answers 7

I’m not quite certain what you’re asking. You mention ‘port’ several times, but then in your example, you say the answer is /dev/ttyUSB0 , which is a device dev path, not a port. So this answer is about finding the dev path for each device.

Below is a quick and dirty script which walks through devices in /sys looking for USB devices with a ID_SERIAL attribute. Typically only real USB devices will have this attribute, and so we can filter with it. If we don’t, you’ll see a lot of things in the list that aren’t physical devices.

#!/bin/bash for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do ( syspath="$" devname="$(udevadm info -q name -p $syspath)" [[ "$devname" == "bus/"* ]] && exit eval "$(udevadm info -q property --export -p $syspath)" [[ -z "$ID_SERIAL" ]] && exit echo "/dev/$devname - $ID_SERIAL" ) done 

On my system, this results in the following:

/dev/ttyACM0 - LG_Electronics_Inc._LGE_Android_Phone_VS930_4G-991c470 /dev/sdb - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0 /dev/sdb1 - Lexar_USB_Flash_Drive_AA26MYU15PJ5QFCL-0:0 /dev/input/event5 - Logitech_USB_Receiver /dev/input/mouse1 - Logitech_USB_Receiver /dev/input/event2 - Razer_Razer_Diamondback_3G /dev/input/mouse0 - Razer_Razer_Diamondback_3G /dev/input/event3 - Logitech_HID_compliant_keyboard /dev/input/event4 - Logitech_HID_compliant_keyboard 

Explanation:

find /sys/bus/usb/devices/usb*/ -name dev 

Devices which show up in /dev have a dev file in their /sys directory. So we search for directories matching this criteria.

We want the directory path, so we strip off /dev .

devname="$(udevadm info -q name -p $syspath)" 

This gives us the path in /dev that corresponds to this /sys device.

Читайте также:  Get service status linux

This filters out things which aren’t actual devices. Otherwise you’ll get things like USB controllers & hubs. The exit exits the subshell, which flows to the next iteration of the loop.

eval "$(udevadm info -q property --export -p $syspath)" 

The udevadm info -q property —export command lists all the device properties in a format that can be parsed by the shell into variables. So we simply call eval on this. This is also the reason why we wrap the code in the parenthesis, so that we use a subshell, and the variables get wiped on each loop.

More filtering of things that aren’t actual devices.

echo "/dev/$devname - $ID_SERIAL" 

I hope you know what this line does 🙂

Thank you. I will learn a lot from your anwser and now I see that terms werent completely clear to me. Is there any shorter way? Maybee a command already integrated in the Linux itself?

@Walf Yes, that is the purpose. The eval creates unknown variables that differ per device. Without () , when it checks $ID_SERIAL , it could be from a previous device.

You can use this command to explore your device if connected to usb0 :

udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0) 

Assuming that you know what the device you plugged in is, in 14.04 Ubuntu, at least, there is the command usb-devices that you can look through and find the information:

$ usb-devices T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 3 D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1 P: Vendor=1d6b ProdID=0002 Rev=04.04 S: Manufacturer=Linux 4.4.0-131-generic ehci_hcd S: Product=EHCI Host Controller S: SerialNumber=0000:00:1a.0 C: #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=0mA I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub 

And the first line lists bus and port, as well as the device number that lsusb gives.

To be fair, as the top answer notes, the question isn’t clear on whether it’s asking for «how do I derive the port» vs «how do I get the exact path?».

I found @phemmer’s accepted answer regularly helpful and turned it into a bash script. I added shell process backgrounding to make it run a bit faster for lots of USB devices connected to a system (which when this is most useful).

#!/bin/bash #findusbdev.sh if [[ "$1" =~ ^(-h|--help)$ ]]; then echo "Find which USB devices are associated with which /dev/ nodes Usage: $0 [-h|--help] [searchString] -h | --help Prints this message searchString Print only /dev/ of matching output With no arguments $0 prints information for all possible USB device nodes E.g. $0 \"FTDI_FT232\" - will show /dev/ttyUSBX for a device using the FTDI FT232 chipset. " exit 0 fi devs=$( ( for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev ); do # ( to launch a subshell here ( syspath="$" devname="$(udevadm info -q name -p $syspath)" [[ "$devname" == "bus/"* ]] && exit eval "$(udevadm info -q property --export -p $syspath)" [[ -z "$ID_SERIAL" ]] && exit echo "/dev/$devname - $ID_SERIAL" )& # & here is causing all of these queries to run simultaneously done # wait then gives a chance for all of the iterations to complete wait # output order is random due to multiprocessing so sort results ) | sort ) if [ -z "$1" ]; then echo "$" else echo "$" | grep "$1" | awk '' fi 
$ ./findusbdev.sh /dev/input/event15 - Peppercon_AG_Multidevice_E999EC989B0BAFB78F9F225288EC6B0A /dev/input/event16 - Peppercon_AG_Multidevice_E999EC989B0BAFB78F9F225288EC6B0A /dev/input/mouse0 - Peppercon_AG_Multidevice_E999EC989B0BAFB78F9F225288EC6B0A /dev/ttyUSB0 - FTDI_FT230X_Basic_UART_D308B2AI /dev/ttyUSB1 - FTDI_FT232R_USB_UART_AH07DPSR 
$ ./findusbdev.sh FT230X /dev/ttyUSB0 
$ ./findusbdev.sh -h Find which USB devices are associated with which /dev/ nodes Usage: ./findusbdev.sh [-h|--help] [searchString] -h | --help Prints this message searchString Print only /dev/ of matching output With no arguments ./findusbdev.sh prints information for all possible USB device nodes E.g. ./findusbdev.sh "FTDI_FT232" - will show /dev/ttyUSBX for a device using the FTDI FT232 chipset. 

Источник

Читайте также:  Creating virtual machine in linux

Ubuntu/Debian How to identify USB port of device

How can you identify individual USB devices to unbind and bind them? Specifically, devices like mice, keyboard, dongles, etc. which are not storage. For example, on Debian (Raspbian GNU/Linux v8.0 in this case), I have a few devices listed in /sys/bus/usb/drivers/usbhid/: ls /sys/bus/usb/drivers/usbhid/ shows USB devices:

1-1.2:1.0 1-1.2:1.1 1-1.2:1.2 1-1.3:1.0 1-1.3:1.1 1-1.4:2.2 
Bus 001 Device 006: ID 1410:9020 Novatel Wireless Bus 001 Device 009: ID 045e:00db Microsoft Corp. Natural Ergonomic Keyboard 4000 V1.0 Bus 001 Device 008: ID 046d:c52b Logitech, Inc. Unifying Receiver Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 
/: Bus 01.Port 1: Dev 1, Driver=dwc_otg/1p, 480M |__ Port 1: Dev 2, If 0, Driver=hub/5p, 480M |__ Port 1: Dev 3, If 0, Specific Class, Driver=smsc95xx, 480M |__ Port 2: Dev 8, If 0, Interface Device, Driver=usbhid, 12M |__ Port 2: Dev 8, If 1, Interface Device, Driver=usbhid, 12M |__ Port 2: Dev 8, If 2, Interface Device, Driver=usbhid, 12M |__ Port 3: Dev 9, If 0, Interface Device, Driver=usbhid, 1.5M |__ Port 3: Dev 9, If 1, Interface Device, Driver=usbhid, 1.5M |__ Port 4: Dev 6, If 0, Driver=cdc_ether, 480M |__ Port 4: Dev 6, If 1, Data, Driver=cdc_ether, 480M |__ Port 4: Dev 6, If 2, Interface Device, Driver=usbhid, 480M 

I know that the Novatel Wireless is 1-1.4:2.2 from guessing. I can then reset it by running echo -n ‘1-1.4:2.2’ > /sys/bus/usb/drivers/usbhid/unbind and echo -n ‘1-1.4:2.2’ > /sys/bus/usb/drivers/usbhid/bind It would be great if there was an obvious way to identify it and reset it in a bash script. Also, how can a device be identified and reset if there are more than one of the same hardware on different ports?

Источник

How to List USB Devices in Linux

Nowadays, many computer peripherals such as webcams, mice, scanners, printers, hard drives, USB (Pendrive) now come as USB devices. Once these devices are connected to the Desktop or server it’s important to know the device name or device path. This helps to identify USB devices for the tasks such as formatting.

In Linux, all device files are stored in /dev directory and must be available to the OS during the system boot.

Читайте также:  Права доступа линукс цифры

In this guide, we will show you the various ways to list USB devices on Linux. Most commands mentioned here should work on all Linux distributions.

List USB Device Names Using df Command

The df command is a useful command that can help list all mounted volumes, including your USB drives.

Once a USB device is plugged into a Linux system, especially for Desktop, it is automatically mounted in the /media partition and becomes ready for use.

df command check plugged in USB devices

From the output above, I have 1 USB drive /dev/sdb with 2 partitions /dev/sdb1 and /dev/sdb2

List USB Device Name using lsblk Command

Lsblk command is used to list all block devices on a Linux system. From the list, you can filter USB devices using the grep command.

lsblk command check plugged in USB devices

To retrieve additional information such as the UUID, manufacturer and filesystem type, use the blkid command as shown.

blkid command output retrieve information on USB block devices

List USB Device Names Using fdisk Command

You can use the good old fdisk command that is used for partitioning volumes to list all the partitions on the Linux system, including the USB drives.

The command will display detailed information about your USB volume including the partitions, size of the volume, sectors, and the filesystem type.

fdisk command list USB devices

List USB Devices Details using lsusb Command

The lsusb command, also known as the “List USB” command, is used in Linux to list all the USB devices attached to the system.

lsusb command

The output above displays the Bus ID, Device ID, USB ID, and the vendor or manufacturer of the USB devices

The lsusb command simply lists the connected devices and does not provide further information about the usb devices.

For more information about the attached USB devices use the dmesg command. The dmesg command also known as “driver message” or “display the message” is used for examining the boot messages. Additionally, it is used for debugging hardware-related issues and printing messages generated by device drivers.

You can use the dmesg command and grep to narrow down to USB devices.

dmesg command

Also, you can pipe the output of dmesg command to less for easier scrolling.

On the output, you can search for a specific string by pressing the forward slash key ( / ) on your keyboard followed by the name or Device ID of the USB device.

In my case, I’m searching for more information regarding a USB device called SanDisk, which is actually my removable pen drive.

Search for a USB drive

List USB controllers and devices using usb-devices

The usb-devices command is a shell script that allows you to list all the USB controllers and the USB devices connected to your PC. It prints out details of the USB device such as the manufacturer, product name, serial number, and so much more. Here’s the output of the command:

usb-devices command to list usb devices

Conclusion

In this guide, we have demonstrated different ways to list the USB devices attached to the Linux system.

Which is your favorite command to list USB? Please provide your suggestions and feedback in the comment section.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector