Optimizing your Linux system unlocks hidden potential, whether you’re running a web server, database, or workstation. Here’s a detailed guide to key tuning areas:
1. Kernel Parameter Tuning via /etc/sysctl.conf
- Increase open file limits:
fs.file-max = 1000000 # Max system-wide open files
- Optimize memory usage:
vm.swappiness = 10 # Reduce swapping tendency (0-100) vm.vfs_cache_pressure = 50 # Favor inode/dentry cache retention
- Network performance:
net.core.somaxconn = 4096 # Increase connection backlog net.ipv4.tcp_fastopen = 3 # Enable TCP Fast Open (TFO)
Apply changes:
sudo sysctl -p
2. I/O Scheduler Selection
Choose a scheduler matching your workload:
- SSDs/NVMe:
kyber
ornone
(noop) for low latency. - HDDs:
mq-deadline
for balanced throughput. - Database/VMs:
bfq
(Budget Fair Queueing) for fairness.# Set scheduler for NVMe drive echo "kyber" | sudo tee /sys/block/nvme0n1/queue/scheduler
3. Filesystem Optimization
- Mount options in
/etc/fstab
:# For XFS/SSD: noatime, discard (TRIM) UUID=... /mountpoint xfs noatime,discard 0 0 # For ext4: data=writeback for faster writes (trade reliability)
- Reserve disk space: Reduce reserved blocks (default: 5%) for large disks:
sudo tune2fs -m 1 /dev/sda1 # Set to 1%
4. Memory Management: Transparent Huge Pages (THP)
Disable THP for database workloads (e.g., MongoDB, PostgreSQL) to avoid latency spikes:
echo "never" | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
Permanent option: Add transparent_hugepage=never
to kernel boot parameters.
5. Network Tuning
- Increase buffer sizes:
net.core.rmem_max = 16777216 net.core.wmem_max = 16777216
- TCP congestion control: Use
bbr
(Google’s BBR) for high-speed networks:net.ipv4.tcp_congestion_control = bbr
6. Security vs. Performance Tradeoffs
- Spectre/Meltdown mitigations: Disable for performance-critical systems (if risk-acceptable):
Addmitigations=off
to kernel boot parameters. - Limit
sysctl
hardening: Avoid overly restrictive settings likekernel.kptr_restrict=2
on internal systems.
7. User Limits: /etc/security/limits.conf
Prevent resource exhaustion for services (e.g., Nginx):
www-data hard nofile 50000 # Web server user
* soft nproc 65535 # Global process limit
8. CPU Governor
Set CPUs to performance
mode for servers:
sudo apt install cpufrequtils
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
sudo systemctl restart cpufrequtils
Monitoring & Validation Tools
- Check disk I/O:
iotop
,iostat -x 1
- Network stats:
ss -tulpn
,nload
- Memory:
vmstat 1
,htop
- Tuning impact: Use
perf
orsysbench
for benchmarks.
> ⚠️ Critical Advice:
> – Test changes in staging environments first.
> – Monitor system stability/performance after each tweak.
> – Document customizations for recovery/auditing.
Final Tip: Automation tools like Ansible or tuned (for RHEL/CentOS) simplify applying tunings across systems. Start small, measure relentlessly, and tailor settings to YOUR workload’s DNA! 🚀
Need deeper dives? Explore kernel documentation at kernel.org.