An introduction to Linux device files

By | 01/10/2012

Everything in Linux is a file. This is a common phrase related to Linux. As and when one gradually learns Linux, he/she gets to understand the fundamentals behind this phrase. In Linux even hardware devices are recognized as files. These are known as Linux device files or special files. Here in this article, we shall be compass the way Linux deals with Device files.

Linux Device files

For Linux, devices can be termed as special files that represent physical or virtual devices attached to Linux system. These special files are used by the Linux operating system to enable access to the physical or virtual devices. This was done in order to make these device accessible using standard I/O operations. Every such device would need to have its specific device driver, which is a Linux kernel module, leveraging the users and the user space programs to interact with the devices.

Here is an abstract illustration:


As can be seen in the figure, the actual hardware is recognized as a file or node at the OS level. The user space applications access the device driver of the device node or device file through standard system calls.

In Linux, every device needs to have device driver so that it can be made accessible by the OS to the users. The device driver actually provides the standard input/output calls to access the device and interact with it. Worth mentioning, all the device drivers are kernel modules in Linux.

All the devices on the Linux can be viewed through:

cat /proc/devices

All the devices will be listed, for example:

Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
5 ttyprintk
6 lp
7 vcs
10 misc
13 input
21 sg
29 fb
99 ppdev
108 ppp
116 alsa
128 ptm
136 pts
180 usb
189 usb_device
226 drm
251 hidraw
252 usbmon
253 bsg
254 rtc

Block devices:
1 ramdisk
2 fd
259 blkext
7 loop
8 sd
9 md
11 sr
65 sd
66 sd
67 sd
68 sd
69 sd
70 sd
71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
252 device-mapper
253 pktcdvd
254 mdp

We see quite a lot familiar devices here, like, memory, ramdisk, usb, etc. However, apart from the external physical devices, Linux also recognises virtual devices which can be created given a memory range, and accessed using its device drivers through the I/O methods. For example, note the device ttyl, which is the terminal. Physically, a Linux terminal is not a device, but it is treated as a device, and hence listed out here.

Hence, Linux device drivers provide the capability to create and use virtual devices also on Linux.

One can also find all the device nodes created at:

ls /dev/

Types of devices

There are generally three types of devices in Linux:
1. Character Devices
2. Block devices
3. Network Devices

The segregation is majorly in the way these devices interact and perform I/O with the operating system. Hence, for all these three types of devices, the device drivers are also classified with these three types. Therefore, a character device will have a character device driver module associated with it, and similarly for block devices, block module and network module for network devices. Lets comprehend each of the type:

1. Character Devices

Character Devices are devices where data is a stream of bytes i.e. the input/output happens byte by byte. Therefore, the character device drivers would be in a such a way to implement such kind of streamed data. The interactive operations (often called the file operations) which would be most vital to implement are:
– open()
– read()
– write()
– close()

Examples: A terminal, Keyboard, etc

2. Block Devices

Block Devices are devices, as the name suggests, deal with blocks of data. Therefore, block device drivers are implemented to read/write block data and also supports random access data. However, since data is to be handled in blocks, therefore its implementation involves an extra intermediate buffer to store data between reads and writes. It has block device operations such as
– open()
– release()
– direct_access()
– revalidate_disk()

Examples: A floppy, hard disks

3. Network devices

Network device are the ones which are used to transmit data from one machine to another. The network device drivers implementation is entirely different to those of char devices and block devices. Here, they are exchange data in the form of data packets with another remote machine, using a standard protocol.They also deal with setting up ip addresses, configurations and modifying transmission parameters, traffic, etc.
Their I/O operations are specific to the data packet transmission protocol, as in, TCP/UDP.

Examples: NIC

Some interesting facts related to Linux device files

  • One can access the device filesystem through /dev/
  • Note from the /proc/devices output, even the Linux is aware of what all are character devices and what all are block devices. Network device are completely different class.
  • One device driver can cater to various devices depending upon the functionality, like a device driver for a xerox/scanner machine would implement for xerox as well as scanner.

One thought on “An introduction to Linux device files

  1. Ivan

    NIce post, man :)! Thank you so much!!!

    Have you got any other technical materials that you would recommend for linux administrators (medium level) just like me?

    Ivan Leon (Brazil – Guaĩba/RS)


Leave a Reply

Your email address will not be published. Required fields are marked *