温馨提示×

怎么优化CentOS性能

小樊
46
2026-08-19 06:28:08
栏目: 智能运维

优化 CentOS 性能可以从 系统配置、内核参数、服务管理、磁盘/内存/网络、应用层 等多个层面入手。下面给你一套实用、可落地的优化清单,适用于 CentOS 7 / 8 / Stream,服务器和虚拟机都适用。


一、系统基础优化

1. 关闭不必要的服务

systemctl list-unit-files --type=service | grep enabled

常见可关闭:

systemctl disable firewalld   # 如有硬件防火墙
systemctl disable postfix
systemctl disable cups

2. 使用高性能 CPU 调度

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

1. 网络性能优化

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

2. 文件与连接数

fs.file-max = 1000000
vm.swappiness = 10
vm.dirty_ratio = 10
vm.dirty_background_ratio = 5

生效:

sysctl -p

三、文件描述符限制

1. 修改 limits

vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

2. systemd 服务(重要)

vi /etc/systemd/system.conf
DefaultLimitNOFILE=65535

重启生效。


四、磁盘与 IO 优化

1. 磁盘调度算法

查看:

cat /sys/block/sda/queue/scheduler

SSD 推荐:

echo deadline > /sys/block/sda/queue/scheduler

2. 关闭 atime

vi /etc/fstab
/dev/sda1  /  ext4  defaults,noatime,nodiratime  0 0

五、内存优化

1. 减少 swap 使用

echo "vm.swappiness = 10" >> /etc/sysctl.conf

2. 大内存服务器

echo always > /sys/kernel/mm/transparent_hugepage/enabled

六、网络优化(高并发必做)

1. 调整连接追踪

net.netfilter.nf_conntrack_max = 262144

2. 防火墙优化

  • 高并发建议 iptables + 硬件防火墙
  • 或关闭 firewalld:
systemctl disable firewalld

七、系统时间 & 日志

1. 使用 NTP

yum install -y chrony
systemctl enable chronyd
systemctl start chronyd

2. 日志优化

journalctl --vacuum-time=7d

八、应用层优化(通用)

1. SSH 优化

vi /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
systemctl restart sshd

2. 禁用 IPv6(如不用)

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

如果你愿意,可以告诉我:

  • ✅ CentOS 版本
  • ✅ 物理机 / 虚拟机
  • ✅ 主要用途(Web / DB / Docker / K8s)

我可以给你 定制专属优化方案

0