CentOS7关闭不必要开机自启服务优化系统
时间:2026-11-15 07:11 作者:emer 分类: Linux
前言
最小化安装仍带很多默认自启服务,占用内存和端口、增大攻击面。关闭用不到的服务能减少资源消耗和暴露面。本文讲解查看自启列表、按用途关闭、禁用与停止的区别和安全清单。
操作步骤
1. 查看已启用的开机自启服务
systemctl list-unit-files --type=service --state=enabled
2. 查看当前运行的服务
systemctl list-units --type=service
3. 停止并禁用某个服务
sudo systemctl stop postfix
sudo systemctl disable postfix
4. 一条命令停止并禁用
sudo systemctl disable --now postfix
5. 重新启用自启
sudo systemctl enable --now postfix
6. 查看某服务是否自启
systemctl is-enabled postfix
7. 常见可安全关闭项
postfix(不用邮件)、avahi-daemon、bluetooth、cups 等按需关闭。
常见坑
- 只 stop 不停 disable,重启后又自动起来。
- 关掉依赖服务导致其他功能异常,先弄清用途。
- firewalld、sshd、networkd 等核心服务别乱关。
- disable 只是不自启,已运行的还要 stop。
- 远程管理别先关 sshd,断连无法恢复。
- 云镜像里的代理/监控服务关闭可能影响云厂商运维。
- 优化后用 systemctl list-unit-files 复核。
总结
先 list-unit-files 看 enabled 列表,disable --now 同时停止并禁用。只关闭明确用不到的服务,核心网络和 SSH 服务保留。改动后复核自启状态,避免关闭依赖项造成功能异常。
遇到问题加QQ23979811 协助处理