/sys/
The /sys
directory is an essential part of the Linux filesystem, especially related to the system's hardware and kernel. Here’s a detailed explanation of /sys
:
Understanding the /sys
Directory
/sys
DirectoryThe /sys
directory, also known as sysfs, is a virtual filesystem in Linux that provides information about the kernel, devices, and other system-related attributes. It was introduced in Linux kernel version 2.6. The /sys
directory is used to export kernel data structures, their attributes, and relationships to userspace. It is mounted automatically by the system at boot time.
Key Characteristics of /sys
:
/sys
:Virtual Filesystem: Similar to
/proc
, the files in/sys
do not exist on disk; they are created dynamically by the kernel.Hierarchy: The structure of
/sys
mirrors the internal kernel structures. It provides a hierarchical view of devices and their attributes.Read and Write: Unlike
/proc
, which is mostly read-only, many files in/sys
can be written to, allowing for dynamic configuration of hardware parameters.
Key Subdirectories in /sys
:
/sys
:/sys/class/:
Contains directories for each class of device. Device classes include
block
,net
,tty
, etc.Example:
/sys/class/net
contains directories for each network interface (e.g.,eth0
,lo
).
/sys/block/:
Contains directories for each block device. Block devices include hard drives and other storage devices.
Example:
/sys/block/sda
for the first SCSI disk.
/sys/bus/:
Contains directories for each bus type in the system (e.g.,
pci
,usb
).Example:
/sys/bus/usb
contains directories for USB devices.
/sys/devices/:
Contains directories for all devices, organized by their physical hierarchy.
Example:
/sys/devices/pci0000:00
for PCI devices.
/sys/firmware/:
Contains directories for firmware-related information.
Example:
/sys/firmware/efi
for EFI firmware variables.
Examples of Using /sys
:
/sys
:Listing Network Interfaces:
Output might include:
Getting Device Information:
Output might be the MAC address of
eth0
:Modifying Device Attributes:
Example: Changing the brightness of a laptop screen.
Practical Use Cases:
Monitoring Hardware Health:
You can read sensor data like CPU temperature:
Managing Power:
You can manage power settings for devices, like enabling or disabling USB ports:
Configuring Kernel Parameters:
You can configure various kernel parameters dynamically without needing a reboot:
Conclusion
The /sys
directory is a powerful tool for Linux system administrators, providing a way to query and control kernel and hardware attributes dynamically. Understanding and utilizing /sys
can greatly enhance your ability to manage and optimize your Linux system.
Feel free to ask for explanations of other objectives or for more examples and details!
Last updated