Score:2

Terminal Output Redirected to another user's terminal

gf flag

I wanted to know if it would be possible to redirect the output of my terminal into another user's (that is part of my group) terminal. For example, I would like to be able to execute this command : date > /dev/pts/2 Is it possible for the other user to give me the permission using chmod xxx /dev/pts/2 and what would be the value replacing the xxx ? Thanks!

FedKad avatar
cn flag
Why don't you try it and see?
ReyRey avatar
gf flag
I only have access to a virtual machine from my university and cannot add other users to try...
Score:2
jp flag

Introduction

PTY(AKA Pseudoterminal) is used for implementing terminal emulators such as xterm … It is involved in enabling the interpretation of passed data in the same way a real terminal would interpret the data … This becomes important when sending input to programs(such as su and sudo) that normally refuse to read input from pipes.

In Ubuntu(and many other Linux distributions), PTYs(slaves are the ones of interest to you here) are pseudo-devices identified under /dev/pts ... These device files are dynamically created by /dev/ptmx(the pseudoterminal multiplexor device) upon user login to a virtual terminal and removed after user logout ... The default owner of those device files are the logged-in user and the tty system group ... This group is used by the write and the wall utilities to enable them to write to other people's virtual terminals, but it is not intended to be used directly.

How it's done

The right way

I wouldn't advise changing permissions at all for security reasons and as these are pseudo-device files and you can mess them up ... And it isn't intended to work that way.

The way to communicate is like so:

  • Ask the other user on /dev/pts/2(this can be found by the command tty) to allow receiving messages from you using the mesg command like so:

    mesg y yourusername
    
  • Start sending messages to that user like so:

    write their_username /dev/pts/2
    
  • Or pipe output to them like so:

    date | write their_username /dev/pts/2
    
  • Or redirect output to them using bash's process substitution like so:

    date >(write their_username /dev/pts/2)
    

Notice: that the user receiving messages must be logged in through terminal not through the X session only ... i.e. :

$ who
ubuntu   :0           2023-01-12 19:48 (:0) <--- Logged-in through X
ubuntu   pts/3        2023-01-27 22:50 <--- Logged-in through terminal

The bad way (in response to your request)

For example, I would like to be able to execute this command : date > /dev/pts/2 Is it possible for the other user to give me the permission using chmod xxx /dev/pts/2 and what would be the value replacing the xxx ?

The information below is just for science and not recommended to try at all:

This is still very bad practice, but for the sake of knowledge ...

The virtual terminal is usually owned by the logged-in user and the tty group(this group is used by utilities like write and wall and has some default members like x and syslog) ... So, the logged-in user can change the ownership and the permissions of the virtual terminal device file without needing sudo ... Therefore, if the other user runs:

chown user:group /dev/pts/2

keeping their owner user and changing only the owner group to the name of the group that you are both members of ... And gives write permission to the group(this is the default so, might not be needed but just in case) like so:

chmod g+w /dev/pts/2

Then you'd be able to redirect output to their terminal e.g. like so:

date > /dev/pts/2

Keep in mind that will, however, grant write permissions to all existing members(and those who might be added to the group meanwhile) of your group and will deny all permission from the original tty group.

This will also be temporary until the user logs out and then the virtual terminal device file should disappear and new virtual terminal device files should be created with the default right permissions upon subsequent user logins.

ReyRey avatar
gf flag
So it's not possible for another user to give me any rights? This is a question from a uni test and I haven't found anything on the internet (as I'm a beginner don't know much about tty files). The question is to know if the other user has the possibility to give me the access to I can redirect the output into his terminal (even if not recommended...)
ReyRey avatar
gf flag
Yes, I have succeeded to redirect the output of a terminal into another terminal that I opened. The only thing I found was on this website : https://josephmuia.ca/2018-05-02-tty-fun/ but I couldn't practically verify
Raffa avatar
jp flag
@ReyRey As I said ... Changing permissions on device files is **bad ,,, bad** practice IMO ... Whatever problem you think it will solve, it will create many problems on the other hand.
ReyRey avatar
gf flag
All right, thank you for your answers!
ReyRey avatar
gf flag
I thought of this, but there's something that I don't get : as the default permissions of the files are rw--w----, two users of the same group should be able to exchange through each other's terminal without any sudo chmod command (which isn't possible).
ReyRey avatar
gf flag
Yeah, I don't really understand why he put this question, because no one should do this but I appreciate your help! Thanks again! :)
sudodus avatar
jp flag
+1; Nice to see old `write` again :-)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.