Linux Kernel Tuning with sysctl on VPS
sysctl is a Linux utility that lets you read and modify kernel parameters at runtime without rebooting. These parameters live under /proc/sys/ and control the network stack, memory management, and IPC — all of which directly affect the performance of a VPS running web servers, databases, or containers.
How sysctl Works
Linux exposes hundreds of tunable kernel parameters via the virtual filesystem at /proc/sys/. The sysctl command is the standard interface for reading and writing these values. Changes made with sysctl -w are temporary and lost on reboot. To persist them, write the settings to /etc/sysctl.conf or a file inside /etc/sysctl.d/.
Checking Current sysctl Values
# List all parameters
sysctl -a
# Search for specific parameters
sysctl -a | grep net.ipv4.tcp
sysctl vm.swappiness
# Show current value
sysctl kernel.shmmax
Network Stack Tuning (net.ipv4)
These parameters affect throughput, latency, and the number of simultaneous connections your server can handle.
TCP Buffer Sizes
Increasing the TCP read/write buffers improves throughput on high-bandwidth connections:
# Check defaults
sysctl net.ipv4.tcp_rmem
sysctl net.ipv4.tcp_wmem
# Recommended values (min default max in bytes)
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
TCP Backlog and Connection Queue
# Maximum queued connections waiting to be accepted (default 128)
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 8192
# Increase network device backlog
net.core.netdev_max_backlog = 5000
TCP TIME_WAIT and Reuse
# Reduce wait time before a port can be reused
net.ipv4.tcp_fin_timeout = 30
# Allow reuse of TIME_WAIT sockets
net.ipv4.tcp_tw_reuse = 1
# Enable SYN flood protection
net.ipv4.tcp_syncookies = 1
IP Forwarding for Containers and VPN
# Enable IP forwarding (required for Docker, WireGuard, OpenVPN)
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
Memory Management Tuning (vm.*)
The vm namespace controls virtual memory behavior — when to swap, how aggressively to cache, and how dirty pages are flushed to disk.
vm.swappiness — Swap Aggressiveness
The default is 60, meaning the kernel starts swapping when about 40% of RAM is used. For a VPS with enough RAM, lowering this value reduces disk I/O:
# 10 = only swap when truly necessary
vm.swappiness = 10
# 0 = avoid swapping almost entirely (risk: OOM killer!)
# Recommended: 1–10 for general web servers
vm.vfs_cache_pressure
# Lower value = cache directory entries longer
# Good for servers opening many files
vm.vfs_cache_pressure = 50
vm.overcommit_memory
# 0 = kernel decides (default)
# 1 = always allow overcommit (recommended for Redis)
# 2 = never overcommit beyond vm.overcommit_ratio
vm.overcommit_memory = 1
Dirty Page Ratios
# Percentage of RAM held as dirty pages before background flush
vm.dirty_background_ratio = 5
# Percentage before blocking writes
vm.dirty_ratio = 10
Kernel Shared Memory (kernel.*)
These settings are critical for PostgreSQL and other databases that use large shared memory segments.
kernel.shmmax — Maximum Shared Memory Segment
# Check current value
sysctl kernel.shmmax
# Set to 2 GB (in bytes)
kernel.shmmax = 2147483648
# Example: 8 GB VPS, set shmmax to 50% = 4 GB
kernel.shmmax = 4294967296
kernel.shmall — Total Shared Memory Pages
# shmall = shmmax / PAGE_SIZE (usually 4096 bytes)
# Example: shmmax = 4 GB → shmall = 4294967296 / 4096 = 1048576
kernel.shmall = 1048576
kernel.shmmni and Message Queues
kernel.shmmni = 4096
kernel.msgmax = 65536
kernel.msgmnb = 65536
Persistent Configuration with sysctl.d
Create a dedicated file under /etc/sysctl.d/ rather than editing /etc/sysctl.conf directly:
cat > /etc/sysctl.d/99-vps-tuning.conf << 'EOF'
# Network tuning
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.core.netdev_max_backlog = 5000
# Memory tuning
vm.swappiness = 10
vm.vfs_cache_pressure = 50
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
# Kernel shared memory
kernel.shmmax = 2147483648
kernel.shmall = 524288
EOF
# Apply immediately
sysctl --system
Verifying sysctl Changes
1. Check That Values Are Applied
sysctl net.core.somaxconn
# Expected: net.core.somaxconn = 65535
sysctl vm.swappiness
# Expected: vm.swappiness = 10
2. Reload from Config File
sysctl -p /etc/sysctl.d/99-vps-tuning.conf
3. Verify Values Persist After Reboot
sysctl --system --dry-run 2>&1 | grep "99-vps-tuning"
4. Spot-Check All Custom Settings
sysctl -a 2>/dev/null | grep -E "swappiness|somaxconn|shmmax|tw_reuse"
5. Monitor Network Socket State
# Socket statistics summary
ss -s
# Count TIME_WAIT sockets
ss -tan | grep TIME-WAIT | wc -l
# Network buffer usage
netstat -s | grep -E "receive|send|buffer"
Common Problems and Fixes
sysctl: setting key: Read-only file system
This happens when the parameter cannot be changed at runtime, or when running inside a container without sufficient privileges:
# Check if the parameter is writable
ls -la /proc/sys/kernel/shmmax
# For Docker, pass sysctl at run time
docker run --sysctl net.core.somaxconn=65535 myimage
sysctl: cannot stat /proc/sys/...: No such file or directory
The parameter does not exist in the running kernel version, or the required module is not loaded:
# Check kernel version
uname -r
# Count available parameters
sysctl -a 2>/dev/null | grep -c ""
sysctl Values Lost After Reboot
The systemd-sysctl service did not apply the config file on boot:
# Check service status
systemctl status systemd-sysctl
# Manually reload all sysctl files
sysctl --system
# Validate config file syntax
sysctl -p /etc/sysctl.d/99-vps-tuning.conf
OOM Killer Firing After Lowering swappiness
When swap is avoided and RAM fills up, the kernel kills processes instead of swapping:
# Check for OOM kill events
dmesg | grep -i "oom\|killed process"
journalctl -k | grep -i oom
# Fix: add more swap, add more RAM, or raise swappiness to 20–30
PostgreSQL: could not create shared memory segment
kernel.shmmax is too small for the shared_buffers value configured in PostgreSQL:
# Check PostgreSQL shared_buffers
grep shared_buffers /etc/postgresql/*/main/postgresql.conf
# Set shmmax larger than shared_buffers
sysctl -w kernel.shmmax=2147483648
echo "kernel.shmmax = 2147483648" >> /etc/sysctl.conf
Frequently Asked Questions
Do I need to reboot for sysctl changes to take effect?
No. Using sysctl -w parameter=value or sysctl -p applies changes immediately. However, writing the values to /etc/sysctl.conf or /etc/sysctl.d/ is required to persist them across reboots.
Can I tune sysctl on an OpenVZ or LXC VPS?
It depends on the permissions your provider grants. KVM and Xen VPS instances have full kernel access. OpenVZ and LXC containers may have certain parameters locked by the host node. If you receive a "Read-only file system" error, your provider does not allow that parameter to be changed.
What sysctl settings are recommended for a WordPress VPS?
For a WordPress/PHP-FPM + Nginx stack: vm.swappiness = 10, net.core.somaxconn = 1024, net.ipv4.tcp_fin_timeout = 30, and net.ipv4.tcp_tw_reuse = 1. These settings reduce latency and improve concurrent connection handling without sacrificing stability.