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 ?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
There are several ways to find this out. I would go with the
who am i (not
$ 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
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
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.
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.
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.
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.
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:
Using the arrow keys scroll down to root and then press Enter.
You should now see a root prompt, something like this:
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.
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.
$ 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.