How I create Linux device files — and why

0

First, what the heck is a Linux device file — and why should I care?

Linux handles almost everything as a file. This has some interesting, powerful, and amazing implications. This concept 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. “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

Device files are technically known as device special files. Device files are employed to provide the operating system and the users with an interface to the devices that they represent. All Linux device files are located in the /dev directory, which is an integral part of the root (/) filesystem because they must be available to the operating system during early stages of the boot process – before other filesystems are mounted.

CentOS and RHEL, as well as all versions of Fedora going back to at least as far Fedora 15, use the newer method of creating the device files. All required device files are created at boot time. This functionality is possible because the udev device manager detects addition and removal of devices as they occur. This allows for true dynamic plug-n-play functionality while the host is up and running. It also performs the same task at boot time by detecting all devices installed on the system very early in the boot process. Linux.com has a good description of udev and Seth Kenlon has posted an article here on Both.org that shows you How to Use udev.

List the files in /dev, notice the date and time on the files. All of them were created during the last boot. You can verify this using the uptime or last commands.

Creating a device file

In the past, the device files in /dev were all created at installation time, resulting in a directory full of almost every possible device file, even though most would never be used. In the unlikely event that a new device file was needed or one was accidentally deleted and needed to be re-created, the mknod program was available to manually create device files. All you had to know was the device major and minor numbers. I needed to do just that for the first and only time ever, a few years ago. The need doesn’t arise very often.

Somehow I managed to delete the default printer device, lp0, so couldn’t print to my laser printer. I needed to recreate that printer without a reboot as I was in the middle of a project. Normally, Linux creates four parallel printer devices at boot time.

# ll /dev/lp*
crw-rw----. 1 root lp 6, 0 Sep 25 10:59 lp0
crw-rw----. 1 root lp 6, 1 Sep 25 10:59 lp1
crw-rw----. 1 root lp 6, 2 Sep 25 10:59 lp2
crw-rw----. 1 root lp 6, 3 Sep 25 10:59 lp3

But this is what I saw.

# ll /dev/lp*
crw-rw----. 1 root lp 6, 1 Sep 25 10:59 lp1
crw-rw----. 1 root lp 6, 2 Sep 25 10:59 lp2
crw-rw----. 1 root lp 6, 3 Sep 25 10:59 lp3

The first thing I did was refer to the Linux Allocated Devices, at the Kernel.org web site. A quick search revealed the information I needed to create the device.

   6 char       Parallel printer devices
0 = /dev/lp0 Parallel printer on parport0
1 = /dev/lp1 Parallel printer on parport1
...

Current Linux kernels no longer have a fixed mapping
between parallel ports and I/O addresses. Instead,
they are redirected through the parport multiplex layer.

The major code for parallel printer devices is 6, and the the minor code is 0 for lpr0. It was already clear to me that these were the codes I needed, but I always check the list of devices at kernel.org to be sure.

The following command created the missing device file. I specify the fully qualified name of the device file, and c defines it as a character file. The 6 and 0 are the major and minor codes.

# # mknod /dev/lp0 c 6 0
root@testvm1:~# ll /dev/lp*
crw-r--r--. 1 root root 6, 0 Sep 25 15:34 /dev/lp0
crw-rw----. 1 root lp   6, 1 Sep 25 10:59 /dev/lp1
crw-rw----. 1 root lp   6, 2 Sep 25 10:59 /dev/lp2
crw-rw----. 1 root lp   6, 3 Sep 25 10:59 /dev/lp3

I then changed the group ownership to lp and the permissions to 660.

# chgrp lp /dev/lp0 ; chmod 660 /dev/lp0 
# ll /dev/lp*
crw-rw----. 1 root lp 6, 0 Sep 25 16:03 lp0
crw-rw----. 1 root lp 6, 1 Sep 25 10:59 lp1
crw-rw----. 1 root lp 6, 2 Sep 25 10:59 lp2
crw-rw----. 1 root lp 6, 3 Sep 25 10:59 lp3

You can practice creating device files in the /tmp directory as that’s a safe place to do so. The files in /dev can be deleted by root just as easily as other files, so it pays to be careful.

Conclusion

Even now, a situation caused by a missing device file can still happen and knowing how to deal with it can be important.

I haven’t covered many of the myriad different types of device files that you might encounter. That information is available in plenty of detail in the resources cited. I hope I have given you some basic understanding of how to work with device files. I encourage you to explore more on your own. explore more on your own.

Additional Resources


Leave a Reply