自建DDNS实现动态IP服务器访问及使用

搭建需求

  • 一个域名(已接入Cloudflare)

  • 一台服务器

获取 Global API Key

访问 Cloudflare 在页面下方找到 Global API Key,点击右侧的 View(查看) 查看 Key,并保存下来。

设置用于 DDNS 解析的二级域名,不开启小云朵

下载DDNS脚本

curl https://raw.githubusercontent.com/aipeach/cloudflare-api-v4-ddns/master/cf-v4-ddns.sh > /root/cf-v4-ddns.sh && chmod +x /root/cf-v4-ddns.sh

修改配置文件

vim cf-v4-ddns.sh
# incorrect api-key results in E_UNAUTH error
# 填写 Global API Key
CFKEY=

# Username, eg: user@example.com
# 填写 CloudFlare 登陆邮箱
CFUSER=

# Zone name, eg: example.com
# 填写需要用来 DDNS 的一级域名
CFZONE_NAME=

# Hostname to update, eg: homeserver.example.com
# 填写 DDNS 的二级域名
CFRECORD_NAME=

修改完成后,按esc,再输入;wq,保存文件即可

运行脚本,成功后前往Cloudflare查看域名指向的IP是否正确变更

./cf-v4-ddns.sh

设置定时任务

crontab -e
*/2 * * * * /root/cf-v4-ddns.sh >/dev/null 2>&1

# 如果需要日志,替换上一行代码
*/2 * * * * /root/cf-v4-ddns.sh >> /var/log/cf-ddns.log 2>&1

保存后退出即可

人生除死无大事