I opened quite a few putty sessions to my remote Ubuntu machine.

For each of the session, I got a /dev/pts/x file assigned to it. Like below:

crw--w---- 1 xxx tty  136, 0 Feb   5 23:08 0
crw--w---- 1 xxx tty  136, 1 Feb   5 23:23 1
crw--w---- 1 xxx tty  136, 2 Feb   5 16:10 2
crw--w---- 1 xxx tty  136, 3 Feb   5 23:20 3
crw--w---- 1 xxx tty  136, 4 Feb   5 23:21 4
crw--w---- 1 xxx tty  136, 5 Feb   5 23:21 5
crw--w---- 1 xxx tty  136, 6 Feb   5 23:25 6
c--------- 1 root root   5, 2 Feb   4 10:28 ptmx

So how can I tell which putty session is using which pts file?



1 Answer 1


The tty command will provide the device associated with the current session:


If there is no current terminal device, tty will report an error and exit with a non-zero status value

not a tty

This allows you to write code that acts differently depending on whether or not it's attached to a terminal:

if tty >/dev/null
    # This is attached to a terminal device
    # This is not
  • Ah... thanks! tty command does the trick. I am new to Linux so I am not aware of this command yet. who command cannot because it lists all the same login name. Thanks! Feb 5 at 15:36
  • why not use tty -s (or --silent , --quiet ) instead of redirecting to /dev/null doesn't all tty suport it ?
    – Archemar
    Feb 5 at 15:56
  • @Archemar I wanted to give a POSIX solution, and the POSIX version of tty takes no options
    – roaima
    Feb 5 at 17:58
  • tty reports the terminal device open on its stdin. That's not the same as checking whether your process belong to a session attached to a terminal (though in practice would be good enough in most cases). ps -o tty= -p "$$" would report the sessions' tty if any (or ? otherwise) Feb 5 at 18:46

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.