How can I connect a bash process to a tty _without_ using getty

I would have two serial usb devices connected to each other, /dev/ttyUSB1 and /dev/ttyUSB2. I would like to run /bin/bash connected to one end, so that I can attach to the other end using picocom and use picocom as a local terminal emulator.

I have tried stuff like this

setsid /bin/bash </dev/ttyUSB2 >/dev/ttyUSB2 2>&1

and then connecting to the other end with picocom, but I just get a screen full of gibberish (:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$ 0m:~$).. (the baud rates match, and the two terminals can communicate with two picocom on both ends)

However, I really have no idea what I am doing. Can anybody tell me the secret?


On a different computer, I not longer get the gibberish I was getting before. When I connect bash to one tty as above, and connect to the other end with picocom, I get the prompt now, and I can run commands, but my commands don’t get echoed back to me (I only see the result), and I don’t have "job control" (which I test with Ctrl+C, which will not stop a sleep process)


I have already successfully use picocom as a local terminal emulator in two other ways:

  1. with getty:

    sudo systemctl start getty@ttyUSB2.service

    allows me to connect to /dev/ttyUSB1 with picocom successfully, but I have to log in.
    (thanks to this answer to a previous question)

  2. with socat

    $ socat -d -d PTY,raw,echo=0 EXEC:"/bin/bash",pty,stderr,setsid,sane
    2024/04/01 18:32:13 socat[40547] N PTY is /dev/pts/4

    which allows me to use picocom as a local terminal like this picocom /dev/pts/4

so yeah I should be able to connect /bin/bash to the usb tty myself right? but my trials all have errors.

Asked By: Alex028502


Your first example looks correct (but see my note at the bottom as well).

I have a machine on my desk to which I am connected via a serial port. On the target machine, the serial port is /dev/ttyS0. On my desktop, the port is /dev/ttyUSB0.

If I connect from the host using a communications tool like picocom:

$ picocom -b 115200 /dev/ttyUSB0

And then stop the getty that normally runs on the serial port and run:

# systemctl stop serial-getty@ttyS0
# setsid bash > /dev/ttyS0 < /dev/ttyS0 2>&1

Everything works as expected. In picocom, I see:

root@radio0:/boot/firmware# tty

Are you sure the baud rate matches in both instances? You can set the baud rate explicitly using stty doing something like:

# stty 115200 < /dev/ttyS0

If your goal is to automatically start a bash shell that doesn’t require a login, see also this article (disclaimer: I wrote that) that describes how to configure getty and pam to provide exactly that.

Answered By: larsks

There were two problems here:

  • the gibberish, which as the accepted answer pointed out, has nothing to do with anything in the question, and can’t be easily reproduced
  • job control and echo didn’t work – the solution important thing is to use ‘sane’ on the end that bash is connected to: (before connecting bash probably)
stty sane -F /dev/ttyUSB2
setsid /bin/bash </dev/ttyUSB2 >/dev/ttyUSB2 2>&1

This is built in to the socat command for the pts version in the question.

Answered By: Alex028502
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.