Linux服务器管理完全攻略 第10篇:安全加固与漏洞管理
Linux Server Administration Complete Guide Part 10 (Final)
Linux服务器管理完全攻略系列
第9篇:备份与恢复 | 第10篇:安全加固 (当前/最终)
前言:安全不是选择而是必须
服务器安全需要纵深防御(Defense in Depth)策略,而不是单一解决方案。在这最后一篇中,我们讲解Linux服务器安全加固的核心内容。
1. 系统加固基础
1.1 禁用不必要的服务
# 检查运行中的服务
systemctl list-units --type=service --state=running
# 禁用不必要的服务
sudo systemctl disable --now cups
sudo systemctl disable --now avahi-daemon
sudo systemctl disable --now bluetooth
# 检查开机启动的服务
systemctl list-unit-files --type=service --state=enabled
1.2 安全更新
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
# RHEL/CentOS
sudo dnf update -y
sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
1.3 GRUB安全
# 设置GRUB密码
sudo grub-mkpasswd-pbkdf2
# 复制哈希值
# 编辑 /etc/grub.d/40_custom
set superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512...
# 应用配置
sudo update-grub
2. 用户安全
2.1 密码策略
# /etc/login.defs
PASS_MAX_DAYS 90 # 最大使用期限
PASS_MIN_DAYS 7 # 最小使用期限
PASS_WARN_AGE 14 # 过期警告
# PAM密码复杂度 (/etc/pam.d/common-password)
password requisite pam_pwquality.so retry=3 minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1
# 应用到现有用户
sudo chage -M 90 -m 7 -W 14 username
2.2 sudo配置
# /etc/sudoers (使用visudo)
# 启用日志记录
Defaults logfile=/var/log/sudo.log
Defaults log_input, log_output
# 超时设置
Defaults timestamp_timeout=5
# 只允许特定命令
username ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx
2.3 账户锁定
# /etc/pam.d/common-auth
auth required pam_tally2.so deny=5 unlock_time=900
# 检查失败次数
pam_tally2 --user=username
# 解锁账户
pam_tally2 --user=username --reset
3. SSH安全加固
# /etc/ssh/sshd_config
# 更改默认端口
Port 2222
# 禁止root登录
PermitRootLogin no
# 禁用密码认证
PasswordAuthentication no
# 只允许密钥认证
PubkeyAuthentication yes
# 禁止空密码
PermitEmptyPasswords no
# 限制登录尝试
MaxAuthTries 3
LoginGraceTime 30
# 只允许特定用户/组
AllowUsers admin deploy
AllowGroups sshusers
# 协议版本
Protocol 2
# 应用更改
sudo systemctl restart sshd
4. 防火墙配置
4.1 UFW (Ubuntu)
# 默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 只允许必要端口
sudo ufw allow 2222/tcp comment 'SSH'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
# 基于IP的限制
sudo ufw allow from 192.168.1.0/24 to any port 22
# 启用防火墙
sudo ufw enable
sudo ufw status verbose
4.2 firewalld (RHEL/CentOS)
# 检查默认区域
sudo firewall-cmd --get-default-zone
# 允许服务
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
# 允许端口
sudo firewall-cmd --permanent --add-port=2222/tcp
# 应用规则
sudo firewall-cmd --reload
5. 入侵检测系统
5.1 Fail2Ban
# 安装
sudo apt install fail2ban
# /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
[sshd]
enabled = true
port = 2222
logpath = /var/log/auth.log
[nginx-http-auth]
enabled = true
# 检查状态
sudo fail2ban-client status
sudo fail2ban-client status sshd
5.2 AIDE(完整性检查)
# 安装
sudo apt install aide
# 初始化数据库
sudo aideinit
# 运行检查
sudo aide --check
# 更新数据库
sudo aide --update
6. SELinux / AppArmor
6.1 SELinux (RHEL/CentOS)
# 检查状态
getenforce
sestatus
# 更改模式
sudo setenforce 0 # Permissive
sudo setenforce 1 # Enforcing
# 永久设置 (/etc/selinux/config)
SELINUX=enforcing
# 故障排除
sudo ausearch -m avc -ts recent
sudo sealert -a /var/log/audit/audit.log
6.2 AppArmor (Ubuntu)
# 检查状态
sudo aa-status
# 配置文件管理
sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
sudo aa-complain /etc/apparmor.d/usr.sbin.nginx
# 查看日志
sudo journalctl -fx | grep apparmor
7. 漏洞扫描
7.1 Lynis
# 安装
sudo apt install lynis
# 系统审计
sudo lynis audit system
# 查看报告
sudo cat /var/log/lynis.log
sudo cat /var/log/lynis-report.dat
7.2 OpenVAS
# 安装 (Ubuntu)
sudo apt install openvas
# 初始设置
sudo gvm-setup
# 开始扫描
sudo gvm-start
# Web界面: https://localhost:9392
8. 日志监控
# 监控可疑活动
# 登录失败
grep "Failed password" /var/log/auth.log | tail -20
# sudo使用记录
grep "sudo" /var/log/auth.log
# 系统启动/关闭
last reboot
last shutdown
# 当前登录用户
who
w
# 网络连接
ss -tuln
netstat -tuln
9. 安全检查清单
#!/bin/bash
# security_audit.sh
echo "=== Linux 安全审计 ==="
echo -e "\n[1] 检查系统更新"
apt list --upgradable 2>/dev/null | head -10
echo -e "\n[2] root登录状态"
grep "^PermitRootLogin" /etc/ssh/sshd_config
echo -e "\n[3] 密码认证状态"
grep "^PasswordAuthentication" /etc/ssh/sshd_config
echo -e "\n[4] 开放端口"
ss -tuln | grep LISTEN
echo -e "\n[5] SUID文件"
find / -perm -4000 -type f 2>/dev/null | head -10
echo -e "\n[6] 空密码账户"
awk -F: '($2 == "") {print $1}' /etc/shadow
echo -e "\n[7] 最近登录失败"
grep "Failed password" /var/log/auth.log 2>/dev/null | tail -5
echo -e "\n[8] 防火墙状态"
ufw status 2>/dev/null || firewall-cmd --state 2>/dev/null
echo -e "\n审计完成"
10. 系列完结
Linux服务器管理完全攻略系列到此结束。通过10篇文章,我们从基础到安全全面讲解了服务器管理的核心内容:
- 第1篇:Linux基础与文件系统
- 第2篇:用户与权限管理
- 第3篇:包管理与服务运维
- 第4篇:网络配置与防火墙
- 第5篇:SSH与远程服务器管理
- 第6篇:Shell脚本基础
- 第7篇:Shell脚本高级
- 第8篇:日志管理与监控
- 第9篇:备份与恢复策略
- 第10篇:安全加固与漏洞管理
希望本系列能成为您Linux服务器管理的坚实基础。