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.
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.
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.
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.
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.
For Fedora.
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.
Now, install it using.
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.