Hugo博客公告弹窗

自建 ntfy 通知服务 + 服务器流量日报自动推送

项目文档:https://docs.ntfy.sh/

环境:Debian 12 测试通过

概述

三步搞定:

  1. 手机安装 ntfy 客户端
  2. 服务器自建 ntfy 通知服务
  3. 下载流量推送脚本,配置定时任务

搭建 ntfy 服务器

第 1 步:添加官方仓库并安装(Debian/Ubuntu, amd64)

sudo mkdir -p /etc/apt/keyrings
sudo curl -L -o /etc/apt/keyrings/ntfy.gpg https://archive.ntfy.sh/apt/keyring.gpg
sudo apt-get install -y apt-transport-https
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/ntfy.gpg] https://archive.ntfy.sh/apt stable main" \
  | sudo tee /etc/apt/sources.list.d/ntfy.list
sudo apt-get update
sudo apt-get install -y ntfy
ntfy -v

第 2 步:配置监听端口

注意:这里配置端口为 2586

sudo sed -i 's|^# listen-http: ":80"|listen-http: ":2586"|' /etc/ntfy/server.yml
grep -n 'listen-http' /etc/ntfy/server.yml

第 3 步:启动并设置开机自启

sudo systemctl enable ntfy
sudo systemctl start ntfy
sudo systemctl status ntfy --no-pager | head -10

第 4 步:绑定域名(可选,推荐)+ Caddy 反代 + HTTPS

Caddy 教程:https://blog.lufei.de/p/530/

配置流量日报推送

第 1 步:安装 vnstat(流量统计)

sudo apt-get update && sudo apt-get install -y vnstat
sudo systemctl enable --now vnstat
vnstat --version

第 2 步:确认主网卡名称

ip -o link show | awk -F': ' '{print $2}' | grep -v '^lo$'

记下第一个非 docker 的网卡名,下一步用到。云服务器一般是 eth0(阿里云/腾讯云)、ens3/ens5(部分 KVM)、enp1s0(部分 VPS)。

第 3 步:下载流量日报脚本

记得按需修改脚本

sudo curl -o /usr/local/bin/traffic-report.sh \
  https://raw.githubusercontent.com/woniu336/open_shell/main/traffic-report.sh

给执行权限:

sudo chmod +x /usr/local/bin/traffic-report.sh

修改脚本

nano /usr/local/bin/traffic-report.sh

第 4 步:测试 + 配置定时任务(每天 10:00)

手动测试,手机/订阅端应能收到推送:

sudo /usr/local/bin/traffic-report.sh

加入 crontab(每天 10:00):

(crontab -l 2>/dev/null; echo "0 10 * * * /usr/local/bin/traffic-report.sh") | crontab -

确认已添加:

crontab -l
CC BY-NC-SA 4.0 转载请注明
最后更新于 2026-08-01 05:49