Understanding Devices in Linux: What Hides Behind /dev and /sys?

Understanding Devices in Linux: What Hides Behind /dev and /sys?

By SysOpsMaster | Level Up: Linux & Ops | 10 Feb 2025


Many Linux users encounter the /dev and /sys directories but may not fully understand their roles in the system. However, when working with disks, partitions, terminals (tty, pty), and other devices, it is useful to grasp their essence.

What is /dev?

The /dev directory contains special files that allow the operating system to interact with both physical devices (such as hard drives, cameras, printers) and virtual ones (such as /null, /random, /tty).

These files are not drivers themselves but serve as an interface between the kernel and hardware. When the system accesses them, the kernel uses appropriate drivers to process requests.

Types of Devices in /dev

When listing the contents of the /dev directory, you will see files with different prefixes:

$ ls -l /dev
brw-rw---- 1 root disk 8, 1 Feb 10 10:15 sdb1
crw-rw-rw- 1 root root 1, 3 Feb 10 10:15 random
prw-r--r-- 1 root root 0 Feb 10 10:15 pipe_data
srw-rw-rw- 1 root root 0 Feb 10 10:15 sock_comm

Notice the first character in each line:

  • b — block device
  • c — character device
  • p — named pipe
  • s — socket

Let's take a closer look at them.

1. Block Devices

Block devices transfer data in fixed-size blocks. An example is a hard drive or a USB stick.

Examples:

  • Hard drives (HDD, SSD)
  • Flash drives
  • SD cards

2. Character Devices

Character devices transfer data character-by-character or in small chunks. Unlike block devices, they do not use file systems but operate with data streams.

Examples:

  • Keyboard (/dev/input/eventX)
  • Mouse (/dev/input/mice)
  • Serial ports (/dev/ttyS0)
  • Null device (/dev/null)

When data is written to /dev/null, it simply disappears. This is useful for discarding unwanted program output.

3. Named Pipes

Named pipes (FIFOs) are similar to the | operator but allow processes to communicate via a file.

Example: /dev/fifo_pipe allows processes to exchange data through a named pipe.

4. Sockets

Sockets are used for interprocess communication both within the system and over a network. They allow programs to exchange data without using files.

Examples:

  • /dev/log — used for logging messages
  • Unix sockets such as /dev/socket/my_app allow process communication without network involvement

Major and Minor Device Numbers

Each device in /dev has two numbers:

8, 1 Feb 10 10:15 sdb1
  • Major (8) — identifies the device driver
  •  
  • Minor (1) — unique identifier within the driver
  • For example, a disk partition will have different minor numbers but the same major number since it uses the same driver.

    How Are /dev Files Created?

    Files in /dev are not permanent — they are created at boot time or when new devices are connected. They are managed by udevd or mdevd, which:

    • Detect new devices
    • Assign appropriate drivers
    • Create corresponding files in /dev

    What is Hidden in /sys?

    /sys is a virtual sysfs file system that provides information about system configuration and hardware. Unlike /dev, /sys represents device structures and properties.

    Examples of useful files in /sys:

    • /sys/class/net/eth0/address — MAC address of a network interface
    • /sys/block/sda/size — disk size of sda in 512-byte sectors
    • /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor — current CPU frequency scaling mode

    Practical Uses of Device Files

    1. Writing to the null device
      echo "Hello" > /dev/null

      The data will disappear without a trace.

      1. Reading random data
        head -c 10 /dev/random

        Outputs 10 random bytes.

                        3. Reading a network interface MAC address

 

cat /sys/class/net/eth0/address

                       4. Creating a named pipe and transmitting data

mkfifo /tmp/mypipe
echo "test" > /tmp/mypipe & cat /tmp/mypipe

The data from echo is passed through the named pipe to cat.

Conclusion

The /dev and /sys directories play a crucial role in Linux, bridging the kernel and hardware. Understanding their structure enables more effective system management, working with disks, terminals, and other hardware components.

How do you rate this article?

0


SysOpsMaster
SysOpsMaster

Hi, I’m a SysOps professional with expertise in automation, CI/CD, and infrastructure management. I specialize in tools like GitLab CI/CD, Ansible (AWX), Docker, Docker Compose, Terraform, and Nexus Repository OSS, working primarily in Linux environments.


Level Up: Linux & Ops
Level Up: Linux & Ops

Exploring the Foundations: Advanced System Administration and Development

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.