Ubuntu: How to check which tty am I using



Question:

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 ?


Solution:1

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  


Solution:2

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  


Solution:3

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+F1 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+F1 to bring up a tty.

  1. Boot the laptop, and right after the computer manufacturer's splash screen disappears, hold down the left Shift key.

  2. From the first purple GNU GRUB screen select Advanced options for Ubuntu 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 menu:

    enter image description here

  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.


Solution:4

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.


Solution: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.


Solution:6

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »