centos9系统怎么加固防御?
美国、香港服务器
centos9系统怎么加固防御?
06-24 来源:
一、基础初始化加固(必做)
1. 系统全量更新,修复高危漏洞
bash
运行
dnf update -y
dnf autoremove -y
# 开启自动安全更新
dnf install dnf-automatic -y
systemctl enable --now dnf-automatic.timer
2. 创建普通运维账号,禁止 root 直接外网登录
bash
运行
# 创建运维用户并加入sudo管理员组
useradd admin
passwd admin
usermod -aG wheel admin
3. 关闭无用高危服务,缩小攻击面
bash
运行
# 关闭闲置服务
for svc in telnet.socket rpcbind nfs-server cups postfix;do
systemctl stop $svc 2>/dev/null
systemctl disable $svc 2>/dev/null
done
# 不使用IPv6则关闭
systemctl stop ip6tables
systemctl disable ip6tables
二、SSH 远程登录硬核防护(防暴力破解核心)
1. 修改 SSH 配置
bash
运行
nano /etc/ssh/sshd_config
修改 / 添加以下配置:
ini
Port 2222 # 修改为高位端口(1025-65535)
PermitRootLogin no # 禁止root远程登录
PasswordAuthentication no # 关闭密码登录,仅密钥登录
PermitEmptyPasswords no
MaxAuthTries 3 # 最多3次密码错误
LoginGraceTime 20
ClientAliveInterval 60
ClientAliveCountMax 3
UseDNS no
AllowUsers admin # 仅允许admin用户SSH登录
校验配置 + 重启服务:
bash
运行
sshd -t
systemctl restart sshd
systemctl enable sshd
2. SSH 密钥登录配置(终极安全方式)
本地生成密钥,把公钥上传到服务器/home/admin/.ssh/authorized_keys,权限必须严格:
bash
运行
mkdir -p /home/admin/.ssh
chmod 700 /home/admin/.ssh
chmod 600 /home/admin/.ssh/authorized_keys
chown -R admin:admin /home/admin/.ssh
三、firewalld 防火墙最小权限加固(重中之重)
1. 开机自启,设置默认拒绝所有入站
bash
运行
systemctl enable --now firewalld
# 设置默认区域为丢弃,只放行你白名单IP
firewall-cmd --set-default-zone=drop --permanent
# 放行本地回环
firewall-cmd --permanent --add-interface=lo
# 放行你的公网IP访问SSH端口(替换你的IP和SSH端口)
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="112.xx.xx.xx/32" port protocol="tcp" port="2222" accept"
# 业务端口仅需要对外开放才放行80、443
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
# 重载生效
firewall-cmd --reload
# 查看规则
firewall-cmd --list-all
安全建议
SSH、宝塔、数据库、Redis绝对不要全网放行 0.0.0.0/0,仅放行你固定公网 IP。
四、部署 Fail2Ban 自动拉黑爆破 IP
1. 安装配置
bash
运行
dnf install epel-release -y
dnf install fail2ban fail2ban-firewalld -y
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
nano /etc/fail2ban/jail.local
拉到末尾新增 SSH 防护规则:
ini
[sshd]
enabled = true
port = 2222
maxretry = 3
bantime = 86400
findtime = 600
backend = systemd
启动服务:
bash
运行
systemctl enable --now fail2ban
# 查看封禁IP
fail2ban-client status sshd
五、内核 sysctl 网络安全优化(抵御洪水、路由劫持)
bash
运行
cp /etc/sysctl.conf /etc/sysctl.conf.bak
nano /etc/sysctl.conf
追加以下参数:
ini
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
kernel.yama.ptrace_scope = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
生效:
bash
运行
sysctl -p
六、临时目录加固(防止挖矿木马执行)
给 /tmp、/var/tmp 禁止脚本执行、SUID 提权
bash
运行
mount -o remount,noexec,nosuid,nodev /tmp
mount -o remount,noexec,nosuid,nodev /var/tmp
# 永久写入fstab(云服务器通用方案)
echo "tmpfs /tmp tmpfs defaults,noexec,nosuid,nodev,size=2G 0 0" >> /etc/fstab
七、账号与密码安全策略
1. 强密码策略
bash
运行
dnf install libpwquality -y
nano /etc/security/pwquality.conf
配置:密码长度≥12 位,大小写 + 数字 + 特殊符号
ini
minlen = 12
dcredit = -1
ucredit = -1
lcredit = -1
ocredit = -1
2. 锁定关键系统文件,防止黑客篡改
bash
运行
chattr +i /etc/passwd /etc/shadow /etc/sudoers /etc/ssh/sshd_config
# 如需修改先解锁:chattr -i 文件名
3. 限制普通用户使用 crontab 定时任务
bash
运行
echo "root" > /etc/cron.allow
echo "admin" >> /etc/cron.allow
八、系统审计 + 日志加固
1. 开启 auditd 审计,监控账号、sudo 敏感操作
bash
运行
dnf install audit -y
systemctl enable --now auditd
# 监控用户、密码文件篡改
auditctl -w /etc/passwd -p wa -k user_change
auditctl -w /etc/shadow -p wa -k shadow_change
auditctl -w /etc/sudoers -p wa -k sudo_modify
2. 日志留存,防止黑客清空操作记录
日志默认存储/var/log/secure、/var/log/messages,建议定期备份日志。
九、资源限制防护(抵御 CC、进程耗尽攻击)
bash
运行
nano /etc/security/limits.conf
末尾添加:
ini
* soft nofile 65535
* hard nofile 65535
* soft nproc 10240
* hard nproc 10240
* soft core 0
* hard core 0
十、Web / 宝塔环境额外加固
修改宝塔默认 8888 端口,面板设置 IP 白名单、开启两步验证
安装 WAF 防火墙,拦截注入、上传木马、CC 攻击
MySQL、Redis 仅监听127.0.0.1,禁止外网直接访问
网站上传目录禁止 PHP、Shell 脚本执行权限
十一、安全巡检常用命令(定期自查)
bash
运行
# 查看异常登录
grep "Accepted password" /var/log/secure
# 查看失败爆破IP
grep "Failed password" /var/log/secure
# 查看所有用户定时任务
for user in $(cut -f1 -d: /etc/passwd);do crontab -l -u $user 2>/dev/null;done
# 查看UID=0高危账号
awk -F: "$3==0{print $1}" /etc/passwd
# 查看可疑外联端口
ss -antlp
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快