以下是20个实用的Linux服务器性能调优技巧,涵盖系统配置、内核参数、资源管理等方面,帮助提升服务器响应速度和资源利用率:
# 保持系统最新
sudo apt update && sudo apt upgrade -y # Debian/Ubuntu
sudo yum update -y # RHEL/CentOS
编辑 /etc/sysctl.conf
:
# 提升网络性能
net.core.somaxconn = 4096 # 提高TCP连接队列
net.ipv4.tcp_tw_reuse = 1 # 快速回收TIME_WAIT连接
net.ipv4.tcp_fin_timeout = 30 # 减少FIN超时
# 内存管理
vm.swappiness = 10 # 减少交换分区使用
vm.dirty_ratio = 60 # 脏页写入阈值
vm.dirty_background_ratio = 5 # 后台脏页刷新阈值
# 文件系统
fs.file-max = 1000000 # 增加最大文件描述符
应用修改:sudo sysctl -p
ext4
或xfs
文件系统,挂载时启用优化选项:
bash
# /etc/fstab 示例
UUID=xxx / ext4 defaults,noatime,nodiratime,data=writeback 0 1
noatime
:禁止记录访问时间data=writeback
:提高写入性能(需权衡数据安全)# 查看当前调度器
cat /sys/block/sda/queue/scheduler
# 设置为deadline或kyber(SSD推荐)
echo "deadline" | sudo tee /sys/block/sda/queue/scheduler
kyber
或none
deadline
# 查看所有服务
systemctl list-unit-files --type=service
# 禁用打印服务(若无打印机)
sudo systemctl disable cups
cgroups
或systemd
限制资源:
ini
# /etc/systemd/system/myapp.service
[Service]
MemoryLimit=2G
CPUQuota=80%
# /etc/sysctl.conf
net.ipv4.tcp_slow_start_after_idle = 0 # 禁用慢启动
net.ipv4.tcp_window_scaling = 1 # 启用窗口缩放
net.core.rmem_max = 16777216 # 增大接收缓冲区
# 修改全局限制(/etc/security/limits.conf)
* soft nofile 65535
* hard nofile 65535
# 使用DNS缓存(如systemd-resolved)
sudo systemctl enable systemd-resolved
# 检查状态
cat /sys/kernel/mm/transparent_hugepage/enabled
# 建议设置为"madvise"
echo "madvise" | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
# /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
# 使用logrotate自动轮转日志
sudo logrotate -f /etc/logrotate.conf
bash
htop # 交互式进程查看
iotop -o # 高磁盘IO进程
iftop # 网络流量监控
# 设置为性能模式
sudo cpupower frequency-set -g performance
# 定期整理
sudo e4defrag / # ext4文件系统
# my.cnf 关键参数
innodb_buffer_pool_size = 4G # 分配足够内存
innodb_flush_log_at_trx_commit = 2 # 平衡性能与安全
nginx
worker_processes auto;
worker_connections 4096;
keepalive_timeout 30;
gzip on;
# /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
# 加载模块
echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# 清理旧内核/缓存
sudo apt autoremove -y # Debian/Ubuntu
sudo yum autoremove # RHEL/CentOS
sar
、vmstat
等工具)通过组合这些技巧,可显著提升服务器的吞吐量、响应速度和稳定性。