Linux Commands

How to List Serial Ports on Linux

Serial ports are interfaces used to communicate with the serial devices connected to the system. Serial devices, such as mice, keyboards, and modems, communicate with the system by transmitting one bit at a time. The system establishes communication with the serial device using the serial port name. On Windows, COM1 or COM2 names are given to serial ports. While on Linux, the system ttyS0, ttyS1, and ttyUSB names are used.

By default, the serial devices automatically establish a connection to the system. However, as a system administrator or developer, it is important to know the serial port names. They are used in troubleshooting the system and applications.

Compared to Windows, finding serial port names on Linux is relatively challenging.

In this instructional guide, I will go through various approaches to displaying serial ports on Linux.

How to List Serial Ports on Linux

There are different approaches to listing the serial ports on Linux.

Through /sys/class Directory

On Linux, /sys/class directory contains information about devices connected to the system. These devices include block devices, serial devices, network devices, USB devices, and PCI devices. A device class signifies a specific type of device connected to the system, and it is used to efficiently manage those devices. To list the contents of the /sys/class directory, utilize the ls command with the -l flag.

ls -l /sys/class/tty/*/device/driver

This will list all the serial ports also including the virtual and pseudo devices. But, we are only interested in the available serial ports, which in my case is ttyAMA0. Let’s remove the /platform/drivers/serial8250 from the list using the grep filtration.

ls -l /sys/class/tty/*/device/driver/ | grep -v /platform/drivers/serial8250

The -v is used to invert the match, basically removing the match.

Now, it shows the ports that are available for serial communication.

To make things simple, a permanent alias can be created for the command given above, by placing it in the bashrc file.

alias getports='ls -l /sys/class/tty/*/device/driver/ | grep -v /platform/drivers/serial8250'

Through dmesg Command

To display the ports, the simplest approach is to use the dmesg command. The dmesg command is used to print the kernel ring buffer messages about hardware connected to the system, and the error encountered by the kernel during the operation of the system. Execute the dmesg command and grep for tty with sudo privileges.

sudo dmesg | grep tty

The output shows the serial port ttyAMA0.

Through Cutecom App

Another method to list the serial ports is to use GUI-based applications. To install the Cutecom application on Linux, use the following commands.

Ubuntu, LinuxMint, and other Debian-based distributions.

sudo apt install cutecom

For Fedora.

sudo dnf install cutecom

Cutecom cannot directly be installed on Red Hat Enterprise Linux. To install Cutecom on RHEL, first, we need to enable the EPEL release, which is short for Extra Packages for Enterprise Linux.

sudo yum install epel-release

Now, install it using.

sudo yum install cutecom

Now, launch the application.

Click on the drop-down menu beside Device to see a list of available serial ports. If you have attached multiple devices, then it will list the assigned ports of all the connected devices.

Conclusion

To list the serial ports on Linux is not a straightforward task. They can be viewed by listing the /sys/class directory. This directory contains information about serial ports. However, not all serial ports represent actual physical hardware. There are many virtual and pseudo devices. In this guide, I discussed how to list the serial ports using approaches such as listing the /sys/class directory, using the dmesg command, and installing the GUI-based application Cutecom.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.