优化 CentOS 性能可以从 系统配置、内核参数、服务管理、磁盘/内存/网络、应用层 等多个层面入手。下面给你一套实用、可落地的优化清单,适用于 CentOS 7 / 8 / Stream,服务器和虚拟机都适用。
systemctl list-unit-files --type=service | grep enabled
常见可关闭:
systemctl disable firewalld # 如有硬件防火墙
systemctl disable postfix
systemctl disable cups
yum install -y tuned
systemctl enable tuned
systemctl start tuned
tuned-adm profile throughput-performance
常见 profile:
balanced(默认)throughput-performance(✅推荐服务器)latency-performance(低延迟)编辑:
vi /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 32768
net.ipv4.ip_local_port_range = 10000 65535
fs.file-max = 1000000
vm.swappiness = 10
vm.dirty_ratio = 10
vm.dirty_background_ratio = 5
生效:
sysctl -p
vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
vi /etc/systemd/system.conf
DefaultLimitNOFILE=65535
重启生效。
查看:
cat /sys/block/sda/queue/scheduler
SSD 推荐:
echo deadline > /sys/block/sda/queue/scheduler
vi /etc/fstab
/dev/sda1 / ext4 defaults,noatime,nodiratime 0 0
echo "vm.swappiness = 10" >> /etc/sysctl.conf
echo always > /sys/kernel/mm/transparent_hugepage/enabled
net.netfilter.nf_conntrack_max = 262144
systemctl disable firewalld
yum install -y chrony
systemctl enable chronyd
systemctl start chronyd
journalctl --vacuum-time=7d
vi /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
systemctl restart sshd
grubby --update-kernel=ALL --args="ipv6.disable=1"
yum install -y htop iotop iftop sysstat
常用:
top
htop
iostat -x 1
vmstat 1
| 场景 | 建议 |
|---|---|
| Web 服务器 | tuned throughput + 网络参数 |
| 数据库 | 禁用 swap + 大内存 + IO 优化 |
| 高并发 | 文件句柄 + TCP 参数 |
| 虚拟机 | 半虚拟化驱动 + 关闭无用服务 |
wget https://raw.githubusercontent.com/tobychui/linux-perf-check/main/check.sh
bash check.sh
如果你愿意,可以告诉我:
我可以给你 定制专属优化方案 ✅