36. Kernel & System Internals
Rather than requiring you to recompile the kernel to add support for new hardware or tune performance, modern Linux systems provide dynamic Kernel Modules and an interface for runtime parameter tuning via sysctl and the /proc and /sys virtual filesystems.
36.1 Virtual Filesystems (/proc, /sys, /dev)
Section titled “36.1 Virtual Filesystems (/proc, /sys, /dev)”The kernel communicates its internal state and provides control levers to user space through special in-memory virtual filesystems.
User Space (Applications, CLI Tools) ↓ System Calls / Virtual Filesystems ┌──────────────┼──────────────┐ ↓ ↓ ↓ /proc /sys /dev (Processes & (Hardware & (Device Nodes) Kernel State) Kernel Objects) └──────────────┬──────────────┘ ↓ Linux Kernel Space ↓ Physical Hardware1. /proc (Process Information Pseudo-Filesystem)
Section titled “1. /proc (Process Information Pseudo-Filesystem)”Does not exist on a physical disk; it is synthesized on the fly by the kernel in RAM.
- Process Data: Each running process gets a folder named after its PID (
/proc/1420/)./proc/<PID>/cmdline: Exact command that started the process./proc/<PID>/environ: Environment variables of the process./proc/<PID>/fd/: File descriptors opened by the process./proc/<PID>/status: Memory, thread count, and state.
- System & Kernel State:
/proc/cpuinfo: CPU model, core count, flags./proc/meminfo: Deep breakdown of RAM, cache, buffers, and swap./proc/sys/: Tunable kernel parameters (mirrored bysysctl).
2. /sys (Sysfs Pseudo-Filesystem)
Section titled “2. /sys (Sysfs Pseudo-Filesystem)”An organized hierarchy introduced in Linux 2.6 that exposes kernel objects, device drivers, power states, and hardware buses to user space.
/sys/class/net/: Network interface controllers./sys/block/: Block storage devices (NVMe, SSD, HDD)./sys/devices/: Physical hardware device tree.
3. /dev (Device Nodes)
Section titled “3. /dev (Device Nodes)”Contains special file nodes representing hardware components.
- Managed dynamically by
udevd. - Character devices (
/dev/urandom,/dev/null,/dev/tty). - Block devices (
/dev/sda,/dev/nvme0n1).
4. dmesg (The Kernel Ring Buffer)
Section titled “4. dmesg (The Kernel Ring Buffer)”The kernel logs boot messages, hardware detection, driver status, and critical faults (OOM killer, hardware errors) into a circular memory buffer:
# View kernel logs with human-readable timestampssudo dmesg -T
# Filter kernel log messages by level (err, crit, alert, emerg)sudo dmesg -l err,crit -T36.2 Kernel Modules (modprobe, lsmod)
Section titled “36.2 Kernel Modules (modprobe, lsmod)”A Kernel Module (LKM - Loadable Kernel Module) is a piece of compiled code (with a .ko extension) that can be loaded into or unloaded from the kernel on demand, without restarting the machine. Modules are used for hardware device drivers, filesystem drivers (e.g., XFS, ext4), and network protocols (e.g., WireGuard, IPVS).
Module binaries reside in /lib/modules/$(uname -r)/.
1. Inspecting Modules (lsmod & modinfo)
Section titled “1. Inspecting Modules (lsmod & modinfo)”# List all currently loaded kernel moduleslsmod
# Filter for a specific module (e.g., network overlay, storage)lsmod | grep -E "overlay|br_netfilter"
# Display detailed information about a module (author, description, parameters)modinfo br_netfilter2. Loading & Unloading Modules (modprobe vs insmod)
Section titled “2. Loading & Unloading Modules (modprobe vs insmod)”The Modern Tool: modprobe
Section titled “The Modern Tool: modprobe”Always prefer modprobe over older utilities. modprobe automatically inspects module dependency trees (modules.dep) and loads all required prerequisite modules automatically.
# Load a kernel module (and its dependencies)sudo modprobe overlaysudo modprobe br_netfilter
# Remove/unload a kernel module (and unused dependencies)sudo modprobe -r overlayThe Low-Level Utilities: insmod & rmmod
Section titled “The Low-Level Utilities: insmod & rmmod”Directly insert or remove a raw .ko file from disk without dependency resolution (mostly used by kernel driver developers):
# Insert raw module file directlysudo insmod /path/to/driver.ko
# Remove module by namesudo rmmod driver3. Persistent Module Loading & Configuration
Section titled “3. Persistent Module Loading & Configuration”Loading Modules Automatically at Boot
Section titled “Loading Modules Automatically at Boot”To ensure critical modules (like container networking modules) load every time the system starts, create a .conf file inside /etc/modules-load.d/:
# Example: Configure required Kubernetes container modulessudo tee /etc/modules-load.d/k8s.conf <<EOFoverlaybr_netfilterEOFBlacklisting & Module Options (/etc/modprobe.d/)
Section titled “Blacklisting & Module Options (/etc/modprobe.d/)”To pass custom configuration options to a module or prevent an unwanted module from loading (e.g., disabling legacy filesystems or Nouveau graphics drivers):
Create a file in /etc/modprobe.d/blacklist.conf:
# Blacklist unwanted driverblacklist nouveau
# Pass parameters to a module on loadoptions bonding mode=1 miimon=10036.3 Sysctl Tuning Architecture
Section titled “36.3 Sysctl Tuning Architecture”The sysctl utility is the interface used to inspect and modify kernel runtime parameters without rebooting. It directly reads and writes values to files in /proc/sys/.
sysctl key: net.ipv4.ip_forward = 1Maps directly to: /proc/sys/net/ipv4/ip_forward1. The sysctl Command
Section titled “1. The sysctl Command”# View all available kernel parameters (thousands of keys)sudo sysctl -a
# View a specific parametersysctl net.ipv4.ip_forward
# Temporarily change a parameter at runtime (resets on reboot!)sudo sysctl -w net.ipv4.ip_forward=1# Equivalent to:echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
# Reload persistent configuration from all /etc/sysctl.d/ filessudo sysctl --system36.4 Production Tuning Presets
Section titled “36.4 Production Tuning Presets”To make kernel parameter changes permanent, create configuration files ending in .conf in the /etc/sysctl.d/ directory.
1. Network Performance & Container Packet Forwarding
Section titled “1. Network Performance & Container Packet Forwarding”Essential for Kubernetes nodes, Docker hosts, and high-throughput web servers:
Create /etc/sysctl.d/99-network.conf:
# Enable IPv4 packet forwarding (MANDATORY for Docker/Kubernetes routing)net.ipv4.ip_forward = 1
# Allow bridge netfilter to inspect iptables rulesnet.bridge.bridge-nf-call-iptables = 1net.bridge.bridge-nf-call-ip6tables = 1
# Increase max socket connection backlog (default is often 128 or 4096)net.core.somaxconn = 65535
# Increase max incoming packet backlog queuenet.core.netdev_max_backlog = 10000
# Increase TCP read/write buffer limits (for 10Gbps+ networks)net.core.rmem_max = 16777216net.core.wmem_max = 16777216
# Enable TCP BBR congestion control algorithm (faster throughput on high latency)net.core.default_qdisc = fqnet.ipv4.tcp_congestion_control = bbr
# Enable fast recycling of TIME_WAIT sockets safelynet.ipv4.tcp_tw_reuse = 12. File Descriptor & Inotify Limits
Section titled “2. File Descriptor & Inotify Limits”High-concurrency servers (Node.js, Elasticsearch, Nginx, Redis) frequently crash with "Too many open files" or "System limit for number of file watchers reached":
Create /etc/sysctl.d/99-file-limits.conf:
# System-wide maximum open file descriptorsfs.file-max = 2097152
# Max inotify instances and watchers (critical for IDEs, containers, file sync)fs.inotify.max_user_instances = 8192fs.inotify.max_user_watches = 5242883. Memory & Swappiness Tuning
Section titled “3. Memory & Swappiness Tuning”Controls how aggressively the kernel swaps memory to disk and handles out-of-memory states:
Create /etc/sysctl.d/99-memory.conf:
# Swappiness: Scale from 0 to 100 (Default is 60)# Lower values make kernel avoid swapping anonymous pages unless necessary# Production servers / Databases: Set to 10 or 1vm.swappiness = 10
# Virtual Memory Overcommit handling# 0 = Heuristic overcommit (default)# 1 = Always overcommit (Required for Redis background snapshots!)vm.overcommit_memory = 1
# Adjust dirty memory writeback thresholds (ensures continuous disk flushing)vm.dirty_ratio = 20vm.dirty_background_ratio = 10
# Max memory map areas (Required for Elasticsearch / Lucene)vm.max_map_count = 262144Apply all configurations immediately:
sudo sysctl --system