Skip to content

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 Hardware

1. /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 by sysctl).

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.

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

The kernel logs boot messages, hardware detection, driver status, and critical faults (OOM killer, hardware errors) into a circular memory buffer:

Terminal window
# View kernel logs with human-readable timestamps
sudo dmesg -T
# Filter kernel log messages by level (err, crit, alert, emerg)
sudo dmesg -l err,crit -T

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)/.

Terminal window
# List all currently loaded kernel modules
lsmod
# 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_netfilter

2. Loading & Unloading Modules (modprobe vs insmod)

Section titled “2. Loading & Unloading Modules (modprobe vs insmod)”

Always prefer modprobe over older utilities. modprobe automatically inspects module dependency trees (modules.dep) and loads all required prerequisite modules automatically.

Terminal window
# Load a kernel module (and its dependencies)
sudo modprobe overlay
sudo modprobe br_netfilter
# Remove/unload a kernel module (and unused dependencies)
sudo modprobe -r overlay

Directly insert or remove a raw .ko file from disk without dependency resolution (mostly used by kernel driver developers):

Terminal window
# Insert raw module file directly
sudo insmod /path/to/driver.ko
# Remove module by name
sudo rmmod driver

3. Persistent Module Loading & Configuration

Section titled “3. Persistent Module Loading & Configuration”

To ensure critical modules (like container networking modules) load every time the system starts, create a .conf file inside /etc/modules-load.d/:

Terminal window
# Example: Configure required Kubernetes container modules
sudo tee /etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOF

Blacklisting & 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 driver
blacklist nouveau
# Pass parameters to a module on load
options bonding mode=1 miimon=100

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 = 1
Maps directly to: /proc/sys/net/ipv4/ip_forward
Terminal window
# View all available kernel parameters (thousands of keys)
sudo sysctl -a
# View a specific parameter
sysctl 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/ files
sudo sysctl --system

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 rules
net.bridge.bridge-nf-call-iptables = 1
net.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 queue
net.core.netdev_max_backlog = 10000
# Increase TCP read/write buffer limits (for 10Gbps+ networks)
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# Enable TCP BBR congestion control algorithm (faster throughput on high latency)
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
# Enable fast recycling of TIME_WAIT sockets safely
net.ipv4.tcp_tw_reuse = 1

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 descriptors
fs.file-max = 2097152
# Max inotify instances and watchers (critical for IDEs, containers, file sync)
fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 524288

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 1
vm.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 = 20
vm.dirty_background_ratio = 10
# Max memory map areas (Required for Elasticsearch / Lucene)
vm.max_map_count = 262144

Apply all configurations immediately:

Terminal window
sudo sysctl --system