去年我们公司一台测试服务器被挖矿木马感染,原因仅仅是因为使用了 22 端口默认 root 登录 + 弱密码 123456。安全这件事,平时不出事没人管,出事就是大事 。本文整理了一份从入门到生产环境的 Linux 服务器安全加固清单,建议收藏并定期检查。
一、SSH 安全(最关键) SSH 是服务器的第一道门,80% 的服务器入侵都是从弱 SSH 密码开始的 。
1. 修改默认端口 1 2 3 4 5 Port 2222 systemctl restart sshd
注意:修改前先确认新端口能通 ,不然你可能被锁在外面。
2. 禁用 root 登录 3. 禁用密码登录,改用密钥 这是最重要的一步!
1 2 3 4 5 6 7 8 9 10 ssh-keygen -t ed25519 -C "your_email@example.com" ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server -p 2222 PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no
4. 配置 fail2ban 防爆破 1 2 yum install -y epel-release yum install -y fail2ban
1 2 3 4 5 6 7 8 9 10 11 [DEFAULT] bantime = 3600 findtime = 600 maxretry = 3 [sshd] enabled = true port = 2222 filter = sshdlogpath = /var/log/secure
1 2 3 systemctl enable fail2ban systemctl start fail2ban fail2ban-client status sshd
5. 仅允许特定 IP 登录 1 2 3 4 5 sshd: 192.168.1.0/24, 10.0.0.5 sshd: ALL
二、防火墙配置 iptables 基础规则 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -p tcp --dport 2222 -j ACCEPT iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT service iptables save
firewalld(CentOS 7+) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 firewall-cmd --get-default-zone firewall-cmd --permanent --add-service=ssh firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --permanent --add-port=2222/tcp firewall-cmd --reload firewall-cmd --list-all
ufw(Ubuntu) 1 2 3 4 5 6 7 ufw default deny incoming ufw default allow outgoing ufw allow 2222/tcp ufw allow 80/tcp ufw allow 443/tcp ufw enable ufw status verbose
三、用户与权限管理 1. 最小权限原则 1 2 3 4 5 6 useradd -m -s /bin/bash opsadmin passwd opsadmin usermod -aG wheel opsadmin
2. 配置 sudo 细粒度授权 1 2 3 opsadmin ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx
3. 重要文件权限 1 2 3 4 5 6 7 8 chmod 600 /etc/shadowchmod 600 /etc/gshadowchmod 644 /etc/passwdchmod 644 /etc/groupchattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow chattr +a /var/log/messages
4. 定期审计账号 1 2 3 4 5 6 7 8 awk -F: '($3 == 0) {print}' /etc/passwd awk -F: '($2 == "" ) {print}' /etc/shadow lastlog | grep -v "Never" | awk '$4=="**" || ...'
四、系统更新与补丁 配置自动更新 1 2 3 4 5 6 7 8 9 yum install -y yum-cron sed -i 's/update_cmd = default/update_cmd = security/' /etc/yum/yum-cron.conf sed -i 's/apply_updates = no/apply_updates = yes/' /etc/yum/yum-cron.conf systemctl enable yum-cron apt install -y unattended-upgrades dpkg-reconfigure -plow unattended-upgrades
手动检查更新 1 2 yum check-update yum update --security
五、入侵检测 1. 安装 AIDE(文件完整性检查) 1 2 3 4 yum install -y aide aide --init mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gzaide --check
2. 安装 rkhunter(查 rootkit) 1 2 3 yum install -y rkhunter rkhunter --update rkhunter --check --skip-keypress
3. 安装 ClamAV(杀毒) 1 2 3 yum install -y clamav clamav-update freshclam clamscan -ri /var/www/
4. 检查异常进程和网络连接 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ps aux | sort -rk 4 | head ss -tulnp ss -tan | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head cat ~/.bash_historycrontab -l ls -la /etc/cron*
六、日志审计 1. 配置 rsyslog 集中日志 1 2 3 *.* @192.168.1.100:514 *.* @@192.168.1.100:514
2. auditd 审计关键操作 1 2 3 4 5 6 7 8 yum install -y audit auditctl -w /etc/passwd -p wa -k passwd_changes auditctl -w /etc/sudoers -p wa -k sudoers_changes ausearch -k passwd_changes
3. 配置 logrotate 1 2 3 4 5 6 7 8 9 10 11 12 13 /var/log/nginx/*.log { daily rotate 14 compress delaycompress notifempty create 0640 www www sharedscripts postrotate [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid) endscript }
七、内核参数加固 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 net.ipv4.ip_forward = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 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.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.all.log_martians = 1
应用:
八、应用层安全 1. Web 服务器 Nginx :隐藏版本号(server_tokens off),限制访问频率,使用 HTTPSApache :同样隐藏版本,禁用目录浏览(Options -Indexes)PHP :expose_php = Off,禁用危险函数1 2 disable_functions = exec,passthru,shell_exec,system,proc_open,popen
2. 数据库(MySQL) 1 2 3 4 5 6 7 8 DELETE FROM mysql.user WHERE User='' ; DROP DATABASE test ; DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%' ; UPDATE mysql.user SET Host='localhost' WHERE User='root' ; FLUSH PRIVILEGES;
3. Redis 1 2 3 4 5 bind 127.0.0.1 requirepass your_strong_password rename-command FLUSHDB "" rename-command FLUSHALL ""
九、备份与灾备 再好的安全防护也不能保证 100% 安全,所以备份是最后一道防线。
1. 数据备份策略 3-2-1 原则 :3 份数据、2 种介质、1 份异地每日全量 + 增量备份 定期恢复演练 (备份不演练等于没备份)2. 自动化备份脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #!/bin/bash BACKUP_DIR=/data/backup DATE=$(date +%Y%m%d) mkdir -p $BACKUP_DIR /$DATE mysqldump -uroot -p$DB_PWD --all-databases | gzip > $BACKUP_DIR /$DATE /db_$DATE .sql.gz tar czf $BACKUP_DIR /$DATE /config_$DATE .tar.gz /etc /var/www rsync -avz $BACKUP_DIR /$DATE / backup@remote-server:/backup/ find $BACKUP_DIR -type d -mtime +7 | xargs rm -rf
3. 快照(云服务器) 阿里云/腾讯云/AWS 都支持磁盘快照 ,定期创建可以在出问题后秒级恢复。
十、加固检查清单 写在最后 安全是一个持续的过程,不是一次性任务 。建议:
把这份清单打印出来,逐项打钩 每月做一次安全审计 关注 CVE 公告,关键漏洞要在 24 小时内修复 给关键服务配置告警(登录告警、异常流量告警等) 记住:没有绝对安全的系统,只有不断加固的过程 。希望这份清单能帮你把服务器的安全等级提升一个台阶。