Back to Blog
Lesson 50 of the Linux: Linux Command Line for Developers course
LinuxSeptember 7, 20264 min read

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.

linuxperformancekernelsysctltuningoptimization
A person reads 'Python for Unix and Linux System Administration' indoors.

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:

Bash
sudo 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:

Bash
sudo sysctl -a | grep net.ipv4.tcp

Applying Changes

To change a parameter temporarily (it will reset on reboot), use the -w flag:

Bash
sudo 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:

Bash
ulimit -a

To modify these limits permanently, edit /etc/security/limits.conf. Add entries for your web server user (e.g., www-data):

TEXT
www-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:

Bash
free -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:

Bash
grep 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

  1. Inspect: Run sysctl net.core.somaxconn to see your current connection queue limit.
  2. Modify: Create a file /etc/sysctl.d/10-custom.conf and set net.core.somaxconn = 1024. Apply it with sysctl -p.
  3. Verify: Run ulimit -n to check your current open file descriptor limit. If it’s low, add a configuration to limits.conf to increase it to 4096.

Common Pitfalls

Close-up of a triangular warning sign indicating a slippery surface, fixed to a wooden post.

  • Over-tuning: Changing kernel parameters without understanding their impact can destabilize your system. Always benchmark before and after.
  • Ignoring Persistence: Remember that sysctl -w is 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

Team members presenting a project in a modern office setting with a focus on collaboration.

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.

Similar Posts