Fun with Device Files

by David Both

Linux handles almost everything as a file. This has some interesting and amazing implications. “Everything is a file” is possible because all devices are implemented by Linux as these things called device files. Device files are not device drivers, rather they are gateways to devices that are exposed to the user.

Device files are technically known as device special files. All Linux device files are located in the /dev directory.

Let's have some fun with some of these device files. We will perform a fun experiment that illustrates the power and flexibility of the Linux device files.

Most Linux distributions have multiple virtual consoles (vcs), vc1 through vc7, that can be used to login to a local console session with a shell interface. These can be accessed using the key combinations Ctrl-Alt-F1 for console 1, Ctrl-Alt-F2 for console 2, and so on.


In this experiment we will show that simple commands can be used to send data between devices. In this case, we are specifically looking at different console and terminal devices. Perform this experiment as a non-root user on a Linux host or VM that can be used for experimentation – not a production host. For my experiments I created a non-root user, "student".

Press Ctrl-Alt-F2 to switch to console 2. On some distributions, the login information includes the tty (Teletype) device associated with this console, but many do not. It should be tty2 because you are in console 2. You might need to use a different key combination if you are using a local instance of a VM.

Login to console 2 as the student user. Then use the who am i command—yes, just like that, with spaces—to determine which tty device is connected to this console.

          [student@f26vm ~]$ who am i
          student  tty2        2017-10-05 13:12

This command also shows the date and time that the user on the console logged in.  

Before we proceed any further with this experiment, let's look at a listing of the tty2 and tty3 devices in /dev. We do that by using a set [23] so that only those two devices are listed.

          [student@f26vm ~]$ ls -l /dev/tty[23]
          crw--w---- 1 root tty 4, 2 Oct  5 08:50 /dev/tty2
          crw--w---- 1 root tty 4, 3 Oct  5 08:50 /dev/tty3

There are a large number of tty devices defined at boot time but we do not care about most of them for this experiment, just the tty2 and tty3 devices. As device files there is nothing special about them, they are simply character type devices; note the “c” in the first column of the results. We will use these two TTY devices for this experiment. The tty2 device is attached to virtual console 2 and the tty3 device is attached to virtual console 3.

Press Ctrl-Alt-F3 to switch to console 3 and login again as the "student" user. Use the who am i command again to verify that you really are on console 3 and then enter the echo command.

          [student@f26vm ~]$ who am i
          student  tty3        2017-10-05 13:18
          [student@f26vm ~]$ echo "Hello world" > /dev/tty2

Press Ctrl-Alt-F2 to return to console 2. The string "Hello world" (without quotes) should displayed on console 2.

This experiment can also be performed with terminal emulators on the GUI desktop. Terminal sessions on the desktop use pseudo terminal devices in the /dev tree, such as /dev/pts/1, where pts stands for “pseudo terminal session.”

Open at least two terminal sessions on the GUI desktop using Konsole, Tilix, Xterm or your other favorite graphical terminal emulator. You may open several if you wish. Determine which pseudo-terminal device files they are connected to with the who am i command and then choose one pair of terminal emulators to work with for this experiment. Use one to send a message to another with the echo command.

          [student@f26vm ~]$ who am i
          student  pts/9        2017-10-19 13:21 (
          [student@f26vm ~]$ w
          13:23:06 up 14 days,  4:32,  9 users,  load average: 0.03, 0.08, 0.09
          USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
          student  pts/1     05Oct17  4:48m  0.04s  0.04s -bash
          student  pts/2     06Oct17  2:16   2.08s  2.01s screen
          student  pts/3     07Oct17 12days  0.04s  0.00s less
          student  pts/4     07Oct17  2:16   0.10s  0.10s /bin/bash
          root     pts/5     08:35    4:08m  0.05s  0.05s /bin/bash
          root     pts/6     08:35    4:47m  1:19   1:19  htop
          root     pts/7     08:35    4:40m  0.05s  0.05s /bin/bash
          root     pts/8     08:50    4:32m  0.03s  0.03s /bin/bash
          student  pts/9     13:21    0.00s  0.04s  0.00s w
          [student@f26vm ~]$ echo "Hello world" > /dev/pts/4

On my test host, I sent the text “Hello world” from /dev/pts/9 to /dev/pts/4. Your terminal devices will be different from the ones I have used on my test VM. Be sure to use the correct devices for your environment for this experiment.

About the Author

David Both is a Linux and Open Source advocate who resides in Raleigh, North Carolina. He has been in the IT industry for over forty years and taught OS/2 for IBM where he worked for over 20 years. While at IBM, he wrote the first training course for the original IBM PC in 1981. He has taught RHCE classes for Red Hat and has worked at MCI Worldcom, Cisco, and the State of North Carolina. He has been working with Linux and Open Source Software for 20 years. David has written articles for OS/2 Magazine, Linux Magazine, Linux Journal and OpenSource.com. His article "Complete Kickstart," co-authored with a colleague at Cisco, was ranked 9th in the Linux Magazine Top Ten Best System Administration Articles list for 2008. He has spoken at POSSCON and All Things Open (ATO).

This article was contributed by David Both, author of The Linux Philosophy for SysAdmins.