1

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?

  • 1
    If you look at your running programs you should see some instances of getty or agetty servicing your pseudo-terminals (Ctrl-Alt-Fn), such as /sbin/agetty --noclear tty1 linux. If you create a new instance with tty1 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. – AFH Jul 1 '18 at 22:12
  • Yeah been trying getty too, except it kills the tab of my terminal after I enter my login name – V0idst4r Jul 1 '18 at 22:19
  • I'm not clear what you mean. Is the terminal tab that disappears the one that launched getty? Do you get a shell on the serial terminal? – AFH Jul 1 '18 at 22:24
  • I get the login prompt, and once I enter my username it kills the tty I'm running agetty in. (And then nothing happens on my terminal until I start it again). I suspect I have to run agetty from my init system? – V0idst4r Jul 1 '18 at 22:25
  • Possibly. In Ubuntu the getty processes are launched from /etc/init/tty?.conf. – AFH Jul 1 '18 at 22:30
2

Note: the answer aggregates few solutions from various sites; at the moment I have no means to test them.


SysVinit

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

Upstart

If your OS uses upstart, the procedure is different. E.g. there is this howto:

  1. 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
    
  2. Ask upstart to start the getty

    sudo start ttyS0
    

Systemd

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, and systemd will automatically start a getty 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 the GRUB_CMDLINE_DEFAULT line to start the console on /dev/ttyS0. Note in the example below, we set two consoles up; one on tty0 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, expecting 38400 baud, 8 data bits, no parity and one stop bit. When Arch boots, systemd will automatically start a getty 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 serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service

On demand

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.

  • Regarding on demand, I had to do sudo su -c "..." for it to work properly. – V0idst4r Jul 1 '18 at 22:35
  • The stuff about using the serial port as a console is unrelated to the stuff about enabling getty, and is independent of init, so it doesn't really fit under a "systemd" heading – user240960 Jul 1 '18 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.