How to Optimize Linux Kernel Parameters for Better Performance
This guide explains how to fine‑tune Linux kernel settings—including sysctl network parameters, shared memory limits, local port ranges, and disk scheduler options—using configuration files and command‑line tools to improve system stability and throughput.
Kernel Parameter Configuration
All kernel tunables are stored in /etc/sysctl.conf. Add each parameter on a separate line, then apply the changes with sysctl -p. Use sysctl -a to list the current values.
Typical error when a key is misspelled: <code># sysctl -p net.ipv4.ip_forward = 1 sysctl: cannot stat /proc/sys/net/core/somaxconn1: No such file or directory</code>
Network‑Related Tunables
net.core.somaxconn=65535– maximum length of the TCP listen queue per port. net.core.netdev_max_backlog=65535 – upper limit of packets queued when the NIC receives data faster than the kernel can process it. net.ipv4.tcp_max_syn_backlog=65535 – maximum size of the SYN backlog; very large values can increase exposure to SYN‑flood attacks. net.ipv4.tcp_fin_timeout=10 – seconds a socket remains in FIN‑WAIT‑2 after the local side initiates closure. net.ipv4.tcp_tw_reuse=1 – enables reuse of TIME‑WAIT sockets for new connections (default 0, disabled). net.ipv4.tcp_tw_recycle=1 – enables fast recycling of TIME‑WAIT sockets (default 0, disabled).
Typical Tuning Template for 8 GB–16 GB RAM Systems
fs.file-max: 1048576 → 9999999– total number of file descriptors the system can allocate. fs.nr_open: 1635590 → 1635590 – maximum file descriptors per process. net.core.rmem_default: 124928 → 262144 – default TCP receive buffer. net.core.wmem_default: 124928 → 262144 – default TCP send buffer. net.core.rmem_max: 124928 → 8388608 – maximum TCP receive buffer. net.core.wmem_max: 124928 → 8388608 – maximum TCP send buffer. net.ipv4.tcp_wmem: 4096 16384 4194304 → 4096 16384 8388608 – TCP send‑buffer range (min, default, max). net.ipv4.tcp_rmem: 4096 87380 4194304 → 4096 87380 8388608 – TCP receive‑buffer range.
net.ipv4.tcp_mem: 384657 512877 769314 → 384657 512877 3057792– TCP memory usage thresholds (low, pressure, high). net.core.netdev_max_backlog: 1000 → 5000 (or 10000 for heavy loads) – max packets queued per NIC. net.core.optmem_max: 20480 → 81920 – maximum socket option memory. net.core.somaxconn: 128 → 2048 – global maximum listen queue length. net.ipv4.tcp_fin_timeout: 60 → 30 – FIN‑WAIT‑2 timeout in seconds. net.ipv4.tcp_max_syn_backlog: 1024 → 2048 – max pending SYN requests. net.ipv4.tcp_tw_reuse: 0 → 1 – enable TIME‑WAIT reuse. net.ipv4.tcp_keepalive_time: 7200 → 900 – idle time before keep‑alive probes. net.ipv4.tcp_keepalive_intvl: 75 → 30 – interval between keep‑alive probes. net.ipv4.tcp_keepalive_probes: 9 → 3 – number of probes before giving up.
Note: Larger values are not always better; they must be balanced against hardware capacity and the needs of other services.
Local Port Range
When the system exhausts the pool of local ports, new connections fail with “Can’t assign requested address”. Adjust the range in /etc/sysctl.conf:
# View current range
cat /proc/sys/net/ipv4/ip_local_port_range
# Typical default: 32768 61000
# Change the range
net.ipv4.ip_local_port_range = 1024 65000
# Apply changes
sysctl -pMinimum value is 1024; ports below this are reserved for system services. Increase the lower bound only if your applications use high‑numbered ports.
Shared Memory Settings
kernel.shmmax=4294967295– maximum size of a single shared memory segment (bytes). Should be at least the size of the database SGA and preferably half of physical RAM. kernel.shmmni=4096 – maximum number of shared memory segments system‑wide. kernel.shmall=2097152 – total number of pages that can be allocated for shared memory (default suitable for up to 8 GB SGA).
Virtual Memory Swappiness
Set vm.swappiness=0 to instruct the kernel to avoid swapping until memory is completely exhausted.
Risk: reduced overall performance and higher likelihood of OOM kills under memory pressure.
Increasing File Descriptor Limits
Edit /etc/security/limits.conf to raise the number of open files for all users:
* soft nofile 65535
* hard nofile 65535 *– applies to all users. soft – current effective limit. hard – maximum enforceable limit. nofile – maximum number of open file descriptors.
Reboot the system for the changes to take effect.
Disk Scheduler Selection
Scheduler files are located at /sys/block/<em>devname</em>/queue/scheduler. Common algorithms:
noop – simple FIFO, best for SSDs and embedded systems.
deadline – guarantees a maximum service time; suitable for database workloads.
anticipatory – waits briefly after a read to batch subsequent writes; improves write‑heavy workloads but degrades database performance.
cfq – completely fair queueing algorithm.
Kernel Parameter Paths
/proc/sys/abi/*– binary compatibility support. /proc/sys/fs/* – file‑system limits and quotas. /proc/sys/kernel/* – hot‑plug, shared memory, PID limits, syslog debug level. /proc/sys/net/* – network stack tuning for IPv4/IPv6. /proc/sys/vm/* – cache and buffer management.
Reference: https://www.cnblogs.com/lovesKey/p/11509683.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
