Table of Contents
- What Are Linux Kernel Parameters?
- Types of Kernel Parameters
- How Kernel Parameters Work
- Viewing Kernel Parameters
- Modifying Kernel Parameters
- Critical Kernel Parameters to Know
- 6.1 Performance Optimization
- 6.2 Security Hardening
- 6.3 Networking
- 6.4 Memory Management
- Best Practices for Managing Kernel Parameters
- Conclusion
- References
1. What Are Linux Kernel Parameters?
Linux kernel parameters are configurable variables that control the kernel’s behavior at runtime or boot time. Think of them as “knobs” that adjust how the kernel allocates resources, handles network traffic, enforces security policies, or interacts with hardware. Unlike user-space configuration files (e.g., /etc/nginx/nginx.conf), kernel parameters directly influence the core operating system, making them powerful but also risky to modify without care.
Examples of what kernel parameters control:
- How aggressively the kernel swaps memory to disk (
vm.swappiness). - Whether IPv4 packet forwarding is enabled (
net.ipv4.ip_forward). - How the kernel mitigates buffer overflow attacks (
kernel.randomize_va_space).
2. Types of Kernel Parameters
Kernel parameters are broadly categorized based on when they are set and how they are modified:
2.1 Compile-Time Parameters
These parameters are fixed when the kernel is compiled (e.g., during OS installation or custom kernel builds). They are defined in the kernel’s source code or via the .config file (generated with tools like make menuconfig). Examples include:
CONFIG_HZ: Controls the kernel timer frequency (e.g., 1000 Hz for low latency).CONFIG_NETFILTER: Enables firewall support (required foriptables/nftables).
Note: Compile-time parameters cannot be changed without recompiling the kernel, so they are less flexible than runtime parameters.
2.2 Runtime Parameters
Runtime parameters can be modified without recompiling the kernel and are the focus of this guide. They are further divided into:
Boot Parameters
Passed to the kernel during system boot via the bootloader (e.g., GRUB, systemd-boot). They often configure hardware initialization or low-level behavior. Examples:
intel_idle.max_cstate=1: Limits CPU power-saving states (reduces latency).nomodeset: Disables kernel mode-setting for graphics drivers (troubleshooting).
Sysctl Parameters
Managed via the sysctl interface (or /proc/sys filesystem) and can be adjusted at runtime. These control higher-level behavior like networking, memory, and security. Examples:
vm.swappiness: Adjusts swap usage behavior.net.ipv4.tcp_syncookies: Enables protection against SYN flood attacks.
3. How Kernel Parameters Work
The kernel stores runtime parameters in memory and exposes them to userspace via two virtual filesystems:
/proc/sys: A read/write interface for sysctl parameters (e.g.,/proc/sys/net/ipv4/ip_forward)./sys: Exposes device-specific parameters and kernel objects (e.g.,/sys/class/net/eth0/speedfor network interface speed).
Boot parameters are passed by the bootloader to the kernel’s init process early in the boot sequence. The kernel parses these parameters and applies them before user-space processes start.
4. Viewing Kernel Parameters
To work with kernel parameters, you first need to know how to inspect their current values.
4.1 Using sysctl
The sysctl command is the standard tool for querying and modifying sysctl parameters.
-
List all sysctl parameters:
sysctl -a # Lists all parameters (may be verbose) -
Filter by category (e.g., memory parameters):
sysctl -a | grep vm # Show all "vm" (virtual memory) parameters -
View a specific parameter:
sysctl vm.swappiness # Output: vm.swappiness = 60 (default)
4.2 Via /proc/sys and /sys Filesystems
The /proc/sys directory mirrors sysctl parameters as files. For example, vm.swappiness is stored in /proc/sys/vm/swappiness.
- Read a parameter via
/proc/sys:cat /proc/sys/vm/swappiness # Output: 60
The /sys filesystem exposes device and driver-specific parameters. For example, to check the maximum CPU frequency:
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
5. Modifying Kernel Parameters
Modifying kernel parameters requires caution: incorrect values can crash the system or introduce security vulnerabilities. Always test changes in a staging environment first!
5.1 Temporary Changes (Runtime)
Temporary changes apply immediately but are lost after a reboot. Use these for testing.
For Sysctl Parameters:
-
With
sysctl -w:sysctl -w vm.swappiness=10 # Temporarily set swappiness to 10 -
Directly via
/proc/sys:echo 10 > /proc/sys/vm/swappiness # Equivalent to the above
For Boot Parameters:
Boot parameters cannot be modified at runtime—they must be set during boot. To test a boot parameter temporarily:
- Reboot the system and interrupt the bootloader (e.g., press
Ein GRUB). - Add the parameter to the kernel command line (e.g., append
intel_idle.max_cstate=1). - Press
Ctrl+Xto boot with the modified parameters.
5.2 Permanent Changes (Persistent Across Reboots)
To make changes survive reboots, use these methods:
For Sysctl Parameters:
-
Edit
/etc/sysctl.conf(traditional):
Add the parameter to/etc/sysctl.conf:echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.confApply changes with:
sudo sysctl -p # Reloads /etc/sysctl.conf -
Use
/etc/sysctl.d/(modern, systemd-based systems):
Create a.conffile in/etc/sysctl.d/(e.g.,99-custom.conf):echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-custom.confSystemd automatically loads files in
/etc/sysctl.d/at boot. To apply immediately:sudo sysctl --system # Reloads all sysctl.d files
For Boot Parameters:
Persistent boot parameters are set via the bootloader configuration.
- GRUB (Most Linux Distributions):
- Edit the GRUB config template:
sudo nano /etc/default/grub - Add the parameter to
GRUB_CMDLINE_LINUX:GRUB_CMDLINE_LINUX="intel_idle.max_cstate=1" - Regenerate the GRUB configuration:
- Debian/Ubuntu:
sudo update-grub - RHEL/CentOS/Fedora:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
- Debian/Ubuntu:
- Reboot for changes to take effect.
- Edit the GRUB config template:
6. Critical Kernel Parameters to Know
Below are key parameters across categories, with use cases and examples.
6.1 Performance Optimization
| Parameter | Description | Default | Use Case Example |
|---|---|---|---|
vm.swappiness | Controls swap aggressiveness (0 = minimal swap, 100 = aggressive swap). | 60 | Set to 10-20 for servers with ample RAM. |
vm.dirty_ratio | % of memory filled with “dirty” (unsynced) data before kernel writes to disk. | 20 | Lower to 5-10 for latency-sensitive apps (e.g., databases). |
net.core.somaxconn | Maximum pending TCP connections (backlog). | 4096 | Increase to 16384 for high-traffic web servers. |
kernel.sched_min_granularity_ns | Minimum time a task runs before preemption (low = better interactivity). | 750000 | Set to 1000000 for batch processing (higher throughput). |
6.2 Security Hardening
| Parameter | Description | Default | Use Case Example |
|---|---|---|---|
kernel.randomize_va_space | Enables Address Space Layout Randomization (ASLR) to prevent buffer overflows. | 2 (full) | Never set to 0 (disables ASLR). |
kernel.kptr_restrict | Hides kernel pointers in /proc files (prevents info leaks). | 1 | Set to 2 for stricter security. |
kernel.dmesg_restrict | Restricts non-root users from reading dmesg (kernel logs). | 0 | Set to 1 to hide sensitive hardware/driver info. |
net.ipv4.conf.all.rp_filter | Enables reverse path filtering (prevents IP spoofing). | 1 | Set to 1 (strict) on routers. |
6.3 Networking
| Parameter | Description | Default | Use Case Example |
|---|---|---|---|
net.ipv4.ip_forward | Enables IPv4 packet forwarding (required for routers/VPNs). | 0 | Set to 1 to route traffic between interfaces. |
net.ipv4.tcp_syncookies | Enables SYN cookies (protects against SYN flood DDoS). | 1 | Always set to 1 on public servers. |
net.ipv4.tcp_keepalive_time | Time (seconds) before sending TCP keepalive probes. | 7200 | Lower to 300 (5 minutes) for unstable connections. |
6.4 Memory Management
| Parameter | Description | Default | Use Case Example |
|---|---|---|---|
vm.overcommit_memory | Controls memory overcommit behavior (0 = heuristic, 1 = always overcommit, 2 = never). | 0 | Set to 1 for databases (e.g., PostgreSQL) that manage their own memory. |
vm.min_free_kbytes | Minimum free memory (KB) reserved for critical operations. | ~1000 | Increase to 1% of total RAM on servers (e.g., 16384 KB for 16GB RAM). |
kernel.shmmax | Maximum size (bytes) of a single shared memory segment (for apps like Oracle). | ~32MB | Set to 50% of RAM (e.g., 536870912 for 1GB RAM). |
7. Best Practices for Managing Kernel Parameters
- Test First: Always modify parameters temporarily in staging before applying them to production.
- Document Changes: Log why a parameter was modified, its original value, and the new value (e.g., in
/etc/sysctl.d/99-custom.confcomments). - Backup Configs: Before editing
grub.cfgorsysctl.conf, create backups (e.g.,sudo cp /etc/default/grub /etc/default/grub.bak). - Understand the Parameter: Read the kernel documentation (see References) to avoid unintended side effects.
- Monitor After Changes: Use tools like
vmstat,sar, ornetstatto verify performance/security improvements.
8. Conclusion
Linux kernel parameters are powerful tools for tailoring your system to specific workloads, whether optimizing for performance, hardening security, or troubleshooting hardware. By understanding how to view, modify, and manage these parameters, you can unlock the full potential of your Linux system while avoiding common pitfalls.
Remember: with great power comes great responsibility. Always test changes carefully, document your work, and refer to official kernel documentation when in doubt.