去年我们公司一台测试服务器被挖矿木马感染,原因仅仅是因为使用了 22 端口默认 root 登录 + 弱密码 123456。安全这件事,平时不出事没人管,出事就是大事。本文整理了一份从入门到生产环境的 Linux 服务器安全加固清单,建议收藏并定期检查。

一、SSH 安全(最关键)

SSH 是服务器的第一道门,80% 的服务器入侵都是从弱 SSH 密码开始的

1. 修改默认端口

1
2
3
4
5
# 编辑 /etc/ssh/sshd_config
Port 2222 # 改成 10000 以上的端口

# 重启服务
systemctl restart sshd

注意:修改前先确认新端口能通,不然你可能被锁在外面。

2. 禁用 root 登录

1
PermitRootLogin no

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
# /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3

[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/secure
1
2
3
systemctl enable fail2ban
systemctl start fail2ban
fail2ban-client status sshd # 查看封禁状态

5. 仅允许特定 IP 登录

1
2
3
4
5
# /etc/hosts.allow
sshd: 192.168.1.0/24, 10.0.0.5

# /etc/hosts.deny
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

# 允许 SSH
iptables -A INPUT -p tcp --dport 2222 -j ACCEPT

# 允许 HTTP/HTTPS
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
# 不要每个运维都给 root,按需分配
useradd -m -s /bin/bash opsadmin
passwd opsadmin

# 把需要 sudo 的用户加入 wheel 组(CentOS)或 sudo 组(Ubuntu)
usermod -aG wheel opsadmin

2. 配置 sudo 细粒度授权

1
2
3
# visudo
# 允许 opsadmin 重启 Nginx 但不能 rm /etc
opsadmin ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx

3. 重要文件权限

1
2
3
4
5
6
7
8
chmod 600 /etc/shadow
chmod 600 /etc/gshadow
chmod 644 /etc/passwd
chmod 644 /etc/group

# 关键配置文件
chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow # 防修改
chattr +a /var/log/messages # 只能追加,不能删除

4. 定期审计账号

1
2
3
4
5
6
7
8
# 查找 UID 为 0 的账号(应该只有 root)
awk -F: '($3 == 0) {print}' /etc/passwd

# 查找可登录的空密码账号
awk -F: '($2 == "" ) {print}' /etc/shadow

# 查找最近 30 天未登录的账号
lastlog | grep -v "Never" | awk '$4=="**" || ...'

四、系统更新与补丁

配置自动更新

1
2
3
4
5
6
7
8
9
# CentOS / RHEL
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

# Ubuntu
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.gz
aide --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/ # 扫描 Web 目录

4. 检查异常进程和网络连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# CPU 占用 TOP 10
ps aux | sort -rk 4 | head

# 监听端口
ss -tulnp

# 已建立的连接(按 IP 统计)
ss -tan | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head

# 历史命令
cat ~/.bash_history

# 定时任务
crontab -l
ls -la /etc/cron*

六、日志审计

1. 配置 rsyslog 集中日志

1
2
3
# /etc/rsyslog.d/remote.conf
*.* @192.168.1.100:514 # UDP
*.* @@192.168.1.100:514 # TCP

2. auditd 审计关键操作

1
2
3
4
5
6
7
8
yum install -y audit

# 审计 /etc/passwd 修改
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
# /etc/logrotate.d/nginx
/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
# /etc/sysctl.conf

# 禁用 IP 转发(非路由器)
net.ipv4.ip_forward = 0

# 禁用 ICMP 重定向
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

# 启用 SYN Cookie(防 SYN Flood)
net.ipv4.tcp_syncookies = 1

# 禁用 IP 源路由
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
sysctl -p

八、应用层安全

1. Web 服务器

  • Nginx:隐藏版本号(server_tokens off),限制访问频率,使用 HTTPS
  • Apache:同样隐藏版本,禁用目录浏览(Options -Indexes)
  • PHP:expose_php = Off,禁用危险函数
1
2
# /etc/php.ini
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\\_%';
# 限制 root 远程登录
UPDATE mysql.user SET Host='localhost' WHERE User='root';
FLUSH PRIVILEGES;

3. Redis

1
2
3
4
5
# redis.conf
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/

# 清理 7 天前的备份
find $BACKUP_DIR -type d -mtime +7 | xargs rm -rf

3. 快照(云服务器)

阿里云/腾讯云/AWS 都支持磁盘快照,定期创建可以在出问题后秒级恢复。

十、加固检查清单

项目优先级状态
SSH 密钥登录P0
禁用 root SSH 登录P0
修改 SSH 默认端口P1
配置 fail2banP1
配置防火墙P0
系统自动更新P1
入侵检测工具P1
集中日志审计P2
数据库安全配置P0
Redis 等服务不暴露公网P0
数据备份策略P0
内核参数加固P2

写在最后

安全是一个持续的过程,不是一次性任务。建议:

  1. 把这份清单打印出来,逐项打钩
  2. 每月做一次安全审计
  3. 关注 CVE 公告,关键漏洞要在 24 小时内修复
  4. 给关键服务配置告警(登录告警、异常流量告警等)

记住:没有绝对安全的系统,只有不断加固的过程。希望这份清单能帮你把服务器的安全等级提升一个台阶。