28

A while ago I noticed this directory that I had not ever seen before, /sys. I researched a bit and read that "modern Linux systems" often have this directory, and that it manages devices. I thought that was what /dev was for. I can't seem to find a whole lot of information about this directory, other than what I mentioned, and this, quoted from this page:

/sys is a virtual file system that can be accessed to set or obtain information about the kernel's view of the system.

I have been running Trusty for a while now, and never noticed it before, which is why I find it a little strange. Would someone please fill me in? What is the difference between /sys and /dev? When did Ubuntu start using this directory, and why? Thanks.

2
  • /sys has been around for over a decade now.
    – muru
    Jan 13, 2016 at 4:21
  • 2
    Sidenote , perhaps will be useful to other users: /proc and /sys directories ,being virtual filesystems, will disappear once the the computer is shut down. If you ever mounted a hard drive with Linux on another OS you'll see that those directories are empty. Jan 13, 2016 at 5:44

1 Answer 1

43

/sys is old. It was introduced before the Linux kernel reached 2.6 (back when there was a 2.4/2.5 split). Since the first Ubuntu release used a 2.6 kernel, every version of Ubuntu has had a /sys.

/dev contains the actual device files. It does not provide access to all devices that the kernel knows of (such as ethernet devices, for one - Why are network interfaces not in /dev like other devices?, Why do Ethernet devices not show up in "/dev"?). It is an interface to the device itself - you write to the device, read from it, etc.

/sys is an interface to the kernel. Specifically, it provides a filesystem-like view of information and configuration settings that the kernel provides, much like /proc. Writing to these files may or may not write to the actual device, depending on the setting you're changing. It isn't only for managing devices, though that's a common use case.

More information can be found in the kernel documentation:

Top Level Directory Layout
~~~~~~~~~~~~~~~~~~~~~~~~~~

The sysfs directory arrangement exposes the relationship of kernel
data structures. 

The top level sysfs directory looks like:

block/
bus/
class/
dev/
devices/
firmware/
net/
fs/

devices/ contains a filesystem representation of the device tree. It maps
directly to the internal kernel device tree, which is a hierarchy of
struct device. 

bus/ contains flat directory layout of the various bus types in the
kernel. Each bus's directory contains two subdirectories:

    devices/
    drivers/

devices/ contains symlinks for each device discovered in the system
that point to the device's directory under root/.

drivers/ contains a directory for each device driver that is loaded
for devices on that particular bus (this assumes that drivers do not
span multiple bus types).

fs/ contains a directory for some filesystems.  Currently each
filesystem wanting to export attributes must create its own hierarchy
below fs/ (see ./fuse.txt for an example).

dev/ contains two directories char/ and block/. Inside these two
directories there are symlinks named <major>:<minor>.  These symlinks
point to the sysfs directory for the given device.  /sys/dev provides a
quick way to lookup the sysfs interface for a device from the result of
a stat(2) operation.

For example:

  • One way of setting the brightness of a laptop monitor is:

    echo N > /sys/class/backlight/acpi_video0/brightness
    
  • To get the a network card's MAC address:

    cat /sys/class/net/enp1s0/address
    
  • To get the current CPU scaling governors:

    cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    

And so on...

3
  • @muru: What if I want to remove one of these files which is consuming a lot of memory? I wrote a device driver which was buggy(may be) which went into a loop and continuously wrote to some file in /sys/devices directory. Also, if I accidentally delete a wrong file what would be the impact? Apr 14, 2016 at 5:13
  • 3
    @BhavikShah you don't remove anything from here - these files don't actually exist. You'll have to remove the module involved.
    – muru
    Apr 14, 2016 at 6:01
  • @muru Can one remove/modify/add any file from/to/in /sys directory? Hope so you'll answer because I'm in some trouble. Jan 2, 2022 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.