I'm trying to forward a shell to serial (USB to RS232) so I can use an old monochrome CRT terminal.
How can I forward a shell to a serial port so the other end can send commands & read output just like a TTY?
Note: the answer aggregates few solutions from various sites; at the moment I have no means to test them.
Pre-systemd
Linux may use sysvinit's /etc/inittab
to spawn getty
on various terminals. The example line may look like this (taken from this old guide):
s0:2345:respawn:/sbin/getty -L 115200 ttyS0 vt102
If your OS uses upstart
, the procedure is different. E.g. there is this howto:
Create a file called
/etc/init/ttyS0.conf
containing the following:# ttyS0 - getty # # This service maintains a getty on ttyS0 from the point the system is # started until it is shut down again. start on stopped rc RUNLEVEL=[12345] stop on runlevel [!12345] respawn exec /sbin/getty -L 115200 ttyS0 vt102
Ask
upstart
to start thegetty
sudo start ttyS0
According to this site a systemd
solution may be as simple as
To make use of a serial console, just use
console=ttyS0
on the kernel command line, andsystemd
will automatically start agetty
on it for you.
You would probably configure your GRUB2 to do this. Analyze what Arch Wiki says and adjust to your distro, if needed:
To make grub enable the serial console, open
/etc/default/grub
in an editor. Change theGRUB_CMDLINE_DEFAULT
line to start the console on/dev/ttyS0
. Note in the example below, we set two consoles up; one ontty0
and one on the serial port.GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,38400n8"
Now we need to tell grub where is the console and what command to start in order to enable the serial console (Note as above for Linux kernel, one can append multiple input/output terminals in grub e.g.
GRUB_TERMINAL="console serial"
would enable both display and serial):## Serial console GRUB_TERMINAL=serial GRUB_SERIAL_COMMAND="serial --speed=38400 --unit=0 --word=8 --parity=no --stop=1"
Rebuild the
grub.cfg
file with following command:grub-mkconfig -o /boot/grub/grub.cfg
After a reboot,
getty
will be listening on/dev/ttyS0
, expecting38400
baud,8
data bits, no parity and one stop bit. When Arch boots,systemd
will automatically start agetty
session to listen on the same device with the same settings.
Both sites agree that if you do not want GRUB2 to listen on the serial device, but only want getty
listening after boot then you will need something like
systemctl enable [email protected]
systemctl start [email protected]
In case of any problem remember that getty
is just a program, it can be run on demand or from rc.local
. Refer to man getty
for details. I think your first try may be like
sudo getty -L 115200 ttyS0 vt102
(Edit) This is the feedback from the OP, it may help users with similar problems:
I had to do
sudo su -c "…"
for it to work properly.
getty
oragetty
servicing your pseudo-terminals (Ctrl-Alt-Fn), such as/sbin/agetty --noclear tty1 linux
. If you create a new instance withtty1
replaced by the serial terminal device name, you should be able to log in on your serial port. You will probably need to add serial options (Baud rate, bits per character, parity, etc), unless you are happy to use the defaults.getty
? Do you get a shell on the serial terminal?getty
processes are launched from/etc/init/tty?.conf
.