What's the "/sys" directory for?

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

1 Answer

/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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like