温馨提示×

怎样优化centos minimal

小樊
60
2026-09-11 17:04:04
栏目: 智能运维

“优化 CentOS Minimal”通常指的是在最小化安装(无 GUI、服务少)的基础上,让它更轻量、更安全、更快、更便于管理。下面按目标分类给出实用优化建议(适用于 CentOS 7 / 8 / Stream,部分适用于 Rocky/Alma)。


一、系统初始化优化

1. 关闭不必要的服务

Minimal 本身服务少,但仍可精简:

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

常见可关闭:

systemctl disable --now postfix
systemctl disable --now firewalld   # 若使用 iptables 或云安全组

⚠️ 生产环境建议保留 firewalld 或配置 iptables。


2. 时间同步(非常重要)

timedatectl set-timezone Asia/Shanghai
yum install -y chrony
systemctl enable --now chronyd

3. 精简内核参数(/etc/sysctl.conf)

示例(通用服务器优化):

net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
vm.swappiness = 10
fs.file-max = 100000

应用:

sysctl -p

二、安全优化(Minimal 更应重视)

1. SSH 安全

vi /etc/ssh/sshd_config

建议:

PermitRootLogin no
PasswordAuthentication no
Port 2222

然后:

systemctl restart sshd

2. 创建普通用户 + sudo

useradd admin
passwd admin
usermod -aG wheel admin

3. 基础防火墙

firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

三、软件与包管理优化

1. 换国内镜像源(加速)

CentOS 7:

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

CentOS 8 / Stream:

sed -e 's|mirrorlist=|#mirrorlist=|g' \
    -e 's|#baseurl=http|baseurl=http|g' \
    -i /etc/yum.repos.d/CentOS-*.repo

2. 安装常用工具(Minimal 缺很多)

yum install -y \
  vim wget curl net-tools lsof \
  bash-completion tree unzip gzip

四、资源与性能优化

1. 禁用无用硬件服务

如果是虚拟机:

systemctl disable --now kdump

2. 调整文件描述符

vi /etc/security/limits.conf

追加:

* soft nofile 100000
* hard nofile 100000

3. 磁盘 IO 优化(SSD)

vi /etc/fstab

挂载加:

noatime,nodiratime

五、监控与维护

1. 日志管理

yum install -y logrotate

2. 基础监控

yum install -y htop iotop

六、如果是“云服务器 / 容器 / 嵌入式”场景

  • 云服务器:用安全组代替 firewalld
  • 容器宿主机:关闭 swap、开启 br_netfilter
  • 嵌入式:用 microdnf(CentOS 8+)

七、推荐的最小可用优化清单 ✅

  • [ ] 改 SSH 端口 + 禁 root
  • [ ] 开 chrony
  • [ ] 换国内源
  • [ ] 装基础工具
  • [ ] 关 kdump / postfix
  • [ ] 配 sysctl + limits

如果你能告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • 用途(服务器 / 桌面 / 容器 / 嵌入式)
  • 是否在云上

我可以直接给你一套可复制的初始化脚本。

0 踩