Spawn getty over serial: systemd

The switch to systemd from init has been wonderful in the world of *nix. Here’s how you could quickly get a getty running on a serial ttyUSB device in the old system:

  • Create a ttyUSB0.conf file inside /etc/init/ and fill it with start on stopped and RUNLEVEL information, followed by an exec statement, that reads something like: /sbin/getty -L 9600 ttyUSB0 vt102. (see addendum)
  • Tell init to pick it up, by adding to /etc/rc.local the line sudo start ttyUSB0.

(Warning: this is the way I do it, which naturally means there’s something ugly about it!).

Here’s how you’d do this through systemd:

  • Modify the system-file in /lib/systemd/system/serial-getty@.service to change the baud-rate to what you need (technically you shouldn’t do this, but we need to get moving quick).
    The line: ExecStart:-/sbin/agetty 9600 %I $TERM instead of --keep-baud. This will tell it to use a specific baudrate as opposed to scanning through a list.
  • The systemctl daemon needs to know that you meddled with the file (which you shouldn’t have, you know.. but we did anyway, spilt milk). Do:
    sudo systemctl daemon-reload
  • Fun part: just enable this setup once, and systemd will remember over reboots:
    sudo systemctl enable serial-getty@ttyUSB0Of course you can disable, start or stop this. Apparently it is not smart enough to ignore you when you start it twice. The way it enables is by creating a symlink from /etc/systemd/system/ to /lib/systemd/system/serial-getty@.service, in case the command doesn’t work (note that the latter is the file you edited previously). Feel free to explain this to me after reading more on this RHEL KB. :)


My old setup (using init) had this inside the ttyUSB0.conf file:
start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
exec /sbin/getty -L 9600 ttyUSB0 vt102

Ooh, how do you know if you’re using systemd or init? If you’re system is up, then PID 1 should tell you :)
(ps -p1).

Leave a Comment