Skip to main content

Managing Kernel Parameters

1. Viewing Kernel Parameters

Command Description Example
sysctl View or change kernel parameters at runtime sysctl net.ipv4.ip_forward
/proc/sys/ Virtual filesystem to view kernel parameters cat /proc/sys/net/ipv4/ip_forward
sysctl -a List all kernel parameters sysctl -a | grep net.ipv4.ip_forward

Example: View IP Forwarding Setting

sysctl net.ipv4.ip_forward
cat /proc/sys/net/ipv4/ip_forward
Note: Values are typically 0 (disabled) or 1 (enabled) for boolean settings.

2. Displaying Specific Parameter

Parameter Value Description
net.ipv4.ip_forward 0 Enables or disables IP forwarding
fs.file-max 1048576 Maximum number of file handles
vm.swappiness 60 Controls tendency to swap memory pages

Example: Displaying a specific kernel parameter

cat /proc/sys/net/ipv4/ip_forward

3. Setting Temporary Parameters

Parameter Value Description
net.core.rmem_max 134217728 Maximum receive socket buffer size in bytes
net.core.wmem_max 134217728 Maximum send socket buffer size in bytes
net.ipv4.tcp_rmem 4096 87380 134217728 Min, default, and max TCP receive buffer sizes
net.ipv4.tcp_wmem 4096 65536 134217728 Min, default, and max TCP send buffer sizes

Example: Temporarily set a kernel parameter

# Set the maximum receive buffer size to 128MB
echo 134217728 > /proc/sys/net/core/rmem_max

# Verify the change
cat /proc/sys/net/core/rmem_max
Note: Changes made using this method are lost after a reboot.

4. Setting Permanent Parameters

Parameter Value Description File Location
net.ipv4.ip_forward 0 Enables IP forwarding /etc/sysctl.conf
fs.file-max 1048576 Maximum number of file handles /etc/sysctl.conf
vm.swappiness 1 Controls swap usage /etc/sysctl.conf

Example: Setting a permanent parameter

# Edit /etc/sysctl.conf
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

# Apply changes
sysctl -p
Note: Changes to /etc/sysctl.conf take effect after running sysctl -p or rebooting the system.

5. Reloading sysctl Configuration

Use the sysctl command to reload kernel parameters from configuration files without rebooting.

Command Description Notes
sysctl -p Reload all settings from /etc/sysctl.conf Applies changes to running kernel
sysctl -p /etc/sysctl.d/custom.conf Load settings from a specific file Useful for custom or modular configurations
sysctl -p --system Reload all system-wide settings Applies changes from /etc/sysctl.d/ directory

Example: Reloading sysctl Configuration

# Reload all sysctl settings from /etc/sysctl.conf
sudo sysctl -p

# Load specific configuration file
sudo sysctl -p /etc/sysctl.d/custom.conf

# Apply all system-wide changes
sudo sysctl -p --system
Note: Changes made with sysctl -p are applied immediately but not persistent across reboots unless saved to /etc/sysctl.conf or a file in /etc/sysctl.d/.
Warning: Incorrectly setting kernel parameters can lead to system instability or performance degradation. Always verify changes before applying them in production environments.

6. Tuning Network Parameters

Parameter Default Value Description Notes
net.core.rmem_max 134217728 Maximum receive socket buffer size in bytes Increases max TCP receive buffer
net.core.wmem_max 134217728 Maximum send socket buffer size in bytes Increases max TCP send buffer
net.ipv4.tcp_rmem 4096 87380 134217728 Min, default, and max TCP receive buffer sizes Controls dynamic buffer sizing
net.ipv4.tcp_wmem 4096 65536 134217728 Min, default, and max TCP send buffer sizes Controls dynamic buffer sizing
net.ipv4.tcp_congestion_control cubic Default TCP congestion control algorithm Can be changed to reno, bbr, etc.
net.ipv4.ip_forward 0 Enables IP forwarding Set to 1 for routing/firewall setups
net.core.somaxconn 128 Maximum number of connections in backlog queue Increase for high-traffic servers
net.ipv4.tcp_max_syn_backlog 1024 Maximum SYN backlog for new connections Increases connection handling capacity

Example: Setting network parameters via sysctl

sudo sysctl -w net.core.rmem_max=134217728
sudo sysctl -w net.core.wmem_max=134217728
sudo sysctl -w net.ipv4.tcp_rmem="4096 87380 134217728"
sudo sysctl -w net.ipv4.tcp_wmem="4096 65536 134217728"

Example: Making changes persistent

