下面给你一套CentOS Minimal(7/8/Stream 通用)系统加固清单,从“最该做”到“可选增强”,偏实战、可操作。
yum update -y
或(CentOS 8 / Stream):
dnf update -y
useradd admin
passwd admin
usermod -aG wheel admin
编辑 sudo 配置:
visudo
确保:
%wheel ALL=(ALL) ALL
vi /etc/ssh/sshd_config
修改:
PermitRootLogin no
PasswordAuthentication no # 推荐用密钥
重启 SSH:
systemctl restart sshd
在客户端:
ssh-keygen
ssh-copy-id admin@server_ip
passwd -l nobody
检查:
awk -F: '($3==0)' /etc/passwd
只应有 root 和管理员
vi /etc/login.defs
示例:
PASS_MAX_DAYS 90
PASS_MIN_DAYS 1
PASS_MIN_LEN 12
PASS_WARN_AGE 7
强制密码复杂度:
yum install -y libpwquality
编辑:
/etc/security/pwquality.conf
minlen = 12
dcredit = -1
ucredit = -1
ocredit = -1
lcredit = -1
systemctl enable --now firewalld
只开放必要端口:
firewall-cmd --permanent --add-port=22/tcp
firewall-cmd --reload
systemctl list-unit-files --type=service | grep enabled
停用示例:
systemctl disable --now postfix
getenforce
若为 disabled:
vi /etc/selinux/config
SELINUX=enforcing
重启系统。
yum install -y epel-release
yum install -y fail2ban
systemctl enable --now fail2ban
示例配置:
/etc/fail2ban/jail.local
[sshd]
enabled = true
maxretry = 3
bantime = 3600
systemctl enable --now auditd
监控重要文件:
auditctl -w /etc/passwd -p wa -k passwd_changes
vi /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
sysctl -p
yum install -y dnf-automatic
vi /etc/dnf/automatic.conf
apply_updates = yes
systemctl enable --now dnf-automatic.timer
✅ 不装 GUI
✅ 不装不需要的服务
✅ 不开放多余端口
✅ 不用弱密码
✅ 不用 root 日常操作
如果你愿意,我可以:
你现在是 CentOS 几?物理机还是云服务器?