温馨提示×

如何优化CentOS Minimal

小樊
42
2026-07-08 02:05:48
栏目: 智能运维

优化 CentOS Minimal(最小化安装)主要是为了:减小体积、提升安全性、加快启动速度、降低资源占用。下面按 系统配置、内核、服务、网络、存储、安全、日常维护 等方面给你一份实践清单(适用于 CentOS 7 / CentOS 8 Stream / Rocky / Alma 类似)。


一、基础系统优化

1. 更新系统(首步)

yum update -y        # CentOS 7
dnf update -y        # CentOS 8+

2. 精简已安装软件

查看并删除无用软件:

rpm -qa | wc -l

典型可删:

yum remove -y nano sendmail ftp telnet

二、内核与启动优化

1. 精简内核参数(sysctl)

编辑:

vi /etc/sysctl.conf

常用优化:

vm.swappiness = 10
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65535
net.core.somaxconn = 4096

生效:

sysctl -p

2. 精简 GRUB 启动项

减少启动等待时间:

vi /etc/default/grub
GRUB_TIMEOUT=2

更新:

grub2-mkconfig -o /boot/grub2/grub.cfg

三、服务优化(重点)

1. 禁用无用服务

查看服务:

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

禁用示例:

systemctl disable postfix
systemctl disable firewalld   # 如用 iptables 或云安全组

保留常用:

  • sshd
  • network / NetworkManager
  • crond

2. 使用轻量服务替代

原服务 替代
firewalld iptables / nftables
postfix 不装
tuned 关闭

四、网络优化

1. 禁用 IPv6(如不需要)

grubby --update-kernel=ALL --args="ipv6.disable=1"

或:

vi /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1

2. 优化 DNS

vi /etc/resolv.conf
nameserver 223.5.5.5
nameserver 8.8.8.8

五、存储与磁盘优化

1. 使用 noatime

vi /etc/fstab
UUID=xxx  /  ext4  defaults,noatime  0 0

2. 删除旧内核(CentOS 7)

package-cleanup --oldkernels --count=1

六、安全优化(Minimal 必做)

1. SSH 加固

vi /etc/ssh/sshd_config

推荐:

Port 2222
PermitRootLogin no
PasswordAuthentication no
UseDNS no

重启:

systemctl restart sshd

2. 配置防火墙(最小规则)

firewall-cmd --set-default-zone=drop
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

3. 禁用 SELinux(如不需要)

vi /etc/selinux/config
SELINUX=disabled

七、日志与监控优化

1. 限制日志大小

vi /etc/systemd/journald.conf
SystemMaxUse=50M

2. 使用轻量监控

  • 不装 Zabbix / Nagios
  • 可用:
    • top
    • htop
    • vmstat
    • netstat -tunlp

八、系统资源限制

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

九、镜像与体积优化(进阶)

1. 清理缓存

yum clean all
dnf clean all

2. 删除文档和语言包

rm -rf /usr/share/doc/*
rm -rf /usr/share/man/*

十、适合 Minimal 的典型用途

✅ 容器基础镜像
✅ 云服务器
✅ 边缘设备
✅ 纯 CLI 环境


推荐“极简优化”组合(最实用)

禁用 IPv6
禁用 SELinux
优化 SSH
禁用无用服务
noatime
sysctl 网络优化

如果你愿意,我可以:

  • ✅ 给你 一键优化脚本
  • ✅ 针对 CentOS 7 / 8 / Stream 单独方案
  • ✅ 按 服务器 / 容器 / 桌面 场景定制

你现在是 物理机 / 虚拟机 / 云服务器 / Docker 基础镜像 哪种?

0