2

I need to a way to connect SSH to the serial port. There is a device connected at a serial port with a shell. I basically want to pipe the SSH client directly to the serial port. Basically you are connected through SSH to the serial console.

SSH client -> SSH server -> serial connected device.

How can I do this? Bridging the SSH to the serial port. The client shouldn't execute any command. It should be directly in the serial console.

3

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:

#!/bin/sh
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 /etc/shells.

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 /usr/local/bin/serial-connection:

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 cu.

|improve this answer|||||
  • 1
    I'd also look into ForceCommand of sshd_config (instead of making it a login shell) to make sure anyone connecting with ssh will always execute the "serial-connection" command, and can't substitute another command. – dirkt Oct 31 '19 at 11:20
  • 2
    The command specified by ForceCommand or specified by the client is executed by the user's login shell, so changing the shell to something that does a fixed thing and won't accept any actual commands already covers that. – telcoM Oct 31 '19 at 11:52

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.