Skip to content

Linux Foundations / Understand the system underneath ยท Step 16 of 38

Lesson: Exploring sysfsSwitch
โ† Linux Foundations

System administration

Exploring sysfs

Use the /sys filesystem to inspect devices, drivers and their settings.

/dev lets you use a device. /sys lets you learn about it. The /sys directory is the mount point of sysfs, a virtual filesystem in which the kernel presents devices, drivers and related objects.

A filesystem made by the kernel

Nothing in /sys is stored on disk. The kernel produces the contents when you read them.

bash
findmnt /sys
TARGET SOURCE FSTYPE OPTIONS
/sys   sysfs  sysfs  rw,nosuid,nodev,noexec,relatime

The main directories

bash
ls /sys
block  bus  class  dev  devices  firmware  fs  kernel  module  power
  • /sys/devices is the device tree, arranged by how things are connected.
  • /sys/class groups devices by function, such as net or tty.
  • /sys/bus groups them by bus, such as pci or usb.
  • /sys/block lists block devices.
  • /sys/module has information about kernel modules.

Many entries in class and block are symbolic links into /sys/devices, so one device can be reached in several ways.

One value per file

An attribute file normally holds a single value as text, so cat is enough. Replace eth0 with an interface that exists on your system.

bash
ls /sys/class/net
cat /sys/class/net/eth0/address
cat /sys/class/net/eth0/mtu
cat /sys/class/net/eth0/operstate
eth0  lo
52:54:00:12:34:56
1500
up

For disks:

bash
cat /sys/block/sda/size
cat /sys/block/sda/queue/rotational
976773168
0

The size value counts 512 byte sectors, whatever the real sector size of the drive. A rotational value of 0 means the kernel treats it as a non spinning device. Virtual disks may not report this accurately.

Writing changes the kernel

Some attributes are writable. Writing one usually changes behaviour immediately and needs root. For example, on a laptop with a backlight entry:

bash
ls /sys/class/backlight
echo 200 | sudo tee /sys/class/backlight/intel_backlight/brightness

The directory name depends on your hardware, and many machines have none. Use tee because a plain sudo echo 200 > file performs the redirection as your normal user. A value written this way does not by itself persist across reboots.

To collect the properties of a device in one go, udevadm info reads from sysfs and the udev database. For permission basics, see the permissions lab.

Try it

  1. Run ls -l /sys/class/net and see where the links point.
  2. Read the address and mtu files of one interface.
  3. Run ls /sys/block, then read the size file for one entry and multiply by 512.
  4. Run ls /sys/module | head to see modules known to the kernel.

Only read files while exploring. Do not write to attributes you do not understand.

Check your understanding

Exploring sysfs

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.