我的运维笔记

第1讲 服务器安全加固

课程目标

重要运维习惯

暴力破解查看日志命令

sudo journalctl -u sshd | grep -i "failed" | tail -30

服务器两条登录通道

  1. SSH通道(Workbench):走公网22端口,受sshd配置控制;sshd配置出错会直接断开,不能作为后路。
  2. VNC带外管理通道:模拟显示器,不走安全组、不受sshd影响;修改SSH配置出错时的救命后路;VNC登录终端为 /dev/tty1,SSH登录是伪终端 /dev/pts/0。

SSH密钥认证原理

生成一对密钥:

新建普通用户

sudo adduser study
sudo passwd study
sudo usermod -aG wheel study

本地生成密钥对(Windows PowerShell / Mac/Linux终端)

ssh-keygen -t ed25519 -C "你的名字-服务器加固"

查看公钥:

Windows:type $env:USERPROFILE\.ssh\id_ed25519.pub

Mac/Linux:cat ~/.ssh/id_ed25519.pub

服务器配置公钥目录与权限(权限错误会直接拒绝密钥登录)

mkdir -p ~/.ssh
chmod 700 ~/.ssh
vi ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
ls -l ~/.ssh
注意:authorized_keys权限必须为600,.ssh目录700,属主必须是对应用户。

测试密钥登录

ssh study@服务器公网IP

✅ 不输入密码直接登录,代表密钥配置成功;必须验证成功之后,再关闭密码登录。

巡检账号teacher配置要点

sudo adduser teacher
sudo mkdir -p /home/teacher/.ssh
sudo vi /home/teacher/.ssh/authorized_keys
sudo chmod 700 /home/teacher/.ssh
sudo chmod 600 /home/teacher/.ssh/authorized_keys
sudo chown -R teacher:teacher /home/teacher/.ssh

echo "teacher ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/teacher
sudo chmod 440 /etc/sudoers.d/teacher
sudo visudo -c
sudo -u teacher sudo -n true && echo "teacher 免密 sudo 可用"

修改sshd_config配置

编辑 /etc/ssh/sshd_config,修改/新增:

PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin no
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers study teacher

检查配置语法,重载sshd:

sudo sshd -t
sudo systemctl reload sshd

两层防护:安全组 + firewalld主机防火墙

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
ICMP协议没有端口概念;安全组显示“任何端口”只是占位符,不等于开放全部端口,ping依赖ICMP。

fail2ban自动封禁暴力破解IP

sudo dnf install -y epel-release
sudo dnf install -y fail2ban

新建配置文件 /etc/fail2ban/jail.d/sshd.local

[sshd]
enabled = true
backend = systemd
maxretry = 3
findtime = 600
bantime = 3600
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

第1讲排错自救要点

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

课程目标

DNS域名解析

nslookup 你的域名
nslookup www.你的域名

放行HTTP 80端口

阿里云安全组入方向开放TCP 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
ls -ld /var/www/mysite
阿里云Rocky镜像默认关闭SELinux;其他环境遇到403要考虑SELinux。

Nginx站点配置文件 /etc/nginx/conf.d/mysite.conf

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;
    }
}

修改配置必须语法校验再重载:

sudo nginx -t
sudo systemctl reload nginx
Nginx依靠HTTP请求头Host字段,结合server_name区分不同网站;IP访问不携带域名Host,会命中默认站点,显示Nginx默认页面。

上传网页文件scp命令示例

scp index.html study@服务器公网IP:/var/www/mysite/

403 Forbidden常见原因

查看网站访问日志

sudo tail -f /var/log/nginx/mysite_access.log

网站故障排查思路

  1. DNS:nslookup检查解析是否正确。
  2. 网络端口:安全组、firewalld确认80端口放行。
  3. Nginx:配置、文件权限、查看error_log。

第3讲 HTTPS与证书自动续期

课程目标

HTTP明文风险

HTTP全部内容明文传输,中间网络节点可以窃听请求内容;HTTPS = HTTP + TLS加密,完成两件事:传输内容加密;服务器身份认证(数字证书充当网站身份证)。

sudo dnf install -y tcpdump
sudo tcpdump -i any -A -nn 'tcp port 80' | grep -a -E "GET |Host:|User‑Agent|"
</code></pre>

    <h3>Let’s Encrypt证书要点</h3>
    <ul>
        <li>免费公益CA;证书有效期90天,未来缩短至45天,不再发送到期提醒,自动续期必须配置。</li>
        <li>HTTP‑01验证前提条件:域名解析正确、80端口开放、Nginx可以访问验证文件。</li>
    </ul>

    <h3>开放HTTPS 443端口</h3>
    <p>安全组开放TCP 443;firewalld配置:</p>
<pre><code>sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
</code></pre>
    <blockquote>⚠️ 80端口<strong>不能关闭</strong>:① 把http访问301跳转https;② 证书续期需要80端口做域名验证。</blockquote>

    <h3>安装certbot工具</h3>
<pre><code>sudo dnf install -y certbot
certbot --version
</code></pre>

    <h3>演练申请证书(--dry-run,测试环境,不计入失败次数)</h3>
<pre><code>sudo certbot certonly --webroot -w /var/www/mysite \
‑d example.com ‑d www.example.com \
‑m 你的邮箱 --agree-tos --no-eff-email --dry-run
</code></pre>
    <p>演练成功,去掉--dry‑run正式申请证书。</p>

    <h3>查看证书信息</h3>
<pre><code>sudo certbot certificates
sudo ls -l /etc/letsencrypt/live/example.com/
sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -issuer -dates -ext subjectAltName
</code></pre>
    <ul>
        <li><code>fullchain.pem</code>:证书+中间证书,发送给浏览器。</li>
        <li><code>privkey.pem</code>:证书私钥,服务器解密使用,严禁泄露。</li>
    </ul>

    <h3>HTTPS Nginx完整配置(mysite.conf)</h3>
<pre><code>#80端口,跳转https,同时给证书续期提供验证文件
server {
    listen 80;
    server_name example.com www.example.com;
    location /.well‑known/acme‑challenge/ {
        root /var/www/mysite;
    }
    location / {
        return 301 https://$host$request_uri;
    }
}

#443端口提供加密网站访问
server {
    listen 443 ssl;
    server_name example.com www.example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    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;
    }
}
</code></pre>
<pre><code>sudo nginx -t
sudo systemctl reload nginx
</code></pre>

    <h3>配置证书自动续期</h3>
<pre><code>sudo systemctl enable --now certbot-renew.timer
systemctl list-timers 'certbot*'
</code></pre>
    <p>创建deploy钩子脚本 <code>/etc/letsencrypt/renewal‑hooks/deploy/reload‑nginx.sh</code></p>
<pre><code>#!/bin/bash
systemctl reload nginx
</code></pre>
<pre><code>sudo chmod +x /etc/letsencrypt/renewal‑hooks/deploy/reload‑nginx.sh
sudo /etc/letsencrypt/renewal‑hooks/deploy/reload‑nginx.sh
systemctl is-active nginx
#模拟续期演练
sudo certbot renew --dry-run
</code></pre>

    <h3>HTTPS抓包对比</h3>
<pre><code>sudo tcpdump -i any -A -nn -c 40 'tcp port 443'
</code></pre>
    <blockquote>HTTPS加密HTTP内容,但是握手阶段SNI域名是明文;加密传输内容,不加密访问的目标域名。</blockquote>

  <footer style="text-align:center;padding:20px;font-size:14px;">
    <a href="https://beian.miit.gov.cn/" target="_blank">浙ICP备2026083023号</a>
</footer>
</body>
</html>