50

There are 7 terminals that can be used in our system. tty7 is GUI based.

How can I check which tty I'm currently using?

Josh Correia
  • 135
  • 7
luv.preet
  • 5,787

8 Answers8

51

There are several ways to find this out. I would go with the who am i (not who) command:

$ who am i
ravexina    tty2        2017-04-10 11:19 (:0)

When I'm in a graphical terminal, it would return something like:

ravexina    pts/0        2017-04-10 1:19 (:0)

Under ssh session using a graphical terminal result is the same, but instead of (:0), I will get my machine IP (192.168.x.x). other commands like w, who, pgrep, ps, etc are helpful too.

For example to find out at which tty my graphical interface is running:

ps $(pgrep Xorg)

Which in my case, because I don't use any display manager like gdm or slim it will produce:

 PID  TTY      STAT   TIME COMMAND
 1693 tty1     Sl     0:05 /usr/lib/xorg/Xorg
Ravexina
  • 55,668
  • 25
  • 164
  • 183
  • 8
    +1, although in mate-terminal who am i returns nothing, and whoami returns zanna, (while who returns all sessions (I remember a related question (that proved unanswerable) where we noticed that different emulators produced different output from who)). – Zanna Apr 09 '17 at 21:08
  • graphical will always use tty7, as that is the only 1 which provides graphical support – luv.preet Apr 09 '17 at 21:14
  • @luv.preet Nope, for example, my GUI is running under tty1 ;) – Ravexina Apr 09 '17 at 21:15
  • can you provide some source where I can visulaize this concept of tty and pts more clearly ? I think I have confused myself. – luv.preet Apr 09 '17 at 21:28
  • 1
    @luv.preet that's out of the scope of your question but see Difference between pts and tty :) - the post of which this one is a duplicate has an awesome answer – Zanna Apr 09 '17 at 21:34
  • @luv.preet All ttys provide "graphical support" as in "it's not their job", and only terminals 0 through 6 have a getty (login prompt) run on them by default. Recent desktops all use (KMS+)DRM-based graphical output, which has zero link with terminals ; Xorg, Wayland and the like treats them as seats only for the sake of paradigm consistency. Running graphical on tty7+ is only a convention. – ElementW Apr 10 '17 at 02:24
  • 1
    also who mom likes – Tejas Kale Apr 10 '17 at 19:56
  • who am i also don't work on rxvt-unicode :(. – pevik Jul 31 '20 at 19:02
  • In my case ps $(pgrep X) is needed (X instead of Xorg). – pevik Jul 31 '20 at 19:04
  • This just seems more convoluted than necessary. The tty command seems to be all you really need. And the who am i doesn't work at all for me. – Seamus Oct 13 '22 at 07:50
35

Use tty command.

If I am right this is the reference:

/dev/tty1 - tty1
/dev/tty2 - tty2
/dev/tty3 - tty3
/dev/tty4 - tty4
/dev/tty5 - tty5
/dev/tty6 - tty6
12

Let's take a step back and start with the confusion: what is a tty? Originally tty was an abbreviation of teletype which became glass teletype which became any terminal or the connection to one, specifically a serial port (These are still around, most commonly /dev/ttyS0, /dev/ttyUSB0 and sometimes /dev/ttyacm0). Then you got pseudo ttys and virtual ttys. old unix hands are familiar with pseudo ttys these are used for remote terminals (ssh and telnet), virtual terminals (xterm and the like), and terminal multiplexers (screen and tmux). Pseudo ttys have had various naming schemes over time but the current one uses names like /dev/pts/0. Virtual ttys on the other hand are associated with virtual consoles, specifically when you use control alt function keys you are switching virtual consoles each of which is paired with a virtual tty. The most famous virtual tty is /dev/tty7 which often used for graphical sessions (because the first six are usually text logins) there is nothing special about tty7 as you can run X servers on any virtual console (although running one on /dev/tty1 is really hard as the kernel opens it for boot logging, but I have done it) and you can do text logons on any virtual tty including tty7 (although init configuration is required and systemd's dynamic allocation makes things interesting and switching to tty13 and up may need a change to the keyboard map).

So if you need to find your pseudo tty (or virtual tty when using text mode) there are all sorts of solutions like who am i or ps ax|grep $$ this is relatively easy to do as there are multiple ways to find out this as the pseudo tty is opened on standard io in most cases and there are other methods that work even when io is redirected. this also has the advantage of also doing the right thing when using serial ports and virtual ttys as long as you are not using graphical terminal emulators or terminal multiplexers.

But what if you are wanting to identify the virtual console? sudo fgconsole may be useful for interactive cases. Or what I do is just walk the consoles as switching is typically quick enough. I am not aware of a programmatic method that works reliably in noninteractive or nonforeground cases.

