Lab 01: sysctl — Kernel Parameters

Time: 40 minutes | Level: Advanced | Docker: docker run -it --rm --privileged ubuntu:22.04 bash


Overview

The Linux kernel exposes hundreds of tunable parameters through the sysctl interface and the /proc/sys/ virtual filesystem. These parameters control networking, memory management, security hardening, and system behavior — all at runtime, without rebooting. This lab covers reading, modifying, and persisting kernel parameters safely.


Step 1: Explore the /proc/sys/ Hierarchy

The /proc/sys/ filesystem mirrors the sysctl namespace as a directory tree.

ls /proc/sys/

📸 Verified Output:

abi  debug  dev  fs  kernel  net  user  vm

Each subdirectory corresponds to a sysctl namespace:

ls /proc/sys/net/ipv4/ | head -10

📸 Verified Output:

conf
fib_multipath_hash_fields
fib_multipath_hash_policy
fib_multipath_hash_seed
fib_multipath_use_neigh
fib_notify_on_flag_change
fwmark_reflect
icmp_echo_enable_probe
icmp_echo_ignore_all
icmp_echo_ignore_broadcasts

💡 Every file under /proc/sys/ maps 1:1 to a sysctl key — slashes become dots. So /proc/sys/net/ipv4/ip_forwardnet.ipv4.ip_forward.


Step 2: Read Parameters with sysctl -a

List all available kernel parameters (983+ on a typical system):

📸 Verified Output:

Read specific parameters:

📸 Verified Output:

💡 You can also read directly from /proc/sys/: cat /proc/sys/net/ipv4/ip_forward — the output is identical.


Step 3: Understand Key Parameters

Parameter
Description
Typical Value

net.ipv4.ip_forward

Enable IPv4 packet forwarding (routing)

0 (off), 1 (on)

vm.swappiness

How aggressively kernel uses swap (0–100)

60 default

fs.file-max

Max open file descriptors system-wide

varies

kernel.panic

Seconds before auto-reboot on panic (0 = never)

0

net.core.somaxconn

Max TCP connection backlog per socket

1284096

Read security-relevant parameters:

📸 Verified Output:

💡 kernel.randomize_va_space = 2 means full ASLR (Address Space Layout Randomization) is enabled — a key defense against exploit techniques.


Step 4: Runtime Changes with sysctl -w

Changes made with -w take effect immediately but are not persistent across reboots:

📸 Verified Output:

You can also write directly to /proc/sys/:

📸 Verified Output:

💡 Direct writes to /proc/sys/ and sysctl -w are equivalent. The proc interface is useful in scripts.


Step 5: Persist Changes with sysctl.conf

Runtime changes vanish on reboot. To persist them, write to /etc/sysctl.conf or a drop-in file under /etc/sysctl.d/:

📸 Verified Output:

The sysctl -p command loads parameters from a file (default: /etc/sysctl.conf).

💡 Files in /etc/sysctl.d/ are processed alphabetically. Use 99- prefix so your overrides apply last, after any distro defaults.


Step 6: Network Tuning Parameters

For high-throughput servers, tune these networking parameters:

📸 Verified Output:

A production web server config (/etc/sysctl.d/99-network-tuning.conf):


Step 7: Security Hardening Parameters

Security-focused sysctl settings to harden a production system:

A security hardening config (/etc/sysctl.d/99-security.conf):

💡 Run sysctl -p /etc/sysctl.d/99-security.conf to apply immediately, and it will auto-apply on next boot.


Step 8: Capstone — Tune a Server for High Connections

Scenario: You're deploying a high-traffic API gateway. Apply and verify a complete tuning profile.

📸 Verified Output:


Summary

Command
Purpose

sysctl -a

List all kernel parameters

sysctl <key>

Read a single parameter

sysctl -w <key>=<val>

Set parameter at runtime (non-persistent)

sysctl -p <file>

Load parameters from a file

cat /proc/sys/...

Read parameter via filesystem

echo val > /proc/sys/...

Write parameter via filesystem

/etc/sysctl.conf

Default persistent config file

/etc/sysctl.d/*.conf

Drop-in config directory (preferred)

sysctl -a | grep <term>

Search for parameters by name

Last updated