Linux服务器安全加固完整实操方案
前言
云服务器暴露公网极易遭受暴力破解、端口扫描攻击,2024年线上运维基础标准就是服务器基线加固。本文整理一套可直接复制执行的加固脚本与配置方案,适用于CentOS、Ubuntu主流系统,帮助运维人员快速建立服务器安全防线。
一、系统基础安全加固
1.1 最小化安装与服务精简
服务器安全的第一原则是”最小化原则”——只安装必要的服务,关闭不需要的端口。
CentOS系统查看并关闭不必要服务:
# 查看所有开机自启服务
systemctl list-unit-files --type=service --state=enabled
# 关闭不需要的服务(示例)
systemctl disable --now postfix
systemctl disable --now avahi-daemon
systemctl disable --now cupsUbuntu系统服务精简:
# 查看监听端口
ss -tulnp
# 卸载不必要的软件包
apt remove --purge -y rpcbind nfs-common
apt autoremove -y1.2 系统更新与补丁管理
及时更新系统补丁是防范已知漏洞的最有效手段。
CentOS/RHEL系统:
# 更新所有软件包
yum update -y
# 仅更新安全补丁(CentOS 7+)
yum update --security -y
# 配置自动安全更新
yum install -y yum-cron
systemctl enable --now yum-cronUbuntu/Debian系统:
# 更新系统
apt update && apt upgrade -y
# 安装自动安全更新工具
apt install -y unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades二、SSH安全配置
SSH是服务器入侵的重灾区,必须进行严格的安全配置。
2.1 修改默认端口与基础安全设置
编辑SSH配置文件:
vim /etc/ssh/sshd_config关键配置项:
# 修改默认端口(建议改为10000以上的端口)
Port 22222
# 禁用root用户直接登录
PermitRootLogin no
# 禁用密码登录(配置密钥登录后开启)
PasswordAuthentication no
# 限制最大认证尝试次数
MaxAuthTries 3
# 登录超时时间(秒)
LoginGraceTime 30
# 禁用空密码
PermitEmptyPasswords no
# 禁用X11转发
X11Forwarding no
# 允许的用户(白名单机制)
AllowUsers admin opsuser配置完成后重启SSH服务:
# CentOS
systemctl restart sshd
# Ubuntu
systemctl restart ssh2.2 SSH密钥登录配置
客户端生成密钥对:
ssh-keygen -t ed25519 -C "your_email@example.com"将公钥上传到服务器:
ssh-copy-id -p 22222 admin@your_server_ip服务器端权限设置:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys三、防火墙配置
3.1 iptables基础配置脚本
#!/bin/bash
# 清空现有规则
iptables -F
iptables -X
iptables -Z
# 默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH(修改为你的端口)
iptables -A INPUT -p tcp --dport 22222 -j ACCEPT
# 允许HTTP/HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许Ping(可选)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 保存规则
iptables-save > /etc/sysconfig/iptables3.2 firewalld配置(CentOS 7+)
# 启动并启用firewalld
systemctl enable --now firewalld
# 开放SSH端口
firewall-cmd --permanent --add-port=22222/tcp
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
# 限制SSH连接频率(防暴力破解)
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 22222 -m state --state NEW -m recent --set --name SSH
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 22222 -m state --state NEW -m recent --update --seconds 60 --hitcount 5 --name SSH -j DROP
# 重载规则
firewall-cmd --reload3.3 ufw配置(Ubuntu)
# 启用ufw
ufw enable
# 设置默认策略
ufw default deny incoming
ufw default allow outgoing
# 允许SSH
ufw allow 22222/tcp
# 允许Web服务
ufw allow 80/tcp
ufw allow 443/tcp
# 限制SSH连接频率
ufw limit 22222/tcp
# 查看状态
ufw status verbose四、用户权限与口令安全
4.1 密码策略配置
编辑 /etc/security/pwquality.conf:
# 密码最小长度
minlen = 12
# 至少包含的大写字母数
ucredit = -1
# 至少包含的数字数
dcredit = -1
# 至少包含的特殊字符数
ocredit = -1
# 禁止与旧密码重复的次数
remember = 54.2 sudo权限管理
创建运维用户并配置sudo权限:
# 创建用户
useradd -m -s /bin/bash opsuser
passwd opsuser
# 添加sudo权限(无需密码)
echo "opsuser ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/opsuser
chmod 440 /etc/sudoers.d/opsuser五、日志审计与入侵检测
5.1 fail2ban防暴力破解
安装fail2ban:
# CentOS
yum install -y epel-release
yum install -y fail2ban
# Ubuntu
apt install -y fail2ban配置SSH防护:
cat > /etc/fail2ban/jail.local << EOF
[DEFAULT]
# 封禁时间(秒)
bantime = 86400
# 检测时间窗口(秒)
findtime = 600
# 最大尝试次数
maxretry = 3
# 忽略的IP
ignoreip = 127.0.0.1/8 192.168.1.0/24
[sshd]
enabled = true
port = 22222
filter = sshd
logpath = /var/log/secure
maxretry = 3
EOF启动服务:
systemctl enable --now fail2ban
# 查看封禁状态
fail2ban-client status sshd六、一键加固脚本
以下是一个综合加固脚本,可直接复制执行:
#!/bin/bash
# Linux服务器安全加固脚本
# 适用:CentOS 7/8、Ubuntu 18.04/20.04/22.04
echo "=== 开始服务器安全加固 ==="
# 1. 系统更新
echo "[1/6] 执行系统更新..."
if [ -f /etc/redhat-release ]; then
yum update -y
yum install -y vim wget curl net-tools
else
apt update && apt upgrade -y
apt install -y vim wget curl net-tools
fi
# 2. SSH配置加固
echo "[2/6] 配置SSH安全..."
SSHD_CONFIG="/etc/ssh/sshd_config"
sed -i 's/^#PermitRootLogin.*/PermitRootLogin no/' $SSHD_CONFIG
sed -i 's/^#MaxAuthTries.*/MaxAuthTries 3/' $SSHD_CONFIG
sed -i 's/^#LoginGraceTime.*/LoginGraceTime 30/' $SSHD_CONFIG
sed -i 's/^#PermitEmptyPasswords.*/PermitEmptyPasswords no/' $SSHD_CONFIG
sed -i 's/^#X11Forwarding.*/X11Forwarding no/' $SSHD_CONFIG
# 3. 安装fail2ban
echo "[3/6] 安装fail2ban入侵检测..."
if [ -f /etc/redhat-release ]; then
yum install -y epel-release fail2ban
else
apt install -y fail2ban
fi
cat > /etc/fail2ban/jail.local << EOF
[DEFAULT]
bantime = 86400
findtime = 600
maxretry = 3
[sshd]
enabled = true
maxretry = 3
EOF
systemctl enable --now fail2ban
# 4. 配置防火墙
echo "[4/6] 配置防火墙规则..."
if [ -f /etc/redhat-release ]; then
systemctl enable --now firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
else
ufw --force enable
ufw default deny incoming
ufw default allow outgoing
ufw allow 80/tcp
ufw allow 443/tcp
fi
# 5. 历史命令记录优化
echo "[5/6] 优化历史命令记录..."
echo 'export HISTTIMEFORMAT="%F %T "' >> /etc/profile
echo 'export HISTSIZE=10000' >> /etc/profile
echo 'export HISTFILESIZE=10000' >> /etc/profile
echo 'shopt -s histappend' >> /etc/profile
# 6. 禁止Ctrl+Alt+Del重启
echo "[6/6] 禁止快捷键重启..."
systemctl mask ctrl-alt-del.target
echo "=== 安全加固完成 ==="
echo "注意:请手动修改SSH端口并配置密钥登录后再禁用密码登录"总结
服务器安全加固是一个持续的过程,本文涵盖了基础系统加固、SSH安全、防火墙、用户权限、入侵检测等核心维度。建议按照以下优先级执行:
- 立即执行:修改SSH端口、禁用root登录、配置防火墙
- 尽快完成:密钥登录替换密码登录、安装fail2ban
- 定期维护:系统补丁更新、日志审计、安全巡检
安全没有银弹,多层防御才能有效降低风险。建议结合云服务商的安全组、WAF等产品构建纵深防御体系。