跳转至

第八章:安全加固

SSH 安全

配置文件

# /etc/ssh/sshd_config

# 禁用 root 登录
PermitRootLogin no

# 修改端口
Port 2222

# 禁用密码登录
PasswordAuthentication no

# 只允许密钥登录
PubkeyAuthentication yes

# 限制登录用户
AllowUsers user1 user2

# 重启服务
systemctl restart sshd

防火墙配置

# 只开放必要端口
ufw default deny incoming
ufw default allow outgoing
ufw allow 22
ufw allow 80
ufw allow 443
ufw enable

系统更新

# 更新系统
apt update && apt upgrade -y

# 自动安全更新
apt install unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades

安全检查

检查登录

# 查看登录历史
last
lastb  # 失败的登录

# 查看当前登录
who
w

检查可疑进程

# 查看网络连接
netstat -tulpn
ss -tulpn

# 查看监听端口
lsof -i

# 检查定时任务
crontab -l
cat /etc/crontab

入侵检测

# 安装 fail2ban
apt install fail2ban

# 配置
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

# 启动服务
systemctl enable fail2ban
systemctl start fail2ban

小结

安全加固要点:

  • SSH 安全:禁用 root、修改端口、密钥登录
  • 防火墙:只开放必要端口
  • 系统更新:定期更新
  • 安全检查:登录历史、可疑进程
  • 入侵检测:fail2ban

完成本教程后,你应该能够安全地管理 Linux 服务器。