将裸奔的Rocky Linux9服务器加固,配置SSH密钥登录、两层防护(安全组+主机防火墙)、fail2ban自动封禁暴力破解;掌握VNC自救手段,不修改SSH端口。
/dev/tty1/dev/pts/*~/.ssh/authorized_keys,可以公开。~/.ssh目录权限:700authorized_keys文件权限:600,属主必须是对应用户,权限过松SSH拒绝密钥登录。# 生成ed25519密钥对
ssh‑keygen ‑t ed25519 ‑C "服务器加固备注"
# 查看ssh登录失败爆破日志
sudo journalctl ‑u sshd | grep ‑i "failed" | tail ‑30
# 测试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
# 安装fail2ban
sudo dnf install ‑y epel‑release
sudo dnf install ‑y fail2ban
sudo systemctl enable ‑‑now fail2ban
sudo fail2ban‑client status sshd
PubkeyAuthentication yes
PasswordAuthentication no # 关闭密码登录
PermitRootLogin no # 禁止root远程SSH登录
MaxAuthTries 3
AllowUsers study teacher # SSH登录白名单用户
配置文件路径:/etc/fail2ban/jail.d/sshd.local,不要修改jail.conf。Rocky9必须使用 backend = systemd,不能读取旧的/var/log/secure。
只安装老师公钥,绝对不要把自己私钥发给别人。teacher无密码,不能加入wheel组,需要单独配置免密sudo,配置后执行 sudo visudo ‑c校验语法,防止sudo全部失效。
@代表主域名;www代表www子域名;# 验证域名解析
nslookup 你的域名
nslookup www.你的域名
# firewalld放行http(80端口)
sudo firewall‑cmd ‑‑permanent ‑‑add‑service=http
sudo firewall‑cmd ‑‑reload
sudo firewall‑cmd ‑‑list‑services
sudo dnf install ‑y nginx
sudo systemctl enable ‑‑now nginx
systemctl status nginx
sudo nginx ‑t
sudo systemctl reload nginx
sudo mkdir ‑p /var/www/mysite
sudo chown ‑R study:study /var/www/mysite
sudo chmod 755 /var/www/mysite
配置存放目录 /etc/nginx/conf.d/*.conf,全部会被自动加载。server_name匹配HTTP请求的Host请求头,区分不同网站。IP访问没有匹配的server_name,会访问系统默认站点,看到Nginx默认页面。
server {
listen 80;
server_name example.com www.example.com;
root /var/www/mysite;
index index.html;
access_log /var/log/nginx/mysite_access.log;
error_log /var/log/nginx/mysite_error.log;
location / {
try_files $uri $uri/ =404;
}
}
# scp本地文件上传服务器
scp index.html study@服务器IP:/var/www/mysite/
# 服务器内直接编辑
vi /var/www/mysite/index.html
HTTP明文传输,中间人可以直接抓取网页内容、表单账号密码。HTTPS = HTTP + TLS,实现两件事:传输内容加密;验证服务器真实身份,防止冒充。依靠CA机构颁发数字证书(网站身份证)。
使用Let’s Encrypt免费证书,有效期90天,未来缩短至45天,官方不再发送到期提醒,必须配置自动续期。
sudo firewall‑cmd ‑‑permanent ‑‑add‑service=https
sudo firewall‑cmd ‑‑reload
sudo firewall‑cmd ‑‑list‑services
# 安装certbot
sudo dnf install ‑y certbot
# 演练模式 --dry‑run,不消耗申请次数,正式申请前必做
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
# 模拟证书续期演练
sudo certbot renew ‑‑dry‑run
fullchain.pem:网站证书+中间证书,发给浏览器;privkey.pem:证书私钥,服务器本地保存,泄露会被冒充网站,严禁外传。systemctl reload nginx,加载新证书;# 启用续期定时器
sudo systemctl enable ‑‑now certbot‑renew.timer
systemctl list‑timers 'certbot*'
# deploy钩子脚本 /etc/letsencrypt/renewal‑hooks/deploy/reload‑nginx.sh
#!/bin/bash
systemctl reload nginx
# 添加执行权限
sudo chmod +x /etc/letsencrypt/renewal‑hooks/deploy/reload‑nginx.sh
HTTPS加密网页传输内容,但是建立连接时SNI域名信息明文传输;第三方可以看到访问哪个域名,但是看不到页面具体内容。
单公网IP运行多个网站,依靠浏览器HTTP请求头的Host字段(请求域名),Nginx匹配不同的server_name,分发到对应的server块。一个网站对应一个server块。
nginx.conf → events{} → http{} → server{} → location{}。外层配置内层继承,内层同名配置会覆盖外层。include /etc/nginx/conf.d/*.conf;加载conf.d目录所有conf配置文件。
# 查看完整合并后的全部生效配置
sudo nginx ‑T
# 筛选监听端口、server_name
sudo nginx ‑T 2>/dev/null | grep ‑E "listen|server_name"
location = /xxx 精确匹配(最高)location ^~ /xxx 前缀匹配,停止正则判断location ~ / ~*正则匹配,按书写顺序location /xxx普通前缀匹配,取最长匹配路径acme验证路径 /.well‑known/acme‑challenge/ 比 location /路径更长,优先匹配,不会被301跳转。
sudo tail ‑f /var/log/nginx/mysite_access.log
sudo tail ‑f /var/log/nginx/notes_access.log