Let's look at an example that is guaranteed to not be able to reliably answer the virtual console ever. The computer I am sitting in front of (alpha) has an Xserver running on tty2(:1), I have three windows open two xterms and a vncviewer (connected to bravo:2). One xterm is running screen, the other is running ssh charley. Screen has two windows open, ssh delta and who (who correctly identifies the pseudo tty in column two and provides the hint (:1:S0) in column five that tells us that the pseudo tty is connected to the first screen session which is connected to the second X server, but does not give any virtual console information). Running who over either ssh session gives the hint (alpha) which identifies the machine that the ssh session came from which is comparatively good for remote sessions. Although there is little information about what is connected to the pseudo tty (and absolutely no virtual console or even remote tty information is available) at least the remote host is identified. the information about screen can be wrong as screen sessions are detachable, and vnc provides no useful information at all as the only difference between a vnc session and a local session is the display number. In many cases you can assume that :0 is on tty7 but not always, and never on mine as bravo does not have a graphics card at all and :0 is a vnc session (which confuses who because it thinks it is the local console) and none of the machines use tty7 as a graphical session, because I (not just to be contrary) dump logs to tty7 (and tty5, tty6, and tty8 also) religating graphical sessions elsewhere. But wait screen can also act as a terminal program allowing you to establish a session over a serial cable. What does such a session report when who is run? Why naught but the serial port whether it be through vnc, xterm, ssh, screen and a usb serial adapter all at once or is connected to my televideo.

hildred
  • 842
7

When you have logged in to a tty it shows which tty you are using on the first line automatically.


If pressing the keyboard combination Ctrl+Alt+F3 shows only a black screen, as you wrote in a comment, there is an alternative way of getting access to a root shell when booting the computer without pressing Ctrl+Alt+F3 to bring up a tty.

  1. Immediately after the motherboard / computer manufacturer logo splash screen appears when the computer is booting, with BIOS, quickly press and hold the Shift key, which will bring up the GNU GRUB menu. (If you see the Ubuntu logo, you've missed the point where you can enter the GRUB menu.) With UEFI press (perhaps several times) the Esc key to get to the GRUB menu. Sometimes the manufacturer's splash screen is a part of the Windows bootloader, so when you power up the machine it goes straight to the GRUB screen, and then pressing Shift is unnecessary.

  2. From the first purple GNU GRUB screen select Advanced options for Ubuntu with the ↑ and ↓ keys and press Enter. You will then be prompted by another purple GNU GRUB menu that has a list of kernel options in it.

    GNU GRUB menu

  3. Press the down arrow key until you select the second entry from the top (the one with the recovery mode in the description) and then press Enter.

    Now you should see this recovery menu:

    recovery menu

  4. Using the arrow keys scroll down to root and then press Enter.

    You should now see a root prompt, something like this:

     root@ubuntu:~#
    
  5. At this stage you should have a read-only filesystem. You have to remount it with write permissions:

     mount -o rw,remount /  
    

    Now you can run commands the same as in a terminal.

karel
  • 114,770
  • does this always open tty1 ? – luv.preet Apr 09 '17 at 21:30
  • No, it does not open any tty. Instead it mounts the root filesystem of the Ubuntu you installed on your hard drive as an interactive shell with write permissions, enabling you to run commands on it. – karel Apr 09 '17 at 21:32
6

I guess you're looking after something else than the other answers provided. Run sudo fgconsole to find out which real tty is now active.

  • @karel It's easy to imagine a situation where you miss the current TTY announcement. For instance your bash profile clears the screen. I offered a command that can be used anytime, not only immediately after login. WRT Ctrl+Alt+F1 not working - there's a quick workaround: use chvt or openvt. – marian adam Apr 09 '17 at 22:45
  • And BTW, run it with sudo. That's why you got the error. – marian adam Apr 10 '17 at 14:53
5

Use command tty, it works on Linux and macOS and give a pretty simple output to read, only the name of the tty you are in.

Example:

$ tty
/dev/pts/0

It's easy to script this or to include it in the prompt, example:

$ PS1='`tty`: '
/dev/pts/0: _

This way you'll always know which terminal you are in.

Andre
  • 161
  • 3
2

Except for the GUI, in which case you get a pts, this also shows the desired output:

ls -l `tty` | awk '{print $10}'

which is equivalent to previous answers:

tty
Ferroao
  • 850
2

The simplest way:

readlink /proc/self/fd/0
Costa
  • 121
  • though, not the most reliable. It won't work after running bash command exec < /dev/null – darw Aug 22 '22 at 18:38