1. Overview

In the realm of Linux administration, two commonly used directories, /dev and /sys/class, serve as cornerstones when managing and interacting with hardware devices. While both are integral to device management, they possess unique characteristics and distinct purposes.

In this tutorial, we’ll find out the differences between the /dev and /sys/class directories.

2. Working With /sys/class

When it comes to /sys/class, each device class has its directory, containing files that represent specific device attributes and properties. Furthermore, these files hold beneficial information about the device specifications.

This is the case with most /sys pseudo-filesystem contents.

2.1. Accessing Device Information

To understand the hierarchical organization of /sys/class, let’s explore the /sys/class/net directory, which contains network interfaces. Moreover, within this directory, we can find subdirectories for each network interface:

$ ls /sys/class/net

Here, we used the ls command to get the list of directories within this path. Of course, we can navigate to any of the paths within the /sys/class/net tree. Consequently, each of the subdirectories contains files that provide information about the respective network interface.

For instance, let’s find out how to retrieve the network interface name:

$ cd /sys/class/net/eth0
$ cat name

We used the cd command to change our current directory. Afterward, we used the cat command to list the content of the name.txt file in this path. As a result, we see the name of the interface, eth0, presented as the output.

Similarly, if we substitute the word net with usb in the snippet above, we’d list all USB devices. Meanwhile, the above snippet is just an example that explains the concept of classification in /sys/class.

2.2. Manipulating Device Attributes

While /sys/class files are primarily intended for reading device information, some files allow for limited manipulation of device attributes. For instance, to enable power management mode on the eth0 network interface, we can write 1 to the power/control file:

$ sudo echo 1 > /sys/class/net/eth0/power/control

This command activates power management mode on the eth0 network interface. Furthermore, we achieved this by utilizing the echo command to change the contents of the power/control file to 1. In practice, we just use the > operand to overwrite the file.

Let’s check out another example:

$ sudo echo 'power-down' > /sys/class/scsi_host/host0/link_power_management_policy

This command changes the link power management policy for the first SCSI host to power down its links when they aren’t in use.

Next, we confirm the change above took place by using the cat command:

$ cat /sys/class/scsi_host/host0/link_power_management_policy

Hence, we can see that the status changed to power-down.

2.3. Scripting Device Management

Automation of device-related tasks can be achieved using scripts that interact with files in /sys/class:

temp=$(cat /sys/class/thermal/thermal_zone0/temp)
if [ "$temp" -gt "70000" ]; then
  echo "High CPU temperature detected! Take necessary actions."

In this example, we edit the path holding the temperature information of the CPU to check if the value has reached 7000 to print a message that a high CPU temperature is detected.

3. Working With /dev

In this section, we’ll understand and get familiar with using /dev. Although they’re not the same, since both are pseudo-filesystems, the /dev directory is strongly related to the /sys/dev directory.

3.1. /dev Structuring

To get the list of device nodes, we use the lsblk command. It employs contents common to the /dev and /sys/dev directories to extract device data.

First, let’s install the needed packages for Debian-based distros:

$ sudo apt-get install util-linux

Next, we’ll understand the above command:

  • sudo grants superuser privileges for a current user
  • apt-get is the utility we use to install a certain package
  • install is the keyword to tell Linux to install a package
  • util-linux is the package we want to install

Now, let’s also list the components in /dev:

$ ls
autofs           input       null      random   tty    usbmon0
block            kmsg        port      shm      tty0   video0
bsg              loop0 ....

Concisely, the output presents a diverse set of device nodes representing various hardware and virtual devices within the /dev directory.

Next, let’s check out and understand the output of the lsblk command:

$ lsblk
sda      8:0    0 238.5G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0    20G  0 part /
└─sda3   8:3    0 217.9G  0 part /home
sdb      8:16   1   7.5G  0 disk 
└─sdb1   8:17   1   7.5G  0 part /media/usb-drive

In particular, we’ll examine each column of the output above:

  1. NAME defines the name of the block device
  2. MAJ:MIN specifies the major and minor device numbers
  3. RM indicates if the device is removable, where 1 is for removable and 0 is for non-removable
  4. SIZE is the total size of the block device
  5. RO indicates whether the device is read-only, where 1 is for read-only, 0 is for read-write
  6. TYPE is the type of block device
  7. MOUNTPOINT defines the mount point where the device is mounted

In this example, there are two block devices, sda and sdb. sda is a storage disk with three partitions (sda1, sda2, and sda3), and sdb is a removable USB drive with one partition (sdb1). Accordingly, the partitions on sda are mounted at /boot/efi, /, and /home respectively. Meanwhile, the partition on sdb is mounted at /media/usb-drive.

3.2. /dev Read and Write Operations

The /dev directory enables us to have direct device access. To see the direct device access capabilities of the /dev directory, let’s explore the /dev/sda1 device node. This device node represents the first partition on a storage disk.

