我的运维笔记

第1讲 服务器安全加固

核心目标

把裸奔服务器加固:密钥登录、两层防护、fail2ban自动封禁暴力破解;VNC作为自救后路。

三大运维习惯:私钥永不外传、改配置先留后路、先验证再收口。

1.暴力破解现象查看

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

2.两条登录通道

3.SSH密钥认证原理

4.新建普通用户study

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

5.本地电脑生成ed25519密钥对

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

Windows查看公钥:type $env:USERPROFILE\.ssh\id_ed25519.pub

Mac/Linux查看公钥:cat ~/.ssh/id_ed25519.pub

6.服务器配置公钥,严格权限(权限过松SSH拒绝密钥登录)

mkdir -p ~/.ssh
chmod 700 ~/.ssh
vi ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
必须先用密钥登录验证成功,再关闭密码登录,防止锁自己门外。

7.老师巡检账号teacher(无密码,免密sudo)

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 可用"

8.修改sshd_config加固SSH

编辑 sudo vi /etc/ssh/sshd_config 修改/新增:

sudo sshd -t
sudo systemctl reload sshd

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

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

ICMP协议没有端口概念;安全组显示“任何”不是开放全部端口,ping依赖ICMP。

sudo ss -tlnp

10.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

常见自救要点

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

核心流程

DNS域名解析 → 安全组&防火墙开放80端口 → Nginx部署个人网站,首页放置ICP备案链接。

DNS域名解析

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

开放80端口(HTTP)两层开门

阿里云安全组入方向开放TCP:80;firewalld放行http服务。

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

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;
    }
}
修改Nginx配置,先语法检查再重载,避免网站中断。
sudo nginx -t
sudo systemctl reload nginx

上传网页两种方式

1)本地scp上传

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

2)服务器vi直接编辑

vi /var/www/mysite/index.html

server_name与Host请求头原理

Nginx根据HTTP请求头Host匹配server_name;IP访问时Host不是域名,匹配不到自定义站点,返回Nginx默认页面。

curl -H "Host:你的域名" http://127.0.0.1
curl http://127.0.0.1 | head -5

403 Forbidden排错

查看网站访问日志

sudo tail -f /var/log/nginx/mysite_access.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验证:CA访问http域名下的验证文件,确认服务器属于申请者。申请证书前提:域名解析正确、80端口开放、Nginx配置正确。</li>
    </ul>

    <h3>开放443端口(HTTPS)</h3>
    <p>安全组入方向TCP443放行;firewalld添加https服务;<strong>80端口不能关闭:用于http跳转https,同时证书续期需要80端口验证。</strong></p>
<pre><code>sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services</code></pre>

    <h3>安装certbot,演练再正式申请证书</h3>
<pre><code>sudo dnf install -y certbot
certbot --version

#演练--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</code></pre>

    <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>
    <div class="tip"><code>privkey.pem</code>证书私钥,绝不外泄,泄露会被冒充网站。和SSH私钥性质一致。</div>

    <h3>Nginx HTTPS完整配置</h3>
    <p>80端口只做ACME验证 + 301重定向跳转HTTPS;443 ssl对外提供网站。</p>
<pre><code>#备份旧配置
sudo cp /etc/nginx/conf.d/mysite.conf /etc/nginx/conf.d/mysite.conf.bak</code></pre>
<pre><code>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;
    }
}

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>
    <p>certbot‑renew.timer定时器到期前自动更新证书;deploy钩子脚本续期成功重载Nginx读取新证书。</p>
<pre><code>sudo systemctl enable --now certbot-renew.timer
systemctl list-timers 'certbot*'</code></pre>
<p>编写钩子脚本 <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>
    <ul>
        <li>HTTPS加密HTTP载荷内容(请求、网页内容)</li>
        <li>SNI明文传递访问域名;别人仍然能知道访问哪个域名,看不到传输内容。</li>
    </ul>

    <footer>
        <a href="https://beian.miit.gov.cn/" target="_blank">浙ICP备2026083108号-1</a>
    </footer>
</body>
</html>