Home » Linux: Difference between /dev/console , /dev/tty and /dev/tty0

Linux: Difference between /dev/console , /dev/tty and /dev/tty0

Solutons:


From the Linux Kernel documentation on Kernel.org:

/dev/tty        Current TTY device
/dev/console    System console
/dev/tty0       Current virtual console

In the good old days /dev/console was System Administrator console. And TTYs were users’ serial devices attached to a server.

Now /dev/console and /dev/tty0 represent current display and usually are the same. You can override it for example by adding console=ttyS0 to grub.conf. After that your /dev/tty0 is a monitor and /dev/console is /dev/ttyS0.

An exercise to show the difference between /dev/tty and /dev/tty0:

Switch to the 2nd console by pressing Ctrl+Alt+F2. Login as root. Type sleep 5; echo tty0 > /dev/tty0. Press Enter and switch to the 3rd console by pressing Alt+F3.
Now switch back to the 2nd console by pressing Alt+F2. Type sleep 5; echo tty > /dev/tty, press Enter and switch to the 3rd console.

You can see that tty is the console where process starts, and tty0 is a always current console.

  • /dev/console is a virtual set of devices which can be set as a parameter at boot time. It might be redirected to a serial device or a virtual console and by default points to /dev/tty0. When multiple console= options are passed to the kernel, the console output will go to more than one device;

  • /dev/tty[0-N] (N is the highest attributed TTY number, e.g. 63) is one of the virtual consoles you switch to with controlaltF1 and so on;

  • /dev/tty0 is also by default virtual console;

  • /dev/tty is kind of an alias to the console (physical, virtual or pseudo device, if any) associated to the process that open it. Unlike the other devices, you do not need root privileges to write to it. Note also that processes like the ones launched by cron and similar batch processes have no usable /dev/tty, as they aren’t associated with any. These processes have a ? in the TTY column of ps -ef output.

/dev/console

https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/serial-console.rst

On Linux, the kernel console can be configured using the console= boot option. Kernel code which calls printk() may write messages to it, e.g. when a device is loaded or an error occurs. These messages are also buffered by the kernel. (See also dmesg). When a console device is found and started, it receives all the previously buffered messages.

You can pass console= multiple times to configure multiple consoles, and messages will be written to all of them. Apparently you can only select one console of each “type”: you can’t use both console=ttyS0 and console=ttyS1.

The kernel documentation specifies /dev/console as a character device numbered (5,1). Opening this character device opens the “main” console, which is the last tty in the list of consoles. The first non-kernel process, called init or “PID 1”, is started with /dev/console connected to standard output, standard error, and standard input.

If none of the consoles are a tty, then opening /dev/console returns the error ENODEV (“No such device”). The kernel will print log a message, and start init regardless. For an example of a kernel console which is not a tty device, see netconsole, or my favourite console the line printer.

You can also see a list of tty consoles by reading /sys/class/tty/console/active. systemd documentation points out that the first device shown is the main console. The list is actually in reverse order of the kernel command line. The current kernel documentation incorrectly states that the last device shown is the main or “active” console. For some reason it is possible to poll this file for changes (in case console devices are removed?).

Inside a systemd-nspawn container, the standard /dev/console file is replaced with a pseudo-terminal device (PTY). These would be best described as virtual terminal devices. They are created dynamically and are also used to implement graphical terminal emulators like GNOME Terminal, and for remote access like ssh.

/dev/tty0

The Linux TTY device nodes tty1 through tty63 are virtual terminals. They are also referred to as VTs, or as virtual consoles. They simulate multiple consoles on top of the physical console device driver. Only one virtual console is shown and controlled at a time. The active terminal can be switched, e.g. using chvt, or Ctrl+Alt+F1 through however many function keys you have.

You can also read and write to the current VT using /dev/tty0. tty0 is the usual kernel console, e.g. if you did not select one explicitly. “The system first looks for a VGA card [which is what VTs run on] and then for a serial port”. You can also set the console to a specific VT, e.g. console=tty1.

“If you don’t have a VGA card in your system, the first serial port will automatically become the console.” A “serial console” like ttyS0 is probably the most common alternative to tty0. It is not possible to use the VT system on top of a serial console.

/dev/tty

/dev/tty is one of the three standard device files specified by POSIX (/dev/ is one of the three directory names specified by POSIX). Opening it is equivalent to opening the controlling terminal of the current process. The controlling terminal is set when a process first opens a terminal, at least on Linux. For example, in init, it would refer to /dev/console.

