The /sys filesystem should contain plenty information for your quest. My system (2.6.32-40-generic #87-Ubuntu) suggests:
/sys/class/tty
Which gives you descriptions of all TTY devices known to the system. A trimmed down example:
# ll /sys/class/tty/ttyUSB*
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0/ttyUSB0/tty/ttyUSB0/
lrwxrwxrwx 1 root root 0 2012-03-28 20:44 /sys/class/tty/ttyUSB1 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/ttyUSB1/tty/ttyUSB1/
Following one of these links:
# ll /sys/class/tty/ttyUSB0/
insgesamt 0
drwxr-xr-x 3 root root 0 2012-03-28 20:43 ./
drwxr-xr-x 3 root root 0 2012-03-28 20:43 ../
-r--r--r-- 1 root root 4096 2012-03-28 20:49 dev
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 device -> ../../../ttyUSB0/
drwxr-xr-x 2 root root 0 2012-03-28 20:49 power/
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 subsystem -> ../../../../../../../../../../class/tty/
-rw-r--r-- 1 root root 4096 2012-03-28 20:43 uevent
Here the dev file contains this information:
# cat /sys/class/tty/ttyUSB0/dev
188:0
This is the major/minor node. These can be searched in the /dev directory to get user-friendly names:
# ll -R /dev |grep "188, *0"
crw-rw---- 1 root dialout 188, 0 2012-03-28 20:44 ttyUSB0
The /sys/class/tty dir contains all TTY devices but you might want to exclude those pesky virtual terminals and pseudo terminals. I suggest you examine only those which have a device/driver entry:
# ll /sys/class/tty/*/device/driver
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS0/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS1/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS2/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS3/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/
lrwxrwxrwx 1 root root 0 2012-03-28 21:15 /sys/class/tty/ttyUSB1/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/
Answer from A.H. on Stack OverflowThe /sys filesystem should contain plenty information for your quest. My system (2.6.32-40-generic #87-Ubuntu) suggests:
/sys/class/tty
Which gives you descriptions of all TTY devices known to the system. A trimmed down example:
# ll /sys/class/tty/ttyUSB*
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0/ttyUSB0/tty/ttyUSB0/
lrwxrwxrwx 1 root root 0 2012-03-28 20:44 /sys/class/tty/ttyUSB1 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/ttyUSB1/tty/ttyUSB1/
Following one of these links:
# ll /sys/class/tty/ttyUSB0/
insgesamt 0
drwxr-xr-x 3 root root 0 2012-03-28 20:43 ./
drwxr-xr-x 3 root root 0 2012-03-28 20:43 ../
-r--r--r-- 1 root root 4096 2012-03-28 20:49 dev
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 device -> ../../../ttyUSB0/
drwxr-xr-x 2 root root 0 2012-03-28 20:49 power/
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 subsystem -> ../../../../../../../../../../class/tty/
-rw-r--r-- 1 root root 4096 2012-03-28 20:43 uevent
Here the dev file contains this information:
# cat /sys/class/tty/ttyUSB0/dev
188:0
This is the major/minor node. These can be searched in the /dev directory to get user-friendly names:
# ll -R /dev |grep "188, *0"
crw-rw---- 1 root dialout 188, 0 2012-03-28 20:44 ttyUSB0
The /sys/class/tty dir contains all TTY devices but you might want to exclude those pesky virtual terminals and pseudo terminals. I suggest you examine only those which have a device/driver entry:
# ll /sys/class/tty/*/device/driver
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS0/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS1/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS2/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS3/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/
lrwxrwxrwx 1 root root 0 2012-03-28 21:15 /sys/class/tty/ttyUSB1/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/
In recent kernels (not sure since when) you can list the contents of /dev/serial to get a list of the serial ports on your system. They are actually symlinks pointing to the correct /dev/ node:
flu0@laptop:~$ ls /dev/serial/
total 0
drwxr-xr-x 2 root root 60 2011-07-20 17:12 by-id/
drwxr-xr-x 2 root root 60 2011-07-20 17:12 by-path/
flu0@laptop:~$ ls /dev/serial/by-id/
total 0
lrwxrwxrwx 1 root root 13 2011-07-20 17:12 usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0 -> ../../ttyUSB0
flu0@laptop:~$ ls /dev/serial/by-path/
total 0
lrwxrwxrwx 1 root root 13 2011-07-20 17:12 pci-0000:00:0b.0-usb-0:3:1.0-port0 -> ../../ttyUSB0
This is a USB-Serial adapter, as you can see. Note that when there are no serial ports on the system, the /dev/serial/ directory does not exists. Hope this helps :).
Unfortunately serial ports are non-PlugNPlay, so kernel doesn't know which device was plugged in. After reading a HowTo tutorial I've got the working idea.
The /dev/ directory of unix like OSes contains files named as ttySn (with n being a number). Most of them doesn't correspond to existing devices. To find which ones do, issue a command:
$ dmesg | grep ttyS
[ 0.872181] 00:06: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[ 0.892626] 00:07: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A
[ 0.915797] 0000:01:01.0: ttyS4 at I/O 0x9800 (irq = 19) is a ST16650V2
[ 0.936942] 0000:01:01.1: ttyS5 at I/O 0x9c00 (irq = 18) is a ST16650V2
Above is an example output of my PC. You can see the initialization of a few serial ports:
ttyS0, ttyS1, ttyS4, ttyS5.
One of them is going to have a positive voltage upon a device plugged in. So by comparing the content of the file /proc/tty/driver/serial with and without the device plugged in we can easily find the ttyS related to our device. So, now do:
$ sudo cat /proc/tty/driver/serial> /tmp/1
(un)plug a device
$ sudo cat /proc/tty/driver/serial> /tmp/2
Next check the difference between the two files. Below is an output of my PC:
$ diff /tmp/1 /tmp/2
2c2
< 0: uart:16550A port:000003F8 irq:4 tx:6 rx:0
---
> 0: uart:16550A port:000003F8 irq:4 tx:6 rx:0 CTS|DSR
By comparing the three numbers with the dmesg output we can determine which one is the port:
[ 0.872181] 00:06: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
Hence, our device is /dev/ttyS0, mission accomplished!
You can also use setserial to get and set serial port information, in addition to dmesg.
Tested on Ubuntu 18.04:
setserial
# install it
sudo apt update
sudo apt install setserial
# Display (and sort) serial information for all /dev/ttyS* devices
setserial -g /dev/ttyS* | sort -V
# Display (and sort) serial information for all /dev/ttyUSB* devices
setserial -g /dev/ttyUSB* | sort -V
# To see extra info. such as the baud rate too, add `-G`:
setserial -g -G /dev/ttyS* | sort -V
setserial -g -G /dev/ttyUSB* | sort -V
Sample run and output. Notice that /dev/ttyS4 is the only port that appears to be alive and not "unknown" (whatever that means). (This /dev/ttyS4 device also shows up in the Arduino IDE even though it's not an Arduino and I don't know what it is.):
$ setserial -g -G /dev/ttyS* | sort -V
/dev/ttyS0 uart unknown port 0x03f8 irq 4 baud_base 115200 spd_normal skip_test
/dev/ttyS1 uart unknown port 0x02f8 irq 3 baud_base 115200 spd_normal skip_test
/dev/ttyS2 uart unknown port 0x03e8 irq 4 baud_base 115200 spd_normal skip_test
/dev/ttyS3 uart unknown port 0x02e8 irq 3 baud_base 115200 spd_normal
/dev/ttyS4 uart 16550A port 0x3060 irq 19 baud_base 115200 spd_normal skip_test
/dev/ttyS5 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS6 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS7 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS8 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS9 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS10 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS11 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS12 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS13 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS14 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS15 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS16 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS17 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS18 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS19 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS20 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS21 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS22 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS23 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS24 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS25 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS26 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS27 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS28 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS29 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS30 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
/dev/ttyS31 uart unknown port 0x0000 irq 0 baud_base 0 spd_normal
dmesg
Use dmesg to also see some of this information:
# Show UART information, incl. baudrate, for /dev/ttyS* devices.
dmesg | grep ttyS
# Show UART information, incl. baudrate, for /dev/ttyUSB* devices.
dmesg | grep ttyUSB
Sample run and output:
$ dmesg | grep ttyS
[ 1.624447] 0000:00:16.3: ttyS4 at I/O 0x3060 (irq = 19, base_baud = 115200) is a 16550A
Even better, watch the dmesg kernel ring buffer output live with the -w option:
dmesg -wH
Now, unplug your USB device you're trying to identify and plug it back in to see which device it is, based on the messages that show up in the dmesg output, live.
Finding out COM port for serial cable in Linux?
How to find the serial port a specific device is plugged into (Ubuntu 22+)
Serial Port device names
What is my /dev/tty name for my USB console cable? Help!
Videos
Does anyone know how to find out which COM port a serial cable is on for an Ubuntu machine?
I know in windows it's easily found in the device manager but I'm trying to learn some new skills so my laptop (personal) is now a linuxbox.
I think it's this command:
dmesg | grep tty
Running that on my own Linux box (which only has 1 Serial port) produces a single ttyS0 output line. Try it on your own, you will see what I mean.
See which UARTs where detected in /proc/tty/driver/serial. A line with uart:unknown means: nothing detected (and likely not existent).
# cat /proc/tty/driver/serial
serinfo:1.0 driver revision:
0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
1: uart:16550A port:000002F8 irq:3 tx:111780 rx:1321 RTS|DTR|DSR
2: uart:unknown port:000003E8 irq:4
3: uart:unknown port:000002E8 irq:3
If you see any of the CTS, DSR, (D)CD or RI flags (these are input signals), like on UART no. 1 above, you can even be pretty sure that there actually is something connected and driving these lines. Same is true for the rx-byte-count.
Seeing a positive tx-byte-count, RTS and/or DTR only reveals that some software accessed the device and ordered it to set those signals or send bytes here, but not if something was listening.
Note: you might see more ports available in hardware than ports reaching the outside of your computer in form of a connector.
I have tried lsusb which shows me the device I am looking for, but doesn't list the serial port anywhere, I have even tried verbose and I don't know what field lists the serial port. Everyone keeps saying to use dmesg | grep -i tty but this seems to just show ports and not what is plugged into them.