Jun 22, 2014 Traditionally in Linux, the first serial port (COM1) is assigned a name /dev/ttyS0, the second serial port (COM2) assigned /dev/ttyS1, etc. If you specify a serial port name as the first argument of the screen command, the current terminal window where you run screen will be.
- Apr 09, 2019 USB-Serial Port adapter (RS-232) in Ubuntu Linux There are two other articles on a serial port adapter for Windows. Below is the instructions for Linux and how to connect your serial adapter while using Linux. This should work for most people using Linux and a serial adapter or usb to rs232 driver which is being discussed.
- How to create a virtual serial/COM port in Linux I was hoping to have my hardware finished by now so I could get on with testing the software over Easter. The hardware does not work yet but I would still like to test the software.
- On Linux, I want to send a command string (i.e. Some data) to a serial port (containing control characters), and listen to the response (which also usually might contain control characters).
Active1 year ago
What is the proper way to get a list of all available serial ports/devices on a Linux system?
In other words, when I iterate over all devices in
/dev/
, how do I tell which ones are serial ports in the classic way, that is, those usually supporting baud rates and RTS/CTS flow control?The solution would be coded in C.
I ask because I am using a third-party library that does this clearly wrong: It appears to only iterate over
/dev/ttyS*
. The problem is that there are, for instance, serial ports over USB (provided by USB-RS232 adapters), and those are listed under /dev/ttyUSB*. And reading the Serial-HOWTO at Linux.org, I get the idea that there'll be other name spaces as well, as time comes.So I need to find the official way to detect serial devices. The problem is that none appears to be documented, or I can't find it.
I imagine one way would be to open all files from
/dev/tty*
and call a specific ioctl()
on them that is only available on serial devices. Would that be a good solution, though?Update
hrickards suggested to look at the source for 'setserial'.Its code does exactly what I had in mind:
First, it opens a device with:
Then it invokes:
If that call returns no error, then it's a serial device, apparently.
I found similar code in Serial Programming/termios, which suggested to also add the
O_NOCTTY
option.There is one problem with this approach, though:
When I tested this code on BSD Unix (that is, Mac OS X), it worked as well. However, serial devices that are provided through Bluetooth cause the system (driver) to try to connect to the Bluetooth device, which takes a while before it'll return with a timeout error. This is caused by just opening the device. And I can imagine that similar things can happen on Linux as well - ideally, I should not need to open the device to figure out its type. I wonder if there's also a way to invoke
ioctl
functions without an open, or open a device in a way that it does not cause connections to be made?What should I do?
Thomas Tempelmann
Linux Serial Port Terminal
Thomas TempelmannThomas Tempelmann6,35233 gold badges5252 silver badges9999 bronze badges
11 Answers
The
/sys
filesystem should contain plenty information for your quest. My system (2.6.32-40-generic #87-Ubuntu) suggests:Which gives you descriptions of all TTY devices known to the system. A trimmed down example:
Following one of these links:
Here the
dev
file contains this information:This is the major/minor node. These can be searched in the
/dev
directory to get user-friendly names:The
A.H.A.H./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:48.9k1111 gold badges7272 silver badges102102 bronze badges
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:
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 :).
flu0flu0
I'm doing something like the following code. It works for USB-devices and also the stupid serial8250-devuices that we all have 30 of - but only a couple of them realy works.
Basically I use concept from previous answers. First enumerate all tty-devices in /sys/class/tty/. Devices that does not contain a /device subdir is filtered away. /sys/class/tty/console is such a device. Then the devices actually containing a devices in then accepted as valid serial-port depending on the target of the driver-symlink fx.
and for ttyS0
All drivers driven by serial8250 must be probes using the previously mentioned ioctl.
Only port reporting a valid device-type is valid.
The complete source for enumerating the serialports looks like this. Additions are welcome.
Søren HolmSøren Holm
I think I found the answer in my kernel source documentation:/usr/src/linux-2.6.37-rc3/Documentation/filesystems/proc.txt
Here is a link to this file:http://git.kernel.org/?p=linux/kernel/git/next/linux-next.git;a=blob_plain;f=Documentation/filesystems/proc.txt;hb=e8883f8057c0f7c9950fa9f20568f37bfa62f34a
mk2mk2
user6655984
RealHuman75RealHuman75
setserial with the -g option appears to do what you want and the C source is available at http://www.koders.com/c/fid39344DABD14604E70DF1B8FEA7D920A94AF78BF8.aspx.
hrickardshrickards58122 gold badges66 silver badges2020 bronze badges
I have no serial device here to test it, but if you have python and dbus you can try it yourself.
If it fails you can search inside
hwmanager_i.GetAllDevicesWithProperties()
to see if the capability name 'serial' that I just guessed has a different name.HTH
baolbaol
I do not have a USB serial device, but there must be a way to find the real ports using the HAL libraries directly:
The posted python-dbus code nor this sh script lists the bluetooth /dev/rfcomm* devices, so it is not the best solution.
Note that on other unix platforms, the serial ports are not named ttyS? and even in linux, some serial cards allow you to name the devices. Assuming a pattern in the serial devices names is wrong.
kelk1kelk1
Using /proc/tty/drivers only indicates which tty drivers are loaded. If you're looking for a list of the serial ports check out /dev/serial, it will have two subdirectories: by-id and by-path.
EX:
Thanks to this post: https://superuser.com/questions/131044/how-do-i-know-which-dev-ttys-is-my-serial-port
Community♦
blarfblarf
My approach via group dialout to get every tty with user 'dialout'
ls -l /dev/tty* | grep 'dialout'
to only get its folderls -l /dev/tty* | grep 'dialout' | rev | cut -d ' ' -f1 | rev
Presonus universal control download. Our good friends at PreSonus have released, as well as a new IOS/Android app, and an update to.
easy listen to the tty output e.g. when arduino serial out:
head --lines 1 < /dev/ttyUSB0
listen to every tty out for one line only:
for i in $(ls -l /dev/tty* | grep 'dialout' | rev | cut -d ' ' -f1 | rev); do head --lines 1 < $i; done
I really like the approach via looking for drivers:
ll /sys/class/tty/*/device/driver
You can pick the tty-Name now:
ls /sys/class/tty/*/device/driver | grep 'driver' | cut -d '/' -f 5
McPepprMcPeppr
The serial communication manager library has many API and features targeted for the task you want. If the device is a USB-UART its VID/PID can be used. If the device is BT-SPP than platform specific APIs can be used. Take a look at this project for serial port programming: https://github.com/RishiGupta12/serial-communication-manager
samuel05051980samuel05051980
Not the answer you're looking for? Browse other questions tagged linuxserial-port or ask your own question.
Linux Serial Port Programming
-->The Serial Console in the Azure portal provides access to a text-based console for Linux virtual machines (VMs) and virtual machine scale set instances. This serial connection connects to the ttys0 serial port of the VM or virtual machine scale set instance, providing access to it independent of the network or operating system state. The serial console can only be accessed by using the Azure portal and is allowed only for those users who have an access role of Contributor or higher to the VM or virtual machine scale set.
Serial Console works in the same manner for VMs and virtual machine scale set instances. In this doc, all mentions to VMs will implicitly include virtual machine scale set instances unless otherwise stated.
For Serial Console documentation for Windows, see Serial Console for Windows.
Note
If you want this to be automatic, select Change settings and you will be able to specify that you want Windows to check for and install updates automatically. To check whether they’re available, do a system search for windows update, and select Check for updates.
This will check for important and optional updates that are available and then allow you to select which you want to install. You can narrow this down to how often it installs them and at what time.
![Update audio drivers windows 7 for 64 bit Update audio drivers windows 7 for 64 bit](https://thewindowsclub-thewindowsclubco.netdna-ssl.com/wp-content/uploads/2010/04/device-manager-uninstall-400x296.png)
The Serial Console is generally available in global Azure regions. It is not yet available in Azure government or Azure China clouds.
Prerequisites
- Your VM or virtual machine scale set instance must use the resource management deployment model. Classic deployments aren't supported.
- Your account that uses serial console must have the Virtual Machine Contributor role for the VM and the boot diagnostics storage account
- Your VM or virtual machine scale set instance must have a password-based user. You can create one with the reset password function of the VM access extension. Select Reset password from the Support + troubleshooting section.
- Your VM or virtual machine scale set instance must have boot diagnostics enabled.
- For settings specific to Linux distributions, see Serial console Linux distribution availability.
- Your VM or virtual machine scale set instance must be configured for serial output on
ttys0
. This is the default for Azure images, but you will want to double check this on custom images. Details below.
Note
The serial console requires a local user with a configured password. VMs or virtual machine scale sets configured only with an SSH public key won't be able to sign in to the serial console. To create a local user with a password, use the VMAccess Extension, which is available in the portal by selecting Reset password in the Azure portal, and create a local user with a password.You can also reset the administrator password in your account by using GRUB to boot into single user mode.
Serial Console Linux distribution availability
For the serial console to function properly, the guest operating system must be configured to read and write console messages to the serial port. Most Endorsed Azure Linux distributions have the serial console configured by default. Selecting Serial console in the Support + troubleshooting section of the Azure portal provides access to the serial console.
Note
If you are not seeing anything in the serial console, make sure that boot diagnostics is enabled on your VM. Hitting Enter will often fix issues where nothing is showing up in the serial console.
Distribution | Serial console access |
---|---|
Red Hat Enterprise Linux | Serial console access enabled by default. |
CentOS | Serial console access enabled by default. |
Ubuntu | Serial console access enabled by default. |
CoreOS | Serial console access enabled by default. |
SUSE | Newer SLES images available on Azure have serial console access enabled by default. If you're using older versions (10 or earlier) of SLES on Azure, see the KB article to enable serial console. |
Oracle Linux | Serial console access enabled by default. |
Custom Linux images
To enable the serial console for your custom Linux VM image, enable console access in the file /etc/inittab to run a terminal on
ttyS0
. For example: S0:12345:respawn:/sbin/agetty -L 115200 console vt102
. You may also need to spawn a getty on ttyS0. This can be done with systemctl start [email protected]
.You will also want to add ttys0 as the destination for serial output. For more information on configuring a custom image to work with the serial console, see the general system requirements at Create and upload a Linux VHD in Azure.
If you're building a custom kernel, consider enabling these kernel flags:
CONFIG_SERIAL_8250=y
and CONFIG_MAGIC_SYSRQ_SERIAL=y
. The configuration file is typically located in the /boot/ path.Common scenarios for accessing the Serial Console
Scenario | Actions in the Serial Console |
---|---|
Broken FSTAB file | Press the Enter key to continue and use a text editor to fix the FSTAB file. You might need to be in single user mode to do so. For more information, see the serial console section of How to fix fstab issues and Use serial console to access GRUB and single user mode. |
Incorrect firewall rules | If you have configured iptables to block SSH connectivity, you can use serial console to interact with your VM without needing SSH. More details can be found at the iptables man page. Similarly, if your firewalld is blocking SSH access, you can access the VM through serial console and reconfigure firewalld. More details can be found in the firewalld documentation. |
Filesystem corruption/check | Please see the serial console section of Azure Linux VM cannot start because of file system errors for more details on troubleshooting corrupted file systems using serial console. |
SSH configuration issues | Access the serial console and change the settings. Serial console can be used regardless of the SSH configuration of a VM as it does not require the VM to have network connectivity to work. A troubleshooting guide is available at Troubleshoot SSH connections to an Azure Linux VM that fails, errors out, or is refused. More details are available at Detailed SSH troubleshooting steps for issues connecting to a Linux VM in Azure |
Interacting with bootloader | Restart your VM from within the serial console blade to access GRUB on your Linux VM. For more details and distro-specific information, see Use serial console to access GRUB and single user mode. |
Disable the Serial Console
By default, all subscriptions have serial console access enabled. You can disable the serial console at either the subscription level or VM/virtual machine scale set level. For detailed instructions, visit Enable and disable the Azure Serial Console.
![Linux Serial Port Linux Serial Port](/uploads/1/2/5/3/125378196/729186957.png)
Serial console security
Access security
Access to the serial console is limited to users who have an access role of Virtual Machine Contributor or higher to the virtual machine. If your Azure Active Directory tenant requires multi-factor authentication (MFA), then access to the serial console will also need MFA because the serial console's access is through the Azure portal.
Channel security
All data that is sent back and forth is encrypted on the wire.
Audit logs
All access to the serial console is currently logged in the boot diagnostics logs of the virtual machine. Access to these logs are owned and controlled by the Azure virtual machine administrator.
Caution
No access passwords for the console are logged. However, if commands run within the console contain or output passwords, secrets, user names, or any other form of personally identifiable information (PII), those will be written to the VM boot diagnostics logs. They will be written along with all other visible text, as part of the implementation of the serial console's scroll back function. These logs are circular and only individuals with read permissions to the diagnostics storage account have access to them. However, we recommend following the best practice of using the Remote Desktop for anything that may involve secrets and/or PII.
Concurrent usage
If a user is connected to the serial console and another user successfully requests access to that same virtual machine, the first user will be disconnected and the second user connected to the same session.
Caution
This means that a user who's disconnected won't be logged out. The ability to enforce a logout upon disconnect (by using SIGHUP or similar mechanism) is still on the roadmap. For Windows there is an automatic timeout enabled in Special Administrative Console (SAC); however, for Linux you can configure the terminal timeout setting. To do so, add
export TMOUT=600
in your .bash_profile or .profile file for the user you use to sign in to the console. This setting will time out the session after 10 minutes.Accessibility
Accessibility is a key focus for the Azure Serial Console. To that end, we've ensured that the serial console is fully accessible.
Keyboard navigation
Use the Tab key on your keyboard to navigate in the serial console interface from the Azure portal. Your location will be highlighted on screen. To leave the focus of the serial console window, press Ctrl+F6 on your keyboard.
Use Serial Console with a screen reader
The serial console has screen reader support built in. Navigating around with a screen reader turned on will allow the alt text for the currently selected button to be read aloud by the screen reader.
Known issues
We're aware of some issues with the serial console. Here's a list of these issues and steps for mitigation. These issues and mitigations apply for both VMs and virtual machine scale set instances.
Issue | Mitigation |
---|---|
Pressing Enter after the connection banner does not cause a sign-in prompt to be displayed. | For more information, see Hitting enter does nothing. This issue can occur if you're running a custom VM, hardened appliance, or GRUB config that causes Linux to fail to connect to the serial port. |
Serial console text only takes up a portion of the screen size (often after using a text editor). | Serial consoles do not support negotiating about window size (RFC 1073), which means that there will be no SIGWINCH signal sent to update screen size and the VM will have no knowledge of your terminal's size. Install xterm or a similar utility to provide you with the resize command, and then run resize . |
Pasting long strings doesn't work. | The serial console limits the length of strings pasted into the terminal to 2048 characters to prevent overloading the serial port bandwidth. |
Serial console does not work with a storage account firewall. | Serial console by design cannot work with storage account firewalls enabled on the boot diagnostics storage account. |
Serial console does not work with a storage account using Azure Data Lake Storage Gen2 with hierarchical namespaces. | This is a known issue with hierarchical namespaces. To mitigate, ensure that your VM's boot diagnostics storage account is not created using Azure Data Lake Storage Gen2. This option can only be set upon storage account creation. You may have to create a separate boot diagnostics storage account without Azure Data Lake Storage Gen2 enabled to mitigate this issue. |
Erratic keyboard input in SLES BYOS images. Keyboard input is only sporadically recognized. | This is an issue with the Plymouth package. Plymouth should not be run in Azure as you don't need a splash screen and Plymouth interferes with the platform ability to use Serial Console. Remove Plymouth with sudo zypper remove plymouth and then reboot. Alternatively, modify the kernel line of your GRUB config by appending plymouth.enable=0 to the end of the line. You can do this by editing the boot entry at boot time, or by editing the GRUB_CMDLINE_LINUX line in /etc/default/grub , rebuilding GRUB with grub2-mkconfig -o /boot/grub2/grub.cfg , and then rebooting. |
Frequently asked questions
Q. How can I send feedback?
A. Provide feedback by creating a GitHub issue at https://aka.ms/serialconsolefeedback. Alternatively (less preferred), you can send feedback via [email protected] or in the virtual machine category of https://feedback.azure.com.
Q. Does the serial console support copy/paste?
A. Yes. Use Ctrl+Shift+C and Ctrl+Shift+V to copy and paste into the terminal.
Q. Can I use serial console instead of an SSH connection?
A. While this usage may seem technically possible, the serial console is intended to be used primarily as a troubleshooting tool in situations where connectivity via SSH isn't possible. We recommend against using the serial console as an SSH replacement for the following reasons:
- The serial console doesn't have as much bandwidth as SSH. Because it's a text-only connection, more GUI-heavy interactions are difficult.
- Serial console access is currently possible only by using a username and password. Because SSH keys are far more secure than username/password combinations, from a sign-in security perspective, we recommend SSH over serial console.
Q. Who can enable or disable serial console for my subscription?
A. To enable or disable the serial console at a subscription-wide level, you must have write permissions to the subscription. Roles that have write permission include administrator or owner roles. Custom roles can also have write permissions.
Q. Who can access the serial console for my VM/virtual machine scale set?
A. You must have the Virtual Machine Contributor role or higher for a VM or virtual machine scale set to access the serial console.
Q. My serial console isn't displaying anything, what do I do?
A. Your image is likely misconfigured for serial console access. For information about configuring your image to enable the serial console, see Serial console Linux distribution availability.
Q. Is the serial console available for virtual machine scale sets?
A. Yes, it is! See Serial Console for Virtual Machine Scale Sets
Q. If I set up my VM or virtual machine scale set by using only SSH key authentication, can I still use the serial console to connect to my VM/virtual machine scale set instance?
A. Yes. Because the serial console doesn't require SSH keys, you only need to set up a username/password combination. You can do so by selecting Reset password in the Azure portal and using those credentials to sign in to the serial console.
Next steps
- Use the serial console to access GRUB and single user mode.
- Use the serial console for NMI and SysRq calls.
- Learn how to use the serial console to enable GRUB in various distros.
- The serial console is also available for Windows VMs.
- Learn more about boot diagnostics.