By opening and reading this device node using the hexdump command, we can retrieve data stored on the storage disk without the risk of breaking our terminal, as the text-only cat might do:

$ hexdump -C /dev/sda1
00000000  eb 0b 3c 1b 77 0a 0d 3d  7b 22 4d 45 54 52 22 3a  |..<.w..={"METR":|
00000010  22 20 22 57 4d 4c 42 75  69 6c 64 22 3a 31 39  |" "{"WMBULD":19|
00000020  39 32 34 38 2c 20 22 43  6f 6d 6d 61 6e 74 22  |9248, "Compact":|
00000030  3a 30 2c 20 22 45 6e 22  3a 27 6d 65 74 72 69  |0, "En":"metri|

Particularly, this command retrieves the contents of the first partition on the storage disk and displays them on the terminal in a hexadecimal format.

Moreover, the /dev directory also supports importing data to devices. For instance, to transfer data from a file to the first partition on the storage disk, we can utilize the dd command:

$ sudo dd if=data.txt of=/dev/sda1

In the above snippet, we copy the contents of the file data.txt to the first partition on the storage disk, sda1.

3.3. /dev Control Operations

Beyond data transfer, /dev empowers device control operations using the ioctl command. In essence, the ioctl command transmits control instructions to device drivers, which oversee the underlying hardware.

For example, to activate disk spinning on the device /dev/sda, we can use the appropriate ioctl command:

$ sudo ioctl /dev/sda BLKRRD

This command engages disk spinning, permitting the storage disk to engage in data read and write operations.

Moreover, device nodes play a pivotal role in managing terminal devices (TTY):

$ screen /dev/ttyUSB0

For instance, /dev/ttyUSB0 represents a USB-to-serial adapter. Accordingly, users can utilize commands like screen or minicom to interact with TTY devices.

4. Key Differences Between /dev and /sys/class

Understanding the nuances between /dev and /sys/class is useful for effective device management in Linux systems.

So, let’s first delve into the key differences between the /dev and /sys/class directories:

  • purpose
  • access
  • kernel interaction
  • content
  • use cases

Each aspect of the list above resembles a difference in how /dev and /sys/class are interpreted.

4.1. Purpose

/dev and /sys/class directories serve distinct purposes within the Linux system.

In particular, /dev is primarily used for direct device control, providing a gateway to device files that represent physical devices. Moreover, these device nodes enable low-level device interactions, such as reading and writing data.

On the other hand, /sys/class focuses on providing device information and configuration. Accordingly, it organizes devices into logical groups based on their functionality, offering a structured view for efficient device identification and management.

4.2. Access

Accessing device nodes in /dev feels quite similar to interacting with standard files, where we can read, write, and execute commands using familiar file operations:

  • open
  • read
  • write
  • close
  • ioctl
  • fsync

This enables applications to directly interact with the underlying hardware. Moreover, such direct access empowers applications to perform device operations easily.

/sys/class takes a different approach. In essence, each device class has its own directory, containing files that represent specific device attributes and properties.

These files provide descriptive information about the device:

  • size
  • type
  • current state

Unlike device nodes in /dev, some of the /sys/class files are read-only, preventing direct manipulation of the underlying hardware.

4.3. Kernel Interaction

Furthermore, /dev communicates directly with the kernel’s device drivers, enabling direct access to hardware devices. On the other hand, /sys/class utilizes the kernel’s sysfs subsystem for device access, providing a more indirect interface.

4.4. Content

While both the /dev and /sys/class directories play huge roles in managing hardware devices, their content differs significantly. In particular, /dev harbors special files called device nodes, which act as virtual representations of underlying physical devices. In addition, these device nodes are treated as regular files, enabling direct manipulation and interaction with the associated hardware.

On the other hand, /sys/class has no device nodes, instead housing files that provide descriptive information about the attributes and properties of devices. Furthermore, some of the files are read-only, such as the .exe files. In this way, they prevent direct manipulation of the underlying hardware but offer valuable insights into device status and configuration.

4.5. Use Cases

/dev is the primary interface for applications that need to directly interact with hardware devices. It offers direct access to device nodes, which act as symbolic links to the underlying hardware. Consequently, this enables applications to perform device operations, such as reading and writing data, configuring device parameters, and controlling device status.

On the other hand, the /sys/class directory is primarily used for diagnostic purposes, configuration, and scripting. In addition, it provides a hierarchical view of devices categorized by class, making it easier to identify and manage similar types of devices. Furthermore, since, some of the files have a read-only nature, /sys/class files make it well-suited for monitoring device attributes and retrieving descriptive information.

5. Conclusion

In conclusion, /dev and /sys/class stand as pillars in the realm of Linux device management. While /dev facilitates direct device control, /sys/class provides a structured approach for information retrieval and configuration.

In this article, we saw the versatility and power of device nodes, understanding their applicability in scenarios ranging from storage management to terminal device interaction.

Comments are closed on this article!