Detaching from the controlling terminal is one of the steps traditionally required to start a background process, for example a system logging daemon. The steps to become a background process are horribly intricate, but to be specific, the step which detaches from the controlling terminal is the setsid system call. In more modern systems, the init system e.g. systemd starts the service without any controlling terminal in the first place.

Related Solutions

How to download package not install it with apt-get command?

Use --download-only: sudo apt-get install --download-only pppoe This will download pppoe and any dependencies you need, and place them in /var/cache/apt/archives. That way a subsequent apt-get install pppoe will be able to complete without any extra downloads....

What defines the maximum size for a command single argument?

Answers Definitely not a bug. The parameter which defines the maximum size for one argument is MAX_ARG_STRLEN. There is no documentation for this parameter other than the comments in binfmts.h: /* * These are the maximum length and maximum number of strings...

Bulk rename, change prefix

I'd say the simplest it to just use the rename command which is common on many Linux distributions. There are two common versions of this command so check its man page to find which one you have: ## rename from Perl (common in Debian systems -- Ubuntu, Mint,...

Output from ls has newlines but displays on a single line. Why?

When you pipe the output, ls acts differently. This fact is hidden away in the info documentation: If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is...

mv: Move file only if destination does not exist

mv -vn file1 file2. This command will do what you want. You can skip -v if you want. -v makes it verbose - mv will tell you that it moved file if it moves it(useful, since there is possibility that file will not be moved) -n moves only if file2 does not exist....

Is it possible to store and query JSON in SQLite?

SQLite 3.9 introduced a new extension (JSON1) that allows you to easily work with JSON data . Also, it introduced support for indexes on expressions, which (in my understanding) should allow you to define indexes on your JSON data as well. PostgreSQL has some...

Combining tail && journalctl

You could use: journalctl -u service-name -f -f, --follow Show only the most recent journal entries, and continuously print new entries as they are appended to the journal. Here I've added "service-name" to distinguish this answer from others; you substitute...

how can shellshock be exploited over SSH?

One example where this can be exploited is on servers with an authorized_keys forced command. When adding an entry to ~/.ssh/authorized_keys, you can prefix the line with command="foo" to force foo to be run any time that ssh public key is used. With this...

Why doesn’t the tilde (~) expand inside double quotes?

The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal. POSIX defines Double-Quotes as: Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the...

What is GNU Info for?

GNU Info was designed to offer documentation that was comprehensive, hyperlinked, and possible to output to multiple formats. Man pages were available, and they were great at providing printed output. However, they were designed such that each man page had a...

Set systemd service to execute after fstab mount

a CIFS network location is mounted via /etc/fstab to /mnt/ on boot-up. No, it is not. Get this right, and the rest falls into place naturally. The mount is handled by a (generated) systemd mount unit that will be named something like mnt-wibble.mount. You can...

Merge two video clips into one, placing them next to each other

To be honest, using the accepted answer resulted in a lot of dropped frames for me. However, using the hstack filter_complex produced perfectly fluid output: ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack output.mp4 ffmpeg -i input1.mp4 -i input2.mp4...

How portable are /dev/stdin, /dev/stdout and /dev/stderr?

It's been available on Linux back into its prehistory. It is not POSIX, although many actual shells (including AT&T ksh and bash) will simulate it if it's not present in the OS; note that this simulation only works at the shell level (i.e. redirection or...

How can I increase the number of inodes in an ext4 filesystem?

It seems that you have a lot more files than normal expectation. I don't know whether there is a solution to change the inode table size dynamically. I'm afraid that you need to back-up your data, and create new filesystem, and restore your data. To create new...

Why doesn’t cp have a progress bar like wget?

The tradition in unix tools is to display messages only if something goes wrong. I think this is both for design and practical reasons. The design is intended to make it obvious when something goes wrong: you get an error message, and it's not drowned in...

OpenSSH: How to end a match block

To end up a match block with openssh 6.5p1 or above, use the line: Match all Here is a piece of code, taken from my /etc/ssh/sshd_config file: # Change to no to disable tunnelled clear text passwords PasswordAuthentication no Match host 192.168.1.12...

Redirecting the content of a file to the command “echo”

You can redirect all you want to echo but it won't do anything with it. echo doesn't read its standard input. All it does is write to standard output its arguments separated by a space character and terminated by a newline character (and with some echo...