对 Rocky Linux9 服务器做安全加固:配置SSH密钥登录、安全组+主机防火墙两层防护、fail2ban自动封禁暴力破解,掌握VNC自救方式。
# 查看ssh登录失败日志
sudo journalctl -u sshd | grep -i "failed" | tail -30
# 创建普通用户study,加入wheel组获得sudo权限
sudo adduser study
sudo passwd study
sudo usermod -aG wheel study
# 本地电脑生成ed25519密钥对
ssh-keygen -t ed25519 -C "名字-服务器加固"
# 设置ssh密钥目录与文件权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# 测试sshd配置语法,重载配置
sudo sshd -t
sudo systemctl reload sshd
# firewalld永久放行ssh服务并生效
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
# 安装epel源与fail2ban
sudo dnf install -y epel-release
sudo dnf install -y fail2ban
sudo systemctl enable --now fail2ban
# 查看fail2ban的sshd防护状态
sudo fail2ban-client status sshd
# 检查sudo配置语法,防止sudo配置写错全部sudo失效
sudo visudo -c
配置DNS域名解析,安装Nginx,发布带ICP备案号的静态个人主页;排查403、404、连接超时等网站故障。
# nslookup验证域名解析
nslookup 你的域名
nslookup www.你的域名
# firewalld放行http(80端口)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
# 安装启动Nginx
sudo dnf install -y nginx
sudo systemctl enable --now nginx
systemctl status nginx
nginx -v
# 创建网站目录,修改属主权限
sudo mkdir -p /var/www/mysite
sudo chown -R study:study /var/www/mysite
sudo chmod 755 /var/www/mysite
# Nginx配置语法检查、重载
sudo nginx -t
sudo systemctl reload nginx
# scp从本地上传网页文件到服务器
scp index.html study@服务器公网IP:/var/www/mysite/
# 实时跟踪Nginx访问日志
sudo tail -f /var/log/nginx/mysite_access.log
# curl模拟指定Host头访问网站
curl -H "Host:你的域名" http://127.0.0.1
# 查看SELinux状态,阿里云Rocky默认为Disabled
getenforce
理解HTTP明文风险、HTTPS/TLS证书原理;使用certbot申请Let's Encrypt免费证书;配置Nginx HTTPS;配置证书自动续期。
# 安装抓包工具,抓80端口http流量
sudo dnf install -y tcpdump
sudo tcpdump -i any -A -nn 'tcp port 80'
# firewalld放行https(443端口)
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
# 安装certbot工具
sudo dnf install -y certbot
certbot --version
# certbot演练申请证书(webroot模式)
sudo certbot certonly --webroot -w /var/www/mysite \
-d example.com -d www.example.com \
-m 邮箱 --agree-tos --no-eff-email --dry-run
# 正式申请证书(去掉--dry-run)
sudo certbot certonly --webroot -w /var/www/mysite \
-d example.com -d www.example.com \
-m 邮箱 --agree-tos --no-eff-email
# 查看本机所有证书信息
sudo certbot certificates
# 使用openssl查看证书详情
sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem \
-noout -issuer -dates -ext subjectAltName
# 启用certbot自动续期定时器
sudo systemctl enable --now certbot-renew.timer
systemctl list-timers 'certbot*'
# 模拟演练证书续期
sudo certbot renew --dry-run