我的运维笔记

📑 快速跳转

第 1 讲:服务器安全加固

−

本章做了什么

关键命令

#查看ssh登录失败日志
sudo journalctl -u sshd | grep -i "failed" | tail -30

#本地电脑生成密钥对
ssh-keygen -t ed25519 -C "你的名字-服务器加固"

#新建普通用户
sudo adduser study
sudo passwd study
sudo usermod -aG wheel study

#密钥目录与文件权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

#校验sshd配置,重载ssh服务
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

#启动fail2ban,查看封禁IP
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

#检查sudo配置语法
sudo visudo -c

遇到的问题与解决办法

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

−

本章做了什么

关键命令

#本地验证域名解析
nslookup 你的域名
nslookup www.你的域名

#防火墙放行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

#校验nginx配置,重载服务
sudo nginx -t
sudo systemctl reload nginx

#本地电脑scp上传网页
scp index.html study@服务器IP:/var/www/mysite/

#模拟指定Host头访问网站
curl -H "Host:你的域名" http://127.0.0.1

#实时跟踪网站访问日志
sudo tail -f /var/log/nginx/mysite_access.log

#查看nginx错误日志排查403
sudo tail -3 /var/log/nginx/mysite_error.log

遇到的问题与解决办法

第 3 讲:HTTPS与证书自动续期

−

本章做了什么

关键命令

#安装tcpdump抓取http明文流量
sudo dnf install -y tcpdump
sudo tcpdump -i any -A -nn 'tcp port 80' | grep -a -E "GET |Host:|User‑Agent|<title>"

#防火墙放行https服务
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-services

#安装certbot证书工具
sudo dnf install -y certbot
certbot --version

#演练申请证书
sudo certbot certonly --webroot -w /var/www/mysite \
-d example.com -d www.example.com \
-m 邮箱 --agree-tos --no-eff-email --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 ls -l /etc/letsencrypt/live/example.com/

#查看证书详细信息
sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -issuer -dates -ext subjectAltName

#启用证书自动续期定时器
sudo systemctl enable --now certbot-renew.timer
systemctl list-timers 'certbot*'

#为deploy钩子脚本添加执行权限
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

#模拟演练证书续期
sudo certbot renew --dry-run

#抓取https443端口流量
sudo tcpdump -i any -A -nn -c 40 'tcp port 443'

遇到的问题与解决办法