本文通过 ddns-go(github 关注数 10K+)项目,将动态公网 IPv4 或 IPv6 地址自动绑定到由域名服务商管理的域名,其实现原理:首先获取本地动态公网 IP 地址,然后通过域名服务商的 API 来管理 DNS 解析记录
ddns-go 支持的域名服务商: 阿里云、腾讯云、Dnspod、 Cloudflare、 华为云、 Callback、 百度云、 Porkbun、 GoDaddy、 Namecheap、 NameSilo、 Dynadot
本文以 NameSilo 为例,具体步骤如下
环境介绍
1》Rocky Linux release 8.9 (kernel 4.18.0)
2》ddns-go v6.6.3
安装、配置 ddns-go
1、下载解压,获取可运行二进制文件 ddns-go
wget https://github.com/jeessy2/ddns-go/releases/download/v6.6.3/ddns-go_6.6.3_linux_x86_64.tar.gz
tar xf ddns-go_6.6.3_linux_x86_64.tar.gz
mv ddns-go /usr/local/bin/
mkdir /etc/ddns-go
2、创建 ddns-go 配置文件
默认配置文件保存路径:/root/.ddns_go_config.yaml ,该配置文件内容可手动编辑,或通过 web 管理
cat /etc/ddns-go/ddns_go_config.yaml
dnsconf:
- name: ""
# ipv4 公网 IP 与 DNS 配置
ipv4:
enable: true
gettype: url # 通过如下配置的 url 获取公网 IPv4 地址
url: https://myip.ipip.net, https://ddns.oray.com/checkip, https://ip.3322.net, https://4.ipw.cn
netinterface: ens3
cmd: ""
domains:
- ""
ipv6:
enable: true
gettype: netInterface # 通过本机网卡 ppp0 获取公网 IPv6 地址
url: https://speed.neu6.edu.cn/getIP.php, https://v6.ident.me, https://6.ipw.cn
netinterface: ppp0
cmd: ""
ipv6reg: ""
domains:
# 通过 DNS 服务商 API 更新域名(demo.jiawen.com)解析到上面获取的 IPv6 地址
- demo.jiawen.com
dns:
# 设置域名 jiawen.com 托管的 dns 服务商为 namesilo
name: namesilo
id: ""
# namesilo api key 获取地址 https://www.namesilo.com/account/api-manager
secret: 24fbafc28671f73aed1f
ttl: ""
user: # ddns-go web 管理账号
username: admin
password: $2a$10$FfGa/rE0BXMcYBhLSjUPcOOcNpCODc3ZesFoXNR/a/FQhA73FHumW
webhook:
webhookurl: ""
webhookrequestbody: ""
webhookheaders: ""
notallowwanaccess: true
lang: zh
运行 ddns-go
1、手动运行 ddns-go
# 运行 ddns-go ,可通过选项 -noweb 禁用 web 管理
/usr/local/bin/ddns-go -l 0.0.0.0:9876 -f 300 -cacheTimes 5 -c /etc/ddns-go/ddns_go_config.yaml
#> 2024/07/14 05:25:10 监听 0.0.0.0:9876
#> 请手动打开浏览器并访问 http://127.0.0.1:9876 进行配置
#> 2024/07/14 05:25:14 "Remote: 192.168.31.2:40776" 登陆成功
#> 2024/07/14 05:26:18 配置文件已保存在: /etc/ddns-go/ddns_go_config.yaml
#> 2024/07/14 05:26:20 新增域名解析 demo.jiawen.com 成功! IP: 240e:891:ba21:1df5:2d0e:d489:d26b:4245
#> 2024/07/14 05:30:10 IPv6未改变, 将等待 5 次后与DNS服务商进行比对
#> ...
2、基于 systemd 运行 ddns-go(可选)
systemctl cat ddns-go.service
# /etc/systemd/system/ddns-go.service
[Unit]
Description=DDNS-Go Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/ddns-go -l 0.0.0.0:9876 -f 300 -cacheTimes 5 -c /etc/ddns-go/ddns_go_config.yaml
Restart=always
[Install]
WantedBy=multi-user.target
3、web 管理 http://127.0.0.1:9876 账号 admin admin@123
选择域名服务商、配置其 API key
配置 IPv6 地址获取方式,及其解析到哪个域名
查看解析结果
使用 dig -t AAAA demo.jiawen.com
查看解析结果
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> -t AAAA demo.jiawen.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50430
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;demo.jiawen.com. IN AAAA
;; ANSWER SECTION:
demo.jiawen.com. 10 IN AAAA 240e:891:ba21:1df5:2d0e:d489:d26b:4245
;; Query time: 208 msec
;; SERVER: 100.100.2.136#53(100.100.2.136)
;; WHEN: Sun Jul 14 08:31:50 CST 2024
;; MSG SIZE rcvd: 62
使用 ping6 demo.jiawen.com
进行连通性测试
PING demo.jiawen.com(240e:891:ba21:1df5:2d0e:d489:d26b:4245 (240e:891:ba21:1df5:2d0e:d489:d26b:4245)) 56 data bytes
64 bytes from 240e:891:ba21:1df5:2d0e:d489:d26b:4245 (240e:891:ba21:1df5:2d0e:d489:d26b:4245): icmp_seq=1 ttl=56 time=44.2 ms
64 bytes from 240e:891:ba21:1df5:2d0e:d489:d26b:4245 (240e:891:ba21:1df5:2d0e:d489:d26b:4245): icmp_seq=2 ttl=56 time=44.0 ms
64 bytes from 240e:891:ba21:1df5:2d0e:d489:d26b:4245 (240e:891:ba21:1df5:2d0e:d489:d26b:4245): icmp_seq=3 ttl=56 time=42.7 ms
64 bytes from 240e:891:ba21:1df5:2d0e:d489:d26b:4245 (240e:891:ba21:1df5:2d0e:d489:d26b:4245): icmp_seq=4 ttl=56 time=42.7 ms
64 bytes from 240e:891:ba21:1df5:2d0e:d489:d26b:4245 (240e:891:ba21:1df5:2d0e:d489:d26b:4245): icmp_seq=5 ttl=56 time=42.9 ms
...
Site 可用性检测
每隔 5 分钟检测该站点是否可用
cat check_site.sh
#!/bin/bash
# 设置站点 URL
SITE_URL="http://demo.jiawen.com"
# 检查站点是否可访问
check_site() {
if curl -s --head "$SITE_URL" | grep "200 OK" > /dev/null; then
echo "$(date): $SITE_URL 可访问"
else
echo "$(date): $SITE_URL 不可访问"
fi
}
# 每隔一段时间执行检查
while true; do
check_site
sleep 300 # 5分钟
done
./check_site.sh 运行日志
Sun Jul 14 06:15:22 CST 2024: http://demo.jiawen.com 可访问
Sun Jul 14 06:20:22 CST 2024: http://demo.jiawen.com 可访问
Sun Jul 14 06:25:22 CST 2024: http://demo.jiawen.com 可访问
Sun Jul 14 06:30:23 CST 2024: http://demo.jiawen.com 可访问
Sun Jul 14 06:35:23 CST 2024: http://demo.jiawen.com 可访问
...