在生产环境下对 CentOS 7.9 进行系统优化配置,是保障系统稳定性、安全性与性能的关键步骤。以下是针对生产环境的全面优化建议,涵盖系统安全、性能调优、内核参数、文件系统、网络配置等方面。
一、系统基础安全优化
1. 更新系统并关闭不必要的服务
# 更新系统
yum update -y
# 关闭不需要的服务(根据实际需求调整)
systemctl disable postfix avahi-daemon bluetooth cups
systemctl stop postfix avahi-daemon bluetooth cups
2. 使用非 root 用户并配置 sudo
useradd admin
passwd admin
usermod -aG wheel admin # 添加到sudo组
编辑 /etc/sudoers(使用 visudo):
admin ALL=(ALL) NOPASSWD:ALL
3. 禁用 root 远程登录
编辑 /etc/ssh/sshd_config:
PermitRootLogin no
AllowUsers admin
重启 SSH:
systemctl restart sshd
4. 配置防火墙(firewalld)
systemctl enable firewalld
systemctl start firewalld
# 开放必要端口(如80、443、自定义服务端口)
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload
5. 安装并配置 fail2ban(防暴力破解)
yum install -y fail2ban
systemctl enable fail2ban
systemctl start fail2ban
配置文件 /etc/fail2ban/jail.local:
[sshd]
enabled = true
maxretry = 3
bantime = 3600
二、系统性能优化
1. 文件系统优化(ext4/xfs)
- 推荐使用 XFS(适合大文件和高并发)
- 挂载选项优化(在
/etc/fstab中):
/dev/sda1 / ext4 defaults,noatime,nodiratime,barrier=1 0 1
常用优化选项:
noatime, nodiratime:减少时间戳更新,提升I/O性能barrier=1:确保数据一致性(默认开启)data=ordered:平衡性能与安全(默认)
2. 调整 swappiness
# 减少swap使用(服务器优先使用内存)
echo 'vm.swappiness = 10' >> /etc/sysctl.conf
sysctl -p
建议值:10(普通服务器),0(内存充足且不允许swap)
3. 文件句柄数限制
编辑 /etc/security/limits.conf:
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
同时确保 /etc/pam.d/common-session 包含:
session required pam_limits.so
4. 内核参数优化(/etc/sysctl.conf)
# 网络优化
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_rmem = 4096 65536 16777216
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.ip_forward = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
# 内存与虚拟内存
vm.overcommit_memory = 1
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
# 文件系统
fs.file-max = 1000000
fs.nr_open = 1000000
应用配置:
sysctl -p
三、时间同步配置
yum install -y chrony
systemctl enable chronyd
systemctl start chronyd
# 编辑 /etc/chrony.conf,添加国内NTP服务器
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
验证:
chronyc sources -v
四、日志管理
1. 配置日志轮转(logrotate)
确保 /etc/logrotate.conf 和 /etc/logrotate.d/ 配置合理,避免日志占满磁盘。
示例(/etc/logrotate.d/nginx):
/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 0640 nginx nginx
}
2. 使用 journald 限制日志大小
编辑 /etc/systemd/journald.conf:
[Journal]
SystemMaxUse=100M
SystemMaxFileSize=50M
MaxRetentionSec=1month
重启服务:
systemctl restart systemd-journald
五、监控与告警
1. 安装基础监控工具
yum install -y htop iotop iftop nmon sysstat
2. 启用 sar(sysstat)
systemctl enable sysstat
systemctl start sysstat
查看历史性能:
sar -u 1 3 # CPU
sar -r 1 3 # 内存
sar -b # I/O
六、SELinux 配置
- 生产环境建议 启用 SELinux,但可设为 permissive 模式调试:
setenforce 0 # 临时关闭
永久配置(/etc/selinux/config):
SELINUX=permissive # 或 enforcing(推荐)
SELINUXTYPE=targeted
推荐最终设置为
enforcing,并根据应用配置策略。
七、内核升级(可选)
CentOS 7.9 默认内核较旧,可考虑升级至较新稳定版(如 ELRepo 提供):
# 导入ELRepo
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
# 安装最新主线内核
yum --enablerepo=elrepo-kernel install kernel-ml -y
# 修改默认启动内核
grub2-set-default 0
reboot
八、其他建议
-
禁用IPv6(如不需要):
echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf echo 'net.ipv6.conf.default.disable_ipv6 = 1' >> /etc/sysctl.conf sysctl -p -
关闭透明大页(THP)(对数据库类应用重要):
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.local或使用 tuned:
yum install tuned-profiles-database tuned-adm profile latency-performance # 或 throughput-performance -
使用 tuned 优化配置
yum install tuned systemctl enable tuned systemctl start tuned tuned-adm profile throughput-performance
九、备份与自动化
- 配置定期备份(rsync + cron 或 bacula/bareos)
- 使用 Ansible/Puppet 实现配置自动化
- 记录变更(配置管理)
总结:关键优化点
| 类别 | 建议配置 |
|---|---|
| 安全 | 禁用root登录、firewalld、fail2ban、sudo用户 |
| 性能 | noatime挂载、swappiness=10、文件句柄限制 |
| 网络 | 优化TCP参数、端口范围、连接队列 |
| 内核 | 调整sysctl、可选升级内核 |
| 日志 | logrotate + journald限制 |
| 监控 | sysstat + htop + 外部监控(Zabbix/Prometheus) |
| SELinux | 建议启用 enforcing 模式 |
⚠️ 注意:所有优化需在测试环境验证后再应用于生产。建议使用配置管理工具(如 Ansible)统一部署。
如提供具体应用场景(如Web服务器、数据库、中间件等),可进一步细化优化策略。
CLOUD技术博