sudo journalctl -u sshd | grep -i "failed" | tail -30
sudo adduser study sudo passwd study sudo usermod -aG wheel study
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
ssh study@服务器公网IP
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 可用"
修改/新增配置项:
sudo sshd -t sudo systemctl reload sshd
sudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --reload sudo firewall-cmd --list-all
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
@代表裸域名 example.com;www代表 www.example.comnslookup 你的域名 nslookup www.你的域名
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 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
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
scp index.html study@你的服务器公网IP:/var/www/mysite/
curl -H "Host: 你的域名" http://127.0.0.1 curl http://127.0.0.1 | head -5
网页文件权限最低 644;网站目录权限最低 755。Nginx工作进程以nginx用户运行。查看错误日志定位Permission denied。
sudo tail -3 /var/log/nginx/mysite_error.log
sudo tail -f /var/log/nginx/mysite_access.log
sudo dnf install -y tcpdump sudo tcpdump -i any -A -nn 'tcp port 80' | grep -a -E "GET |Host:|User-Agent|<title>"
sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload sudo firewall-cmd --list-services
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 cp /etc/nginx/conf.d/mysite.conf /etc/nginx/conf.d/mysite.conf.bak
# 80端口,ACME验证 + 301跳转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 https站点
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;
}
}
sudo nginx -t sudo systemctl reload nginx
sudo systemctl enable --now certbot-renew.timer systemctl list-timers 'certbot*'
编辑 /etc/letsencrypt/renewal‑hooks/deploy/reload‑nginx.sh
#!/bin/bash systemctl reload nginx
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh sudo /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
sudo certbot renew --dry-run
sudo tcpdump -i any -A -nn -c 40 'tcp port 443'