21

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

How can I know that which tty am I currently using ?

like, tty1, tty2....tty6 ?

30

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
  • 4
    +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 9 '17 at 21:08
  • graphical will always use tty7, as that is the only 1 which provides graphical support – luv.preet Apr 9 '17 at 21:14
  • 1
    @Zanna I encounter to this situation myself, but I think it worth to mention. – Ravexina Apr 9 '17 at 21:16
  • 1
    anyway, who am i will always work nicely in a vt, and the pgrep is very smart (you might want to use $() syntax for command sub - backticks can get a bit confusing in markdown) so your answer is very nice and whoever downvoted it should not have done so... – Zanna Apr 9 '17 at 21:24
  • 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 9 '17 at 21:34
14

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
  • it is showinng pts/18. – luv.preet Apr 9 '17 at 20:54
  • this pts might be running in a tty, isn't it? How can I know that ? – luv.preet Apr 9 '17 at 20:55
  • 5
    @luv.preet that is your terminal. See "Difference between pts and tty". – Chai T. Rex Apr 9 '17 at 20:56
  • 4
    pts means you are using terminal from GUI or through SSH – lithium_js Apr 9 '17 at 20:56
  • 3
    You'll need to ask a new question for that. – Chai T. Rex Apr 9 '17 at 20:58
5

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.

5

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 BIOS/UEFI splash screen during boot, 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 menu that has a list of kernel options in it.

  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 twice.

    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.

  • does this always open tty1 ? – luv.preet Apr 9 '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 9 '17 at 21:32
2

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 9 '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
1

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.

0

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

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

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.