Configure Linux kernel parameters with sysctl for system optimization

Beginner 25 min Apr 17, 2026 1,088 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Learn to view, modify, and persist Linux kernel parameters using sysctl for system performance tuning. Configure network, memory, and security settings safely with temporary and permanent changes.

Prerequisites

  • Root or sudo access
  • Basic command line knowledge
  • Understanding of system administration concepts

What this solves

The Linux kernel exposes hundreds of tunable parameters through the sysctl interface, allowing you to optimize system behavior for specific workloads. You need this when running high-traffic web servers, databases, or applications that require specific network buffer sizes, memory management settings, or security configurations. This tutorial shows you how to safely view current parameters, test changes temporarily, and make permanent optimizations.

Understanding sysctl and kernel parameters

The sysctl (system control) interface provides runtime access to kernel parameters stored in the /proc/sys virtual filesystem. These parameters control everything from network stack behavior to memory management policies. Parameters are organized in a hierarchical structure using dots as separators, like net.core.rmem_max or vm.swappiness.

Changes made with the sysctl command take effect immediately but are lost on reboot unless saved to configuration files. This makes it safe to test optimizations before committing them permanently.

Step-by-step configuration

Install sysctl tools

Most distributions include sysctl by default, but ensure you have the complete procps package for all utilities.

sudo apt update
sudo apt install -y procps
sudo dnf install -y procps-ng

View all current kernel parameters

Display all available sysctl parameters and their current values. This shows you the complete system state.

sysctl -a | head -20
sysctl -a | wc -l

Search for specific parameters

Find parameters related to specific subsystems like networking, memory management, or security.

# Network-related parameters
sysctl -a | grep -i net.core

# Memory management parameters
sysctl -a | grep -i vm

# Security parameters
sysctl -a | grep -i kernel

View individual parameter values

Check specific kernel parameters to understand current system configuration before making changes.

# View network buffer sizes
sysctl net.core.rmem_max
sysctl net.core.wmem_max

# View memory management settings
sysctl vm.swappiness
sysctl vm.dirty_ratio

# View security settings
sysctl kernel.dmesg_restrict

Make temporary parameter changes

Test kernel parameter changes temporarily before making them permanent. These changes take effect immediately but are lost on reboot.

# Increase network receive buffer size
sudo sysctl net.core.rmem_max=134217728

# Adjust memory swappiness (lower = less swap usage)
sudo sysctl vm.swappiness=10

# Enable IP forwarding for routing
sudo sysctl net.ipv4.ip_forward=1

Verify temporary changes

Confirm your temporary changes are active and test application behavior before making changes permanent.

# Verify the changes took effect
sysctl net.core.rmem_max
sysctl vm.swappiness
sysctl net.ipv4.ip_forward

Make permanent changes with sysctl.conf

Add permanent kernel parameter changes to the main sysctl configuration file. These settings are applied at boot time.

sudo cp /etc/sysctl.conf /etc/sysctl.conf.backup
# Network optimizations
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.core.rmem_default = 65536
net.core.wmem_default = 65536

# Memory management
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5

# Security settings
kernel.dmesg_restrict = 1
net.ipv4.conf.default.rp_filter = 1

Create modular configurations in sysctl.d

Use the /etc/sysctl.d/ directory to organize settings by purpose. Files are processed in lexicographical order.

sudo mkdir -p /etc/sysctl.d
# High-performance network settings
net.core.netdev_max_backlog = 5000
net.core.somaxconn = 1024
net.ipv4.tcp_rmem = 4096 87380 134217728
net.ipv4.tcp_wmem = 4096 65536 134217728
net.ipv4.tcp_congestion_control = bbr
# Security hardening
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0

Apply permanent changes

Load the new configuration without rebooting to test your permanent settings.

# Reload all sysctl settings
sudo sysctl -p

# Reload specific configuration file
sudo sysctl -p /etc/sysctl.d/99-network-tuning.conf

# Reload all files in sysctl.d
sudo sysctl --system

Common kernel parameters for optimization

Web server optimization

These parameters improve performance for high-traffic web servers and applications handling many concurrent connections.

# Increase connection queue size
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000

# TCP buffer optimization
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_rmem = 4096 87380 134217728
net.ipv4.tcp_wmem = 4096 65536 134217728

