Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

tty

Print the name of the terminal device you are using (like /dev/tty3 or /dev/pts/0).

Level
Everyday Linux
Group
Shell and help
Needs root
No
Package
coreutils

What it does

Every session has a terminal device: a real console (/dev/tty1 … /dev/tty6) or a pseudo terminal for terminal windows and SSH (/dev/pts/0, /dev/pts/1 …).

tty prints the name of yours. When the input is not a terminal (for example in a cron job or a pipe), it prints "not a tty" and returns an error code — scripts use this to know if a person is watching.

How to write it

tty [OPTION]...

Options 3

Most used

-s, --silent, --quiet
Print nothing; only return the exit code (0 = terminal, 1 = not a terminal).
--help
Show help.
--version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Which terminal am I on?

    tty
    /dev/pts/0

    pts = terminal window or SSH session.

  2. 2On the local text console

    tty
    /dev/tty3

    A real virtual console (Ctrl+Alt+F3).

  3. 3Check in a script if a person is watching

    if tty -s; then echo "interactive"; else echo "batch (cron, pipe)"; fi

    Ask questions only when a person can answer.

  4. 4In a pipe there is no terminal

    echo | tty
    not a tty

    stdin comes from the pipe, not from a terminal.

  5. 5Send a message to another session

    who
    write admin pts/1

    write and wall use the terminal names that tty and who show.

  6. 6End another session

    pkill -KILL -t pts/3

    Logs out the session on pts/3 (find it with who).

  7. 7Set terminal options for your session

    stty -a

    Shows the settings of the current tty (like the erase key).

  8. 8Log the terminal in a script

    echo "$(date) started on $(tty) by $USER" >> /var/tmp/run.log

    Helps to see later how and where a script was run.

  9. 9See the owner of your terminal device

    ls -l $(tty)
    crw--w----. 1 admin tty 136, 0 Oct 25 09:02 /dev/pts/0

    Your terminal belongs to you; group tty may write messages to it.

  10. 10Find processes attached to a terminal

    ps -t pts/0

    All processes of that session.

  11. 11Check that SSH gave you a terminal

    ssh server2 tty; ssh -t server2 tty

    Without -t a remote command has no terminal ("not a tty").

Tips to remember

  • /dev/ttyN = console, /dev/pts/N = window or SSH. tty -s = test in scripts.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man tty