8

(I am using Ubuntu.) In my understanding tty is just a kind of port. You input something and it should output something. And a terminal (emulator) is a software handle those input and output. Literally terminal can choose which port to connect to.

I saw a lot of tty files in /dev and a lot of pts files in /dev/pts/ I don't know why there are so many of them. Are they all being used?

If not, can I start a terminal with specified pty?

e.g. gnome-terminal /dev/pts/47

Update

The reason I do this is I want to have a remote terminal. I know the appropriate approach is to use ssh or telnet.

But for temporary access, it is not very convenient. I want to use socat to forward a tty or pty to client. Then client can start a terminal with that pty or tty.

The following code works fine. It is executed on client and on the server side I get "hello".

echo "hello" > /home/myhomefoler/pty_created_by_socat
9

On UNIX, a tty (like many other things) appears as a file. Data written to the tty device goes to the terminal and data coming from the terminal is available for reading on the tty.

If the tty is a hardware serial port, then data written to it gets sent on the wire and data coming from the wire appears on the tty.

If the tty is a machine's video console then data written to it gets displayed on the screen and data coming from the keyboard appears on the tty.

If the tty is a pseudo-tty (virtual terminal) connected to an X terminal emulator such as gnome-terminal then data written to it gets delivered to the X terminal emulator software and in turn gets shown in a window, while data typed into that window is available for reading on the virtual terminal. The software is said to be connected to the "master" end of the pseudo-terminal. Real terminals don't have a "master" end because there's a real device behind them (like the serial port), not a virtual device implemented by a piece of software.

So you can see already that it wouldn't make any sense to ask a X terminal emulator to operate on an arbitrary tty device such as /dev/ttyS0 (a real serial port, on Linux). It must be a pseudo-tty.

But can the terminal emulator choose the numeric ID of the pseudo-terminal device that it uses? In principle, it would be possible for a kernel to permit this, but in fact the kernel interface for allocating a new pseudo-terminal does not support it: the kernel makes it own choice. (In the SysV model pesudo-terminals are created by opening a special device called /dev/ptmx and the lowest-numbered available pseudo-terminal device is automatically allocated.)

But: why would you need to choose the number of the pseudo-terminal device that gets allocated? The kernel chooses an unused one that's guaranteed to be available and usable. Do you have a reason why you'd prefer if it chose a different one?

As to you other question:

I saw a lot of tty files in /dev and a lot of pts files in /dev/pts/ I don't know why there are so many of them. Are they all being used?

It depends on the system. On some systems, all possible pseudo-terminal devices are pre-created in /dev/pts or /dev, whether they are in use or not. On others, the device nodes only exist if the pseudo-terminal is in use. You say you are using Ununbu, which uses Linux, which is the latter type. So, yes, all of the devices nodes you see in /dev/pts are presently in use.

But for temporary access, it is not very convenient. I want to use socat to forward a tty or pty to client. Then client can start a terminal with that pty or tty.

If you want socat or any other piece of software to connect to the master end of a pseudo-terminal, then you need for that software to specifically support doing that. But you're in luck because socat does. For example, if I run:

socat PTY,link=/tmp/socat.pty TCP-LISTEN:2222 &
sleep 1 && ( setsid bash ) </tmp/socat.pty >/tmp/socat.pty 2>&1

I can get a shell by connecting to port 2222 from somewhere else. Very dangerous from a security point of view!!!

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.