内容导航

2026/5/14

服务器安全日报与 ntfy 告警

记录云服务器日报脚本如何检查服务、端口、资源和 SSH 登录状态,并通过 ntfy 的 server-alert 用户推送到多端设备。

自动化工具 服务器安全自动化ntfy
本文目录

这篇只记录服务器日报和 ntfy 告警。ntfy 的短信、n8n 和跨设备文本通道属于同一套通知基础设施,但本文聚焦在服务器监控。

检查内容

日报检查:

服务:nginx / frps / ntfy / rustdesk-hbbs / rustdesk-hbbr
入口:主站 / n8n / 小程序 / FRP 本地入口 / ntfy health
资源:负载 / 内存 / 磁盘
安全:SSH 失败登录 / 最近登录摘要
端口:关键监听端口 / 非预期监听端口
备案:fallback 页面是否存在

ntfy 权限

使用发送专用用户:

server-alert -> write-only j-alert-<random>

授权:

sudo ntfy access server-alert "j-alert-<random>" wo

接收端使用主账号订阅 j-alert-*

环境文件

/etc/server-report.env

示例:

BLOG_HOST=example.com
N8N_HOST=n8n.example.com
MINI_HOST=mini.example.com
FRP_LOCAL_PORT=9002
BEIAN_FILE=/var/www/beian/index.html

NTFY_URL=http://127.0.0.1:2587
NTFY_TOPIC=j-alert-<random>
NTFY_USER=server-alert
NTFY_PASS=<server-alert-password>

权限:

sudo chmod 600 /etc/server-report.env

脚本

位置:

/usr/local/bin/server-daily-report.sh

手动测试:

sudo /usr/local/bin/server-daily-report.sh

发送核心:

curl -sS \
  -u "${NTFY_USER}:${NTFY_PASS}" \
  -H "Title: 云服务器安全日报|${OVERALL}" \
  -H "Priority: ${PRIORITY}" \
  -H "Tags: warning" \
  -d "$REPORT" \
  "${NTFY_URL}/${NTFY_TOPIC}"

端口白名单

白名单只放已知端口,例如:

22
80
443
7000
7500
2586
2587
9001
9002
21115
21116
21117
21118
21119

其中:

  • 7500 是 frps dashboard,本机监听时可接受。
  • 2586 是 ntfy 临时公网入口,域名 HTTPS 后关闭。
  • 21118/21119 是 RustDesk WebSocket/兼容端口,可在安全组关闭公网访问后作为已知本机监听。
  • 9001/9002 是临时入口,正式域名后关闭。

定时任务

每天 9 点:

sudo crontab -e
0 9 * * * /usr/local/bin/server-daily-report.sh >> /var/log/server-daily-report.log 2>&1

日志:

sudo tail -n 80 /var/log/server-daily-report.log

排障

401 unauthorized:账号或密码错误。

sudo ntfy user change-pass server-alert
sudo nano /etc/server-report.env

403 forbidden:topic 权限错误。

sudo ntfy access server-alert "j-alert-<random>" wo

ntfy 健康检查失败:

systemctl status ntfy --no-pager
curl -I http://127.0.0.1:2587/v1/health

没有自动发送:

sudo crontab -l
sudo tail -n 80 /var/log/server-daily-report.log

评论

Giscus 评论尚未配置。填写 GitHub Discussions 的仓库和分类 ID 后,这里会显示评论区。