One of the indispensable elements when working on embedded Linux projects is accessing your device via the serial console interface. For this, the computer on which you are developing an embedded system must have a serial port. However, very few computers today have a serial port, and that's why we use USB to serial converters instead.

By the end, you'll have a detailed understanding of what serial consoles are and how they work on Linux.

Setting Up the USB to Serial Converter

Almost any USB serial converter you can find on the market is automatically recognized by Linux. Plug any USB converter into your computer and use the dmesg command to find out the file name of the device.

        dmesg
dmesg-serial-usb-converter-info-command

In the example above, you see VirtualBox in the manufacturer section, as the host is a Linux distribution running on VirtualBox. Here, the manufacturer name will change depending on the converter you are using. You can also see that the serial converter you are using is attached to the system with a name, such as ttyUSB0.

You can use the serial converter recognized by your system via the device file /dev/ttyUSB0. Depending on the distribution you use, the device file is automatically created under the /dev directory, showing major, minor, and device types. For USB serial converters, the naming scheme is usually ttyUSB0, ttyUSB1, and ttyUSBX for each simultaneous translation.

If you remove and reinsert the USB serial converter while it is in use by an application, the system issues a new number to the device.

Access Authorization on Serial Devices

The default access privileges of mounted USB converter device files are usually as follows:

        ls -l /dev/ttyUSB0


# Output
crw-rw---- 1 root dialout 188, 46 Jul 17 15:34 /dev/ttyUSB0

On examining the above output, you can tell:

  • The letter c at the beginning of the line denotes that this is a character-based device
  • The file owner is the root user and the user has read and write privileges
  • The group owner of the file is the dialout group and users included in this group also have read and write permissions
  • The rest of the users do not have any read and write rights on the file

If you're currently logged in as a regular user, who isn't a member of the dialout group, you cannot read and write to the USB serial converter. To solve this problem, you must either make the current user a member of the dialout group or edit the udev rule files on your system.

You can utilize the first technique for convenience. To begin, use the id command to determine which groups your user belongs to:

        id

Add your user to the group using the adduser or usermod command:

        # For Fedora
sudo usermod -aG dialout USERNAME


# For Debian
sudo adduser USERNAME dialout


# For Arch
sudo usermod -a -G uucp USERNAME

The group membership procedure is now complete. However, the system checks group membership information during the first login step in graphical sessions and then provides it to all running processes.

As a result, you must log out of the current graphical session and then log back in or restart your machine for the modifications to take effect. You will have read/write access to the serial converter device after finishing this operation.

How to Connect to Serial Consoles on Linux

When you need access to a computer or network console, you can refer to serial console applications. Usually, you require access over SSH to do so. However, from a software and hardware point of view, in some cases, it is also possible to access the console using only serial ports.

You'll find these types of examples most often on older computers. Below are some of the best serial console applications that will work for you.

Using Minicom

You can install Minicom, an old but still working application on your system as follows:

        # On Fedora, CentOS, and RHEL
sudo yum install minicom


# On Debian and Ubuntu
sudo apt-get install minicom


# On Arch Linux
sudo snap install minicom

When you launch the application for the first time, it will try to open a device file such as /dev/modem by default. It terminates when it cannot find the file. To overcome this problem, you can directly start the application's settings screen with the -s parameter:

        minicom -s
    
minicom-s-command-output-serial-ports

You can set the device name by pressing the A key, and the serial port speed by pressing the E key. Then when you exit with Exit, you can use the corresponding serial port.

minicom-s-command-serial-port-setup-option

While on the application main screen, you can return to the configuration screen with Ctrl + A + O, exit the application with Ctrl + A + X, activate the line wrap mode with Ctrl + A + W, and get help with other shortcuts with Ctrl + A + Z.

Using GTKTerm

Minicom is generally used by advanced Linux users and therefore, is not recommended for beginners. Linux newcomers can install GTKTerm, a graphical serial terminal emulator for Linux. You can install the application on your system using the following commands:

        # On Fedora, CentOS, and RHEL
sudo dnf -y install gtkterm


# On Debian and Ubuntu
sudo apt-get install gtkterm


# On Arch Linux
yay -S gtkterm

When you launch the app, you'll have to set the device name and speed parameters via the Configuration > Port menu as follows:

gtkterm-gui-connection-rate-speed

Save the settings as default to facilitate later use.

Using screen

If you are familiar with screen for multi-terminal management, you'd know that you can use it for serial port access as well. Here are the commands to install it:

        # On Fedora, CentOS, and RHEL
sudo dnf -y install screen


# On Debian, Ubuntu, and Linux Mint
sudo apt-get -y install screen


# Use Snap for other distributions
sudo snap install screen --classic

After installation, you can run a command like this to connect to a serial console:

        screen /dev/ttyUSB0 115500
    

Now You Can Establish a Serial Port Connection on Linux

Being able to access your Linux system using the serial port is a very useful feature, especially when you don't want to attach a monitor to the SBC (Session Border Controller).

Since the serial console authorization is built into the Linux kernel at the time of compilation, you should have at least a basic understanding of the Linux kernel and its working.