我的运维笔记

第1讲 服务器安全加固

课程目标

将裸奔的Rocky Linux9服务器加固,配置SSH密钥登录、两层防护(安全组+主机防火墙)、fail2ban自动封禁暴力破解;掌握VNC自救手段,不修改SSH端口。

服务器两条登录通道

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

sshd_config关键配置项

PubkeyAuthentication yes
PasswordAuthentication no      # 关闭密码登录
PermitRootLogin no             # 禁止root远程SSH登录
MaxAuthTries 3
AllowUsers study teacher       # SSH登录白名单用户

fail2ban配置要点

配置文件路径:/etc/fail2ban/jail.d/sshd.local,不要修改jail.conf。Rocky9必须使用 backend = systemd,不能读取旧的/var/log/secure。

云服务器两层防护概念

ICMP协议(ping)没有端口,安全组显示“任何”只是占位符,不等于开放全部端口。防火墙永久规则必须加--permanent,再执行--reload才会永久生效。

运维三大习惯

  1. 私钥永不外传;
  2. 修改配置先留后路(VNC);
  3. 先验证,再收口;新登录方式验证成功,再关闭旧登录方式。

teacher巡检账号注意

只安装老师公钥,绝对不要把自己私钥发给别人。teacher无密码,不能加入wheel组,需要单独配置免密sudo,配置后执行 sudo visudo ‑c校验语法,防止sudo全部失效。

第2讲 网站上线:域名解析与Nginx个人主页

网站上线三大环节

  1. DNS域名解析:域名解析到服务器公网IP;
  2. 开门:安全组 + firewalld放行80端口;
  3. Nginx Web服务:接收浏览器请求,返回网页文件。

DNS解析A记录

# 验证域名解析
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
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

Nginx站点配置核心知识点

配置存放目录 /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

网站故障排查

合规要求:网站首页底部必须放置ICP备案号,链接工信部备案查询网站。网站上线30日内完成公安联网备案。

第3讲 HTTPS与证书自动续期

HTTP与HTTPS区别

HTTP明文传输,中间人可以直接抓取网页内容、表单账号密码。HTTPS = HTTP + TLS,实现两件事:传输内容加密;验证服务器真实身份,防止冒充。依靠CA机构颁发数字证书(网站身份证)。

使用Let’s Encrypt免费证书,有效期90天,未来缩短至45天,官方不再发送到期提醒,必须配置自动续期。

HTTP‑01域名验证前提条件

  1. 域名正确A记录解析到服务器;
  2. 80端口必须对外开放;
  3. Nginx可以访问 .well‑known/acme‑challenge 路径验证文件。

开放443(HTTPS)端口

sudo firewall‑cmd ‑‑permanent ‑‑add‑service=https
sudo firewall‑cmd ‑‑reload
sudo firewall‑cmd ‑‑list‑services
80端口不能关闭:① http访问跳转https;② 证书续期时Let’s Encrypt需要使用80端口做域名验证。

certbot申请证书命令

# 安装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

证书关键文件

Nginx HTTPS配置说明

自动续期配置

  1. 启用certbot‑renew.timer定时器,自动定期检查证书;
  2. deploy钩子脚本:证书续期成功后自动执行 systemctl reload nginx,加载新证书;
  3. 钩子脚本需要执行权限。
# 启用续期定时器
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域名信息明文传输;第三方可以看到访问哪个域名,但是看不到页面具体内容。

第4讲 Nginx多站点虚拟主机(子域名)

虚拟主机原理

单公网IP运行多个网站,依靠浏览器HTTP请求头的Host字段(请求域名),Nginx匹配不同的server_name,分发到对应的server块。一个网站对应一个server块。

Nginx进程模型

Nginx配置层级结构

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"

默认站点规则

  1. 请求域名匹配不到任何server_name,交给该端口的default_server;
  2. 没有设置default_server,使用本端口第一个加载的server块作为默认站点。

location匹配优先级(由高到低)

  1. location = /xxx 精确匹配(最高)
  2. location ^~ /xxx 前缀匹配,停止正则判断
  3. location ~ / ~*正则匹配,按书写顺序
  4. location /xxx普通前缀匹配,取最长匹配路径

acme验证路径 /.well‑known/acme‑challenge/ 比 location /路径更长,优先匹配,不会被301跳转。

子域名notes.example.com部署流程

  1. DNS添加A记录,主机记录notes指向服务器公网IP;
  2. 创建独立网站目录,权限755,属主study;
  3. 新建notes.conf配置文件,先写80端口配置,nginx‑t检查再reload;
  4. certbot单独为notes子域名申请证书(先‑‑dry‑run演练);
  5. 追加443 ssl server块,独立access_log、error_log,实现一站一日志;
一站一证书优点:各个站点证书互不干扰,删除子站点不会影响其他网站。certbot‑renew.timer自动管理服务器上全部证书的续期。

日志分离查看命令

sudo tail ‑f /var/log/nginx/mysite_access.log
sudo tail ‑f /var/log/nginx/notes_access.log