3

I have a CentOS 7 headless system with no serial ports. I sometimes want to access the server using a serial cable, so I plug in a USB serial cable (to my laptop's serial port) but I can't get a console/BASH from the connection.

Is there something I have to do to tell the kernel to always create a serial console on appearance of a USB serial port?

TSG
  • 1,698

1 Answers1

5

EDIT: This won't work if you have a recent udev version, because udev prevents you from starting long-lived background processes in RUN scripts. You may or may not be able to get around this by prefixing the getty command with setsid but in any case it's discouraged if not outright disallowed. If you have a system which uses systemd then there is another way to achieve this, which I hope someone will supply with another answer. In the meantime, I'm leaving this answer here in case it works for you.


You cannot use a USB serial port as a console because USB is initialized too late in the boot sequence, long after the console needs to be working.

You can run getty on a USB serial port to allow you to log in and get a shell session on that port, but it will not be the system's console.

To get getty to start automatically, try this udev rule:

ACTION=="add", SUBSYSTEM=="tty", ENV{ID_BUS}=="usb", RUN+="/usr/local/sbin/usbrungetty"

Put that in a rules file in /etc/udev/rules.d and then create this executable script /usr/local/sbin/usbrungetty:

#!/bin/sh

/sbin/getty -L "$DEVNAME" 115200 vt102 &
Celada
  • 44,132
  • 3
    With judicious use of SYSTEMD_WANTS you should be able to start serial-getty@$name.service directly, eliminate the shell script middleman entirely, and have proper full control over the resultant service. This has the added bonus of conforming to the precept that udev children and grandchildren spawned from rules not be long-running processes. – JdeBP Dec 16 '15 at 07:53
  • You're correct, there's a different "right way" to do this in the systemd world. I didn't include it because I don't know systemd. As for the long-running process restriction on udev-spawned scripts, my understanding is that it's okay if you start something in the background, as long as the actual script udev launches terminates quickly. – Celada Dec 16 '15 at 07:58
  • No. As of 2012, grandchildren must be short-lived, too, now. Another benefit of not having a hand-written getty invocation is that serial-getty@.service is well known as the place to tweak settings such as TERM. – JdeBP Dec 16 '15 at 08:13
  • I stand corrected. – Celada Dec 16 '15 at 08:18
  • Now if only jasonwryan of aforelinked web log had a StackExchange account and there were some form of @jasonwryan txt spk for getting xyr attention so that xe knew that there was an answer that xe could write. (-: – JdeBP Dec 16 '15 at 08:27
  • @JdeBP - it actually doesn't unless he's in the thread. – mikeserv Dec 16 '15 at 10:20
  • 1
    Since CentOS7 uses systemd, can someone provide an example of how to do this? – TSG Dec 16 '15 at 15:41