Difference between /dev and /sys

14,006

Solution 1

The /sys filesystem (sysfs) contains files that provide information about devices: whether it's powered on, the vendor name and model, what bus the device is plugged into, etc. It's of interest to applications that manage devices.

The /dev filesystem contains files that allow programs to access the devices themselves: write data to a serial port, read a hard disk, etc. It's of interest to applications that access devices.

A metaphor is that /sys provides access to the packaging, while /dev provides access to the content of the box.

The files in /sys are not device nodes, but symbolic links and regular files. Those regular files are special in that reading or writing to them invokes file-specific functions in the kernel, like device nodes. The difference is that files in /sys work this way because of the filesystem they are on, whereas device nodes work this way due to their device node characteristics (the file type indicating a (block or character) device, and the device major and minor number indicating which device it is).

The reason for /dev existing independently of /sys is partly historical: /dev dates back to the dawn of Unix, while /sys is a much more recent invention. If Linux was designed today with no historical background, /dev/sda might be /sys/block/sda/content.

Solution 2

  • /dev directory allows access to the device via the device files (or device nodes)
  • /sys directory allows the viewing of the device information and details.

for instance: cat /sys/block/*/device/serial (with * replaced by nvme0n1 or sda or hda ... depending on your case) will print the serial number of the disk storage.

Share:
14,006

Related videos on Youtube

sounak
Author by

sounak

Updated on September 18, 2022

Comments

  • sounak
    sounak over 1 year

    Exactly what is the difference between devfs and sysfs? Both seem to maintain a list of hardwares attached to the system. Then why the need for 2 separate fs even arose? As far as I can get /sys maintains somewhat "raw" list of devices(like "ser0"). Udev acts on those devices, gets various informations and applies various rules to present them as recognizable names which are then mapped onto /dev(like "camera"). Is this the only reason? And then we mount the corresponding devices from the /dev fs(can't we do that from the /sys fs) into the /media fs.

    I have read the answer at Difference between /dev and /sys/class?. But I cannot get the sys fs part where it states that

    Sysfs contain the hierarchy of devices, as they are attached to the computer

    Are the files in /sys not device node files? Then what type of files are they?

    • Ciro Santilli Путлер Капут 六四事
      Ciro Santilli Путлер Капут 六四事 over 7 years
    • Anthony Geoghegan
      Anthony Geoghegan over 7 years
      I don't think this is a duplicate. The answers to both questions look at sysfs from different perspectives and none of the answers to the other question explain how the /dev directory relates to sysfs.
  • Jakob Bennemann
    Jakob Bennemann over 9 years
    See also: man hier.