安全加固

SSL/防火墙/备份策略指南

资源 列表

可直接复制使用的 AI 提示词、脚本和代码

bash

服务器初始化安全脚本

适合在安全加固阶段作为脚本参考。执行前请先检查系统版本、目录路径、域名和权限配置,建议在测试环境验证后再用于生产服务器。

#!/bin/bash
# 服务器初始化安全配置

# 创建非 root 用户
adduser deploy
usermod -aG sudo deploy

# SSH 加固
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
systemctl restart sshd

# 防火墙
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable

# 安装 Fail2Ban
apt install fail2ban -y
systemctl enable fail2ban
systemctl start fail2ban

# 自动更新
apt install unattended-upgrades -y
dpkg-reconfigure -plow unattended-upgrades
bash

SSL 证书一键申请

适合在安全加固阶段作为脚本参考。执行前请先检查系统版本、目录路径、域名和权限配置,建议在测试环境验证后再用于生产服务器。

# 安装 Certbot
apt install certbot python3-certbot-nginx -y

# 申请证书(Nginx 模式)
certbot --nginx -d example.com -d www.example.com

# 续期测试
certbot renew --dry-run

# 查看证书状态
certbot certificates

# 自动续期(系统自动添加定时任务)
# 续期记录查看
grep Certbot /var/log/letsencrypt/letsencrypt.log