System Performance Tuning: A Linux Kernel Optimization Guide
Master Linux system performance tuning. Learn to adjust kernel parameters, manage resource limits, and monitor swap usage to optimize your server.

Previously in this course, we covered advanced text processing with awk to parse system data. In this lesson, we shift from observing data to actively shaping your operating system's behavior through performance tuning.
While modern Linux distributions come with sensible defaults, high-performance web servers often hit artificial "ceilings" during traffic spikes. By tuning the kernel and system limits, you ensure your infrastructure doesn't throttle itself when it should be scaling.
Understanding Kernel Parameters with sysctl
The Linux kernel exposes internal configuration settings through the /proc/sys virtual filesystem. Rather than editing these files directly, we use the sysctl command to view and modify them. These settings control everything from network buffer sizes to how aggressively the kernel reclaims memory.
To list all current kernel parameters, run:
Bashsudo sysctl -a
Since this list is massive, you’ll typically search for specific subsystems. For example, to view network-related settings for optimizing TCP connections:
Bashsudo sysctl -a | grep net.ipv4.tcp
Applying Changes
To change a parameter temporarily (it will reset on reboot), use the -w flag:
Bashsudo sysctl -w net.ipv4.tcp_fin_timeout=30
To make these changes permanent, add them to a configuration file in /etc/sysctl.d/. Create a new file, such as /etc/sysctl.d/99-webserver.conf:
TEXT# Increase the maximum number of open files fs.file-max = 2097152 # Improve TCP performance net.ipv4.tcp_tw_reuse = 1
After saving, apply the changes without rebooting: sudo sysctl -p /etc/sysctl.d/99-webserver.conf.
Adjusting System Limits with limits.conf
Kernel parameters control global OS behavior, but ulimit settings control the resources available to specific users or processes. If your web server process tries to open 5,000 files but the OS limit is 1,024, the application will crash with "Too many open files."
View your current shell limits with:
Bashulimit -a
To modify these limits permanently, edit /etc/security/limits.conf. Add entries for your web server user (e.g., www-data):
TEXTwww-data soft nofile 65535 www-data hard nofile 65535
The "soft" limit is the advisory limit the user can increase up to the "hard" limit. The "hard" limit is the ceiling enforced by the root user.
Monitoring Swap Usage
Swap is disk space used as "overflow" memory when physical RAM is exhausted. While it prevents out-of-memory (OOM) crashes, swapping to disk is significantly slower than RAM. Monitoring this is critical, as high swap activity is usually the first sign of a performance bottleneck, as discussed in analyzing resource bottlenecks.
Use free -h to see swap status:
Bashfree -h
If you see "used" memory in the swap column, your server is under pressure. You can check which processes are using swap by inspecting the /proc filesystem:
Bashgrep VmSwap /proc/[0-9]*/status | sort -k 2 -n -r | head -n 5
This command maps swap usage back to specific process IDs (PIDs).
Hands-on Exercise: Tuning Your Server
- Inspect: Run
sysctl net.core.somaxconnto see your current connection queue limit. - Modify: Create a file
/etc/sysctl.d/10-custom.confand setnet.core.somaxconn = 1024. Apply it withsysctl -p. - Verify: Run
ulimit -nto check your current open file descriptor limit. If it’s low, add a configuration tolimits.confto increase it to 4096.
Common Pitfalls

- Over-tuning: Changing kernel parameters without understanding their impact can destabilize your system. Always benchmark before and after.
- Ignoring Persistence: Remember that
sysctl -wis volatile. Always update files in/etc/sysctl.d/for production environments. - Misinterpreting Swap: A small amount of swap usage is normal for idle background processes. Constant, high swap activity is what indicates a real performance issue.
FAQ
Q: Do I need to reboot after changing sysctl or limits.conf?
A: No. sysctl -p applies kernel changes, and limits.conf takes effect for new sessions or processes. You may need to restart your service (e.g., systemctl restart nginx) for it to pick up the new file descriptor limits.
Q: Can I set these limits per-process?
A: Yes, if you are using systemd, you can use LimitNOFILE=65535 in your service unit file, which is often cleaner than global limits.conf changes.
Recap

Performance tuning is the final layer of hardening your infrastructure. By controlling kernel parameters via sysctl, setting process limits in limits.conf, and keeping a close eye on swap memory, you move from "it works" to "it scales." Understanding these levers allows you to handle high-write databases or heavy web traffic as outlined in high-write database optimization.
Up next: We will automate our safety net by setting up Backup Automation for our web server configuration.
Work with me

Laravel Bug Fixes, Maintenance & Optimization
Stuck on a Laravel bug or a slow app? Fast, reliable fixes, upgrades, and performance tuning from an experienced Laravel engineer.

Custom Email & File Storage System on Cloudflare (Google Workspace Alternative)
Your own private email + file storage suite on your domain — unlimited mailboxes, no per-seat fees. A self-owned Google Workspace alternative for a flat ~$5/month.

