Write a small script that connects a terminal session on the SSH server to the serial port without needing any command line parameters. For example, you might use the
cu command like this:
exec cu -l /dev/ttyS0 -s 115200 # add/modify options if needed
Save this to e.g.
/usr/local/bin/serial-connection and mark it executable with
chmod a+x /usr/local/bin/serial-connection.
Add the pathname
/usr/local/bin/serial-connection to file
Create a Linux user for the serial connection. You can name it however you want - for example,
serial. Set a password for it and verify that the user can log in normally.
Finally, change the
serial user's shell to
chsh -s /usr/local/bin/serial-connection serial
Now, as the
serial user logs in, the user's shell is automatically started - but now it will actually be the command that connects the SSH session to the serial port. And if that command ends for any reason, the SSH session will end automatically.
To terminate the connection, the
serial user will need to type
~. (tilde-dot) if using PuTTY or similar, or
~~. (tilde-tilde-dot) if using OpenSSH client. By chance, both
cu and OpenSSH client will use
~ as an escape character: typing it twice in the beginning of a line will emit a single tilde character for