# Connection handling
net.ipv4.tcp_max_syn_backlog = 30000
net.ipv4.tcp_max_tw_buckets = 2000000
net.ipv4.tcp_fin_timeout = 10

Database server optimization

Parameters optimized for database workloads with large memory requirements and specific I/O patterns.

# Memory management for databases
vm.swappiness = 1
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.overcommit_memory = 2
vm.overcommit_ratio = 80

# Shared memory settings
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
kernel.shmmni = 4096

# Semaphore settings
kernel.sem = 250 32000 100 128

Security hardening parameters

Essential security settings to protect against common network attacks and information disclosure.

# Kernel security
kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
kernel.yama.ptrace_scope = 1

# Network security
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0

# IP spoofing protection
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# Ignore ping requests
net.ipv4.icmp_echo_ignore_all = 1

Verify your setup

# Verify sysctl is working
sysctl --version

# Check that settings are loaded
sysctl -a | grep -E "(net.core.somaxconn|vm.swappiness|kernel.dmesg_restrict)"

# Verify configuration files are valid
sudo sysctl -p /etc/sysctl.conf
sudo sysctl --system

# Test specific parameters
sysctl net.core.rmem_max
sysctl vm.dirty_ratio

Troubleshoot sysctl permission errors

Handle read-only parameters

Some kernel parameters are read-only and cannot be modified. These typically reflect hardware capabilities or kernel build options.

# Check if parameter is writable
ls -la /proc/sys/net/core/rmem_max

# Read-only parameters show this error
</code><h2><code>sysctl: setting key "kernel.osrelease": Read-only file system</code></h2></pre>
</div>

<div class="step">
### Fix permission denied errors
<p>Most sysctl modifications require root privileges. Always use sudo when making changes.</p>
<pre class="terminal"><code># Wrong - will fail with permission denied
sysctl net.core.rmem_max=134217728

# Correct - use sudo
sudo sysctl net.core.rmem_max=134217728

Validate configuration syntax

Test configuration files for syntax errors before applying them system-wide.

# Test configuration file syntax
sudo sysctl -p /etc/sysctl.d/99-network-tuning.conf

# Check for typos in parameter names
sudo sysctl net.core.nonexistent 2>&1 | grep "No such file"

Best practices and safety guidelines

Warning: Always test kernel parameter changes in a non-production environment first. Some parameters can severely impact system stability or security if set incorrectly.
  • Start with temporary changes: Use sysctl -w parameter=value to test before making permanent changes
  • Document your changes: Add comments to configuration files explaining why each parameter was modified
  • Monitor system behavior: Check application performance and system logs after making changes
  • Use modular configuration: Organize settings in /etc/sysctl.d/ by purpose for easier management
  • Keep backups: Always backup /etc/sysctl.conf before making changes
  • Understand the parameters: Read kernel documentation before modifying critical settings

When optimizing for specific applications, consider how they interact with other system components. For example, increasing network buffers may require adjusting application-specific settings like process limits and resource monitoring.

For containerized environments, kernel parameters affect all containers on the host. Consider using memory cgroups for container-specific resource control instead of global kernel tuning.

Common issues

SymptomCauseFix
Permission denied when setting parameterMissing sudo privilegesUse sudo sysctl parameter=value
Read-only file system errorParameter is read-onlyCheck ls -la /proc/sys/path for write permissions
No such file or directoryParameter name typo or not availableUse sysctl -a | grep keyword to find correct name
Settings lost after rebootNot saved to configuration fileAdd to /etc/sysctl.conf or /etc/sysctl.d/
Invalid argument errorValue outside acceptable rangeCheck kernel documentation for valid ranges
System becomes unstableInappropriate parameter valuesReboot to restore defaults, then adjust gradually

Next steps

Running this in production?

Want this handled for you? This works for a single server. When you run multiple environments or need this available 24/7, keeping it healthy is a different job. See how we run infrastructure like this for European teams.

Automated install script

Run this to automate the entire setup

Wil je dit niet zelf beheren?

Wij beheren infrastructuur voor bedrijven die afhankelijk zijn van uptime. Volledig beheerd, met één vast aanspreekpunt dat je omgeving kent.

U krijgt één vast aanspreekpunt dat uw omgeving kent

Rotterdam 12:56 · bereikbaar in een bericht, geen ticketformulier