echo 'net.core.rmem_max = 134217728' | sudo tee -a /etc/sysctl.conf
echo 'net.core.wmem_max = 134217728' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_rmem = 4096 87380 134217728' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_wmem = 4096 65536 134217728' | sudo tee -a /etc/sysctl.conf
Note: Changes to net.ipv4.tcp_congestion_control may require a reboot or module reload to take effect fully.
Warning: Setting buffer sizes too high can lead to memory exhaustion or system instability. Always test in a controlled environment.

7. Tuning Memory Parameters

Parameter Description Default Value Notes
vm.swappiness Controls the tendency of the kernel to swap memory pages 60 Lower values (0-10) mean less swapping; higher (60-100) more aggressive
vm.overcommit_memory Controls how the kernel handles memory overcommitment 0 0 = heuristic overcommit; 1 = always overcommit; 2 = never overcommit
vm.overcommit_ratio Percentage of RAM to consider for overcommitment when overcommit_memory=2 50 Only used when overcommit_memory=2
vm.min_free_kbytes Minimum free kilobytes to keep available for kernel operations 1024 Should be set to at least 1% of total RAM for large systems
vm.dirty_ratio Maximum percentage of system memory that can be filled with "dirty" pages before sync 10 Higher values increase performance but risk data loss during crash
vm.dirty_background_ratio Percentage of system memory at which background flushing starts 5 Should be less than dirty_ratio

Example: Setting swappiness to 10

sudo sysctl vm.swappiness=10

Example: Making swappiness persistent

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Note: Changes to vm.swappiness affect system responsiveness under memory pressure. Lower values reduce swapping but increase the chance of OOM (Out-of-Memory) conditions.

8. Configuring TCP/IP Stack

Parameter Default Value Description Notes
net.ipv4.tcp_syncookies 1 Enables SYN cookies to protect against SYN flood attacks Set to 0 to disable
net.ipv4.tcp_max_syn_backlog 1024 Maximum number of queued connection requests Higher values allow more connections to queue
net.ipv4.tcp_fin_timeout 60 Time to wait for a FIN packet before closing the connection Reducing this can free up resources faster
net.ipv4.tcp_keepalive_time 7200 Time (seconds) before keepalive probes are sent Higher values reduce network traffic
net.ipv4.tcp_tw_reuse 0 Allow reuse of TIME_WAIT sockets for new connections Set to 1 in modern systems to improve performance
net.ipv4.ip_forward 0 Enables IP forwarding between interfaces Set to 1 to enable routing

Example: Modifying TCP/IP Parameters

sudo sysctl -w net.ipv4.tcp_syncookies=1
sudo sysctl -w net.ipv4.tcp_max_syn_backlog=2048
sudo sysctl -w net.ipv4.tcp_fin_timeout=30

Example: Making Changes Persistent

echo 'net.ipv4.tcp_syncookies = 1' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_max_syn_backlog = 2048' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_fin_timeout = 30' | sudo tee -a /etc/sysctl.conf
Note: Changes made with sysctl are temporary and will be lost after reboot unless added to /etc/sysctl.conf.

9. Loading Modules at Boot

Method Description Configuration File Notes
/etc/modules Simple list of module names to load at boot /etc/modules Each module name on a new line. No parameters supported.
/etc/modules-load.d/ Directory for module load configuration files /etc/modules-load.d/*.conf Each file contains one module name per line. Supports parameters via options.
modprobe.d Configuration directory for module options and dependencies /etc/modprobe.d/*.conf Allows setting module options and aliases. Can be used to configure parameters for modules.

Example: Loading a module at boot using /etc/modules

# Add module name to /etc/modules
echo "vboxdrv" >> /etc/modules

Example: Loading a module with options using /etc/modules-load.d/

# Create a config file in /etc/modules-load.d/
echo "vboxdrv" > /etc/modules-load.d/virtualbox.conf

Example: Setting module options using /etc/modprobe.d/

# Create a config file in /etc/modprobe.d/
echo "options vboxdrv ignore_localhost=1" > /etc/modprobe.d/virtualbox.conf
Note: The /etc/modules-load.d/ method is preferred in modern systems as it allows more granular control and better integration with systemd.

10. Understanding /proc and /sys Filesystems

Filesystem Description Use Case
/proc Virtual filesystem providing runtime system information Monitor processes, kernel parameters, and hardware stats
/sys Virtual filesystem exposing kernel data structures and device drivers Configure hardware, manage drivers, inspect system state

Example: View kernel parameters via /proc

cat /proc/sys/net/core/somaxconn

Example: Modify kernel parameter via /sys

echo 1024 > /sys/module/ipv4/parameters/ip_unprivileged_port_start
Note: Changes made to /sys are typically ephemeral and reset on reboot unless persisted via configuration files or systemd services.

Was this article helpful?