There is one particular concept that makes Linux and Unix especially flexible and powerful.
Everything is a file
Linux handles everything as a file. This has some interesting and amazing implications. This makes it possible to copy an entire hard drive, boot record included, because the entire hard drive is a file, just as are the individual partitions. Printers are files and so are serial ports and USB mass storage devices.
If you remember this as you work with Linux, you will see many ways in which this concept is already used. You will also discern may ways in which you can use it.
Here is a trick question for you. Which of the following are files?
- Shell scripts
- LibreOffice documents
- Serial ports
- Kernel data structures
- Kernel tuning parameters
- Hard drives
- Logical Volumes (LVM)
Perhaps you won’t believe this but to Unix and Linux they are all files and that is one of the most amazing concepts. It makes possible some very simple yet powerful methods for performing many administrative tasks that might otherwise be extremely difficult or impossible.
Back up the Master Boot Record
Consider, for example, the simple task of making a backup of the Master Boot Record (MBR) of your hard drive. I have had, on occasion, needed to restore or recreate my MBR, particularly the partition table. Recreating it from scratch is very difficult. Restoring it from a saved file is easy. Linux comes with a powerful GNU utility, dd, that can perform this and many other functions.
The dd stands for “disk dump” but those of us administrators who have been around for a long time know it as “disk destroyer” because, if you are not very careful, it will do exactly what you tell it to, including destroy all of the data on a hard drive or partition.
The following command will back up your MBR. It must be run as root because non-root users do not have access to the hard drive device files in the /dev directory. BS is Block Size and count is the number of blocks to read from the source file. This command will create a file, myMBR.bak in the /tmp directory. The file will be 512 bytes in size and contain the contents of your MBR including the bootstrap code and partition table.
dd if=/dev/sda of=/tmp/myMBR.bak bs=512 count=1
If the MBR were damaged, it would be necessary to boot to a rescue disk and use the following command which performs essentially the reverse operation of the one above. Notice that it is not necessary to specify the block size and block count as in the first command because the dd command will simply copy the backup file to the first sector of the hard drive and stop when it reaches the end of the source file.
dd if=/tmp/myMBR.bak if=/dev/sda
It is all part of the filesystem
Everything on a Linux computer is accessible as a file in the filesystem space. The whole point of this is to be able to use common tools on different things.
The dd command can be used to copy an entire partition of hard drive to a file or another hard drive as shown below. Here again the dd command copies data to the end of the input device and stops. Be sure that the size of the output device is larger than the input device.
dd if=/dev/sdf2 of=/dev/sdg3
dd if=/dev/sda of=/dev/sdg
Other filesystem tools work, too. The cat command for example, can be used to send the content of any file to Standard Output. This includes partitions and entire hard drives. Then the output can be redirected to a file.
cat /dev/sda1 > partition1.backup
The cat command, however, does not have the control that the dd command does. For example, one cannot specify the amount of data to be read from the source device or file.
Here is an interesting experiment that will demonstrate the fact that everything is a file. Most Linux distributions have multiple virtual consoles, 1 through 7, 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.
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.
Login as a non-root user. Then you can use the who am i command – yes, just like that, with spaces – to determine which tty device is connected to this console.
Before we actually perform this experiment, look at a listing of the tty2 and tty3 devices in /dev.
ls -l /dev tty
There will be a large number of tty devices defined but we do not care about most of them, just the tty2 and tty3 devices. As device files, there is nothing special about them; they are simply character type devices. We will use these 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. Login again as the same non-root user.
Now enter the following command on console 3.
echo “Hello world” > /dev/tty2
Press Ctrl-Alt-F3 to return to console 2. The string “Hello world” (without quotes) is displayed in 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. Open two terminal sessions using Konsole or Xterm. Determine which pseudo-terminals they are connected to and use one to send a message to the other.
Now continue the experiment by using the cat command to display the /etc/fstab file on a different terminal.
Another interesting experiment is to print a file directly to the printer using the cat command. Assuming that your printer device is /dev/usb/lp0, and that your printer can print PDF files directly, the following command will print a PDF file on your printer.
cat test.pdf > /dev/usb/lp0
The dd command can also be used to print a printer ready file. I think the cat command is actually better suited to this task, though.
Implications of Everything is a File
The implications of “everything is a file” are far-reaching and much greater than can be listed in a short article such as this one. You have already seen some examples in the preceding experiments. But here is a short list that encompasses those and more.
- Clone hard drives.
- Back up partitions.
- Back up the master boot record (MBR).
- Install ISO images onto USB thumb drives.
- Communicate with users on other terminals.
- Print files to a printer.
- Change the contents of certain files in the /proc pseudo filesystem to modify configuration parameters of the running kernel.
- Overwrite files, partitions or entire hard drives with random data or zeros.
- Redirect unwanted output from commands to a null device where it disappears forever.
- etc., etc., etc.
There are so many possibilities here that any list can really only scratch the surface. I am sure that you have – or will – figure out many ways to use this feature of Linux far more creatively than I have mentioned here. I would be interested to see your comments on how you use “Everything is a file.”
For more information on the /dev/ directory and the devices that you may find there, refer to this article at The Linux Journal. For more specific information on individual devices, this article and this article at the Linux Documentation Project are helpful.