What's the "/sys" directory for?
/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...