7.5. Inspecting and Tuning Kernel Behavior
The kernel exposes devices, drivers, and tunables through virtual
filesystems. /sys describes the device and driver model; /proc/sys
contains writable kernel parameters; udev turns kernel events into device
nodes and stable names. These interfaces are not a substitute for the
configuration files covered earlier, but they are the fastest way to see what
the kernel actually did and to test a change before making it persistent.
7.5.1. Discovering Devices Through sysfs and udev
/sys/class contains symlinks grouped by device class, while /sys/devices
contains the physical hierarchy. The network interfaces are a compact
example:
$ ls /sys/class/net
eth0
lo
The symlink resolves to the device's place in the hierarchy:
$ readlink -f /sys/class/net/eth0
/sys/devices/pci0000:00/0000:00:05.0/virtio2/net/eth0
The path shows a virtio device on PCI bus 0000:00:05.0. Attributes in the interface directory expose the hardware address and statistics:
$ cat /sys/class/net/eth0/address
52:54:00:30:78:ce
Block devices appear under /sys/block in the same way:
$ ls /sys/block
vda
udevadm info reads the udev database and the sysfs attributes for a device.
The --query=property form prints the properties as KEY=value lines, which
is convenient for matching and scripting:
$ udevadm info --query=property --name=/dev/vda | sed -n "1,12p"
DEVPATH=/devices/pci0000:00/0000:00:03.0/virtio0/block/vda
DEVNAME=/dev/vda
DEVTYPE=disk
DISKSEQ=1
MAJOR=254
MINOR=0
SUBSYSTEM=block
USEC_INITIALIZED=1945027
ID_PATH=pci-0000:00:03.0
ID_PATH_TAG=pci-0000_00_03_0
ID_PART_TABLE_UUID=3427c43a
ID_PART_TABLE_TYPE=dos
DEVPATH is the sysfs path, DEVNAME is the kernel device node, DEVTYPE
distinguishes a whole disk from a partition, and ID_PATH is a stable
hardware path that udev rules can match. The properties are also available
for a symlink created by a rule, which is how a later section verifies its
work. Table 7.14 lists the udevadm query options used most often.
| Option | Description |
|---|---|
--query=property |
Print the device's udev properties. |
--query=all |
Print the device record and properties. |
--query=path |
Print the sysfs device path. |
--query=name |
Print the device node name. |
--name=/dev/... |
Select a device by node. |
--path=/sys/... |
Select a device by sysfs path. |
--attribute-walk |
Walk up the device chain and show attributes usable in rules. |
udevadm monitor is the live view. It prints kernel and udev events as they
occur and keeps running until interrupted:
$ sudo udevadm monitor
The useful options are --kernel, --udev, --property, and --subsystem.
The command is interactive; stop it with Ctrl-C. A udevadm trigger sends
a synthetic event to devices that already exist, which is useful after
changing a rule without replugging hardware. This is the mechanism used in
Section 7.5.3 to apply a new rule to the root disk.
7.5.2. Temporary and Persistent sysctl Settings
/proc/sys exposes kernel parameters as files. Reading one is an ordinary
file read and needs no special privilege:
$ cat /proc/sys/net/ipv4/ip_forward
0
The same value can be read with sysctl, but the tool lives in /usr/sbin,
which is not always in a normal user's PATH; using sudo sysctl or the
/proc/sys path avoids the problem. A temporary change is written with
sysctl -w:
# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
The change takes effect immediately and is visible in /proc/sys:
$ cat /proc/sys/net/ipv4/ip_forward
1
It is not persistent. Restore the original value with the same mechanism:
# sysctl -w net.ipv4.ip_forward=0
net.ipv4.ip_forward = 0
A persistent setting belongs in /etc/sysctl.d, not in a shell startup file
and not in /etc/sysctl.conf on a new system. The file name controls the
order in which systemd-sysctl applies the files at boot; a local file should
sort after the vendor defaults, so 99- is a common prefix:
$ sudo tee /etc/sysctl.d/99-handbook-forward.conf <<"EOF"
net.ipv4.ip_forward=1
EOF
net.ipv4.ip_forward=1
Apply all sysctl configuration files without rebooting:
# sysctl --system
* Applying /usr/lib/sysctl.d/10-coredump-debian.conf ...
* Applying /usr/lib/sysctl.d/50-bubblewrap.conf ...
* Applying /usr/lib/sysctl.d/50-default.conf ...
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
* Applying /etc/sysctl.d/99-handbook-forward.conf ...
kernel.core_pattern = core
kernel.unprivileged_userns_clone = 1
kernel.sysrq = 0x01b6
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.eth0.rp_filter = 2
net.ipv4.conf.lo.rp_filter = 2
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.eth0.accept_source_route = 0
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.eth0.promote_secondaries = 1
net.ipv4.conf.lo.promote_secondaries = 1
net.ipv4.ping_group_range = 0 2147483647
net.core.default_qdisc = fq_codel
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.protected_regular = 2
fs.protected_fifos = 1
vm.max_map_count = 1048576
kernel.pid_max = 4194304
net.ipv4.ip_forward = 1
Each Applying line names one configuration file, and the assignments below
it are the values that file set; the local file is applied last, so its
net.ipv4.ip_forward = 1 wins for this boot. The new value is now in effect:
$ cat /proc/sys/net/ipv4/ip_forward
1
Remove the local file:
$ sudo rm -f /etc/sysctl.d/99-handbook-forward.conf
The command is silent. Running sysctl --system again applies the remaining
files, but it does not reset a value that no file mentions; the kernel
keeps the last value until it is changed or the machine reboots. The output
from the second run contains the other assignments but no
net.ipv4.ip_forward line. Set it back explicitly:
# sysctl -w net.ipv4.ip_forward=0
net.ipv4.ip_forward = 0
$ cat /proc/sys/net/ipv4/ip_forward
0
This behavior is important for rollback: deleting a sysctl file does not undo
the running setting. Plan the restore command, or reboot after removing the
file, and verify with cat /proc/sys/.... Table 7.15 lists the common
sysctl forms.
| Command | Description |
|---|---|
sysctl name |
Read one parameter, for example net.ipv4.ip_forward. |
sysctl -a |
Print all readable parameters. |
sysctl -w name=value |
Set a parameter immediately. |
sysctl -p [file] |
Apply one file, or /etc/sysctl.conf by default. |
sysctl --system |
Apply all files from the standard sysctl directories. |
sysctl -n name |
Print only the value, without the name. |
The standard directories are /etc/sysctl.d, /run/sysctl.d, and
/usr/lib/sysctl.d; the files are processed in lexical order, with a later
assignment winning. /etc/sysctl.conf is still read for compatibility, but a
new local setting should use a drop-in so its origin and ordering are clear.
At boot, systemd-sysctl.service applies the same configuration; an
interactive sysctl --system is the fastest way to test a file before the
next boot.
7.5.3. Device Naming and Targeted udev Rules
Predictable network names are generated by udev from the device's hardware path, MAC address, or slot. The properties are visible even when the names are disabled by the kernel command line:
$ udevadm info --query=property --path=/sys/class/net/eth0 | grep -E "^(ID_NET_NAME|ID_PATH|ID_NET_DRIVER)"
ID_NET_DRIVER=virtio_net
ID_NET_NAME_MAC=enx5254003078ce
ID_NET_NAME_PATH=enp0s5
ID_NET_NAME_SLOT=ens5
ID_PATH=pci-0000:00:05.0
ID_PATH_TAG=pci-0000_00_05_0
ID_NET_NAME=eth0
ID_NET_NAME_SLOT=ens5 is the name udev would use by default on this
machine; ID_NET_NAME=eth0 is the final name because the kernel command line
contains net.ifnames=0. ID_NET_NAME_MAC and ID_NET_NAME_PATH are
alternative schemes, and ID_PATH is the stable hardware path. The
net.ifnames=0 biosdevname=0 parameters are an image choice; a standard
Debian installation normally uses the predictable ens5-style name. The
naming policy can also be set in a systemd .link file under
/etc/systemd/network, which is covered with network configuration in
Chapter 17.
A udev rule can create an additional name without renaming the kernel device.
This example creates a symlink for the root disk, which is safer than changing
/dev/vda itself:
$ sudo tee /etc/udev/rules.d/99-handbook-disk.rules <<"EOF"
KERNEL=="vda", SYMLINK+="handbook-root-disk"
EOF
KERNEL=="vda", SYMLINK+="handbook-root-disk"
The rule matches the kernel name vda and adds a symlink in /dev. A rule
file is read when udev is told to reload; a rule can be matched by KERNEL,
SUBSYSTEM, ATTRS{...}, ENV{...}, and other keys. SYMLINK+= appends to
the list of symlinks, so other rules can add their own names. Reload the
rules and trigger the device:
# udevadm control --reload-rules
The command is silent. Then:
# udevadm trigger --action=change --name-match=vda
The command is silent. The symlink now exists and points at the kernel device:
$ ls -l /dev/handbook-root-disk
lrwxrwxrwx 1 root root 3 Nov 19 04:33 /dev/handbook-root-disk -> vda
The device properties are the same as those of the real node:
$ udevadm info --query=property --name=/dev/handbook-root-disk | grep -E "^(DEVNAME|DEVTYPE|ID_PATH)="
DEVNAME=/dev/vda
DEVTYPE=disk
ID_PATH=pci-0000:00:03.0
Remove the rule, reload, and trigger again to remove the symlink:
$ sudo rm -f /etc/udev/rules.d/99-handbook-disk.rules
The command is silent. Then:
# udevadm control --reload-rules
# udevadm trigger --action=change --name-match=vda
Both are silent. The symlink is gone:
$ ls -l /dev/handbook-root-disk
ls: cannot access '/dev/handbook-root-disk': No such file or directory
The vda name in this rule is specific to this virtio disk. On physical
hardware, match a stable property such as ID_SERIAL, ID_WWN, or a
filesystem UUID instead of a kernel name; the kernel name can change when
disks are added or controllers are reordered. A symlink is useful for a
backup script or a mount unit, but it does not replace the UUID in
/etc/fstab. Table 7.16 lists common rule keys and their use.
| Key | Matches or sets |
|---|---|
KERNEL=="vda" |
The kernel device name. |
SUBSYSTEM=="block" |
The device subsystem. |
ATTRS{serial}=="..." |
An attribute of the device or a parent. |
ENV{ID_SERIAL}=="..." |
A property already assigned by udev. |
SYMLINK+="name" |
Add a symlink in /dev. |
OWNER= / GROUP= / MODE= |
Set ownership or permissions on the node. |
RUN+="/path" |
Run a short program after the device event. |
Rules run in lexical order by filename, and the first assignment of NAME
wins; SYMLINK is additive. A rule that runs a long program in RUN+= will
delay every matching device event, and a rule that renames a network interface
can break the very configuration that expects the old name. Keep rules
narrow, test with udevadm test, and prefer symlinks for block devices.