什么是“/ sys”目录?

前一段时间我注意到了以前从未见过的这个目录, /sys 。 我研究了一下,读到“现代Linux系统”经常有这个目录,并且管理设备。 我认为那是/ dev的用途。 我似乎无法找到有关此目录的大量信息,除了我提到的内容,以及本页引用的内容:

/ sys是一个虚拟文件系统,可以访问它来设置或获取有关内核系统视图的信息。

我已经运行了Trusty一段时间了,之前从未注意过,这就是为什么我觉得它有点奇怪。 请有人请我填一下吗? 这个和/ dev有什么区别? Ubuntu什么时候开始使用这个目录,为什么? 谢谢。

/sys 。 它是在Linux内核达到2.6之前引入的(当时有2.4 / 2.5分割)。 由于第一个Ubuntu版本使用的是2.6内核 ,因此每个版本的Ubuntu都有一个/sys

/dev包含实际的设备文件。 它不提供对内核知道的所有设备的访问(例如以太网设备, 为什么 – 为什么网络接口不像其他设备一样在/ dev中? 为什么以太网设备不会出现在“/ dev”中? )。 它是设备本身的接口 – 您可以写入设备,从设备读取等。

/sys是内核的接口。 具体来说,它提供了类似文件系统的内核提供的信息和配置设置视图,就像/proc 。 写入这些文件可能会也可能不会写入实际设备,具体取决于您要更改的设置。 它不仅用于管理设备,还有一个常见的用例。

可以在内核文档中找到更多信息:

 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 :. 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. 

例如:

  • 设置笔记本电脑显示器亮度的一种方法是:

     echo N > /sys/class/backlight/acpi_video0/brightness 
  • 获取网卡的MAC地址:

     cat /sys/class/net/enp1s0/address 
  • 要获得当前的CPU缩放调控器:

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

等等…