Skip to content

Instantly share code, notes, and snippets.

@AskAlice
Created February 1, 2026 18:38
Show Gist options
  • Select an option

  • Save AskAlice/e5dfcecf3ee8e4755f6fb4d6cb9dc651 to your computer and use it in GitHub Desktop.

Select an option

Save AskAlice/e5dfcecf3ee8e4755f6fb4d6cb9dc651 to your computer and use it in GitHub Desktop.

Bidirectional Serial Console Setup

Configure a serial port (e.g., /dev/ttyS0) to allow both incoming getty login and outbound terminal connections without conflicts.

Prerequisites

apt install util-linux procps

Method 1: agetty with wait-cr (Simplest)

systemd

Create /etc/systemd/system/[email protected]/bidirectional.conf:

[Service]
ExecStart=
ExecStart=-/sbin/agetty --noclear --keep-baud --wait-cr --nohangup --noissue 115200,38400,9600 %I xterm-256color

Enable:

systemctl daemon-reload
systemctl enable [email protected]
systemctl start [email protected]

SysV init

Add to /etc/inittab:

T0:23:respawn:/sbin/agetty -L --wait-cr --nohangup --noissue ttyS0 115200 xterm-256color

Reload:

init q

Method 2: mgetty (Feature-rich)

Install

apt install mgetty mgetty-fax

Configure

Edit /etc/mgetty+sendfax/mgetty.config:

port ttyS0
  speed 115200
  direct y
  fax n
  data y
  blocking n
  port-owner uucp
  port-group dialout
  port-mode 0660
  term xterm-256color

Add to /etc/inittab:

T0:23:respawn:/usr/sbin/mgetty ttyS0

Reload:

init q

Method 3: Custom Script (Full Control)

Create /usr/local/bin/bidir-getty:

#!/bin/bash
TTY=$1
stty -F /dev/$TTY -echo -icanon min 0 time 0
while :;do
  if fuser /dev/$TTY &>/dev/null;then sleep 1;continue;fi
  read -rsn1 -t86400 c </dev/$TTY || continue
  TERM=xterm-256color flock -x -n /var/lock/LCK..$TTY /sbin/agetty --login-options "$c" $TTY 115200 xterm-256color
done

Make executable:

chmod +x /usr/local/bin/bidir-getty

systemd service

Create /etc/systemd/system/[email protected]:

[Unit]
Description=Bidirectional Getty on %I
After=systemd-user-sessions.service

[Service]
ExecStart=/usr/local/bin/bidir-getty %I
Restart=always
StandardInput=tty
StandardOutput=tty

[Install]
WantedBy=multi-user.target

Enable:

systemctl daemon-reload
systemctl enable [email protected]
systemctl start [email protected]

SysV init

Add to /etc/inittab:

T0:23:respawn:/usr/local/bin/bidir-getty ttyS0

Reload:

init q

Testing

Outbound connection

screen /dev/ttyS0 115200
# or
minicom -D /dev/ttyS0

Inbound connection

Connect from another device and press Enter to trigger login prompt.

How It Works

  • --wait-cr: Getty waits silently for CR/LF before displaying login prompt
  • --nohangup: Doesn't send hangup signal, allows port sharing
  • --noissue: Suppresses pre-login banner
  • flock: File locking prevents simultaneous access conflicts
  • fuser: Checks if port is in use by another process

When outbound program (screen/minicom) opens the port, getty detects it's locked and waits. When released, getty resumes listening for incoming connections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment