Cloudflare DNS
Manage DNS records via Cloudflare API using the bundled cf-dns.sh script.
Setup
Store credentials in environment or pass via flags:
CODEBLOCK0
Get API token: Cloudflare Dashboard → My Profile → API Tokens → Create Token → "Edit zone DNS" template.
Get Zone ID: Cloudflare Dashboard → select domain → Overview → right sidebar "Zone ID".
Usage
The script is at scripts/cf-dns.sh. All commands:
CODEBLOCK1
Common Patterns
Add subdomain pointing to IP:
CODEBLOCK2
Set up email (MX + SPF):
CODEBLOCK3
Dynamic DNS for home server:
CODEBLOCK4
Notes
- -
--proxied enables Cloudflare proxy (orange cloud) — hides origin IP, adds CDN - TTL in seconds; use 1 for "Auto" when proxied
- INLINECODE3 means root domain
- Script outputs JSON; pipe to
jq for parsing
Cloudflare DNS
通过使用内置的 cf-dns.sh 脚本,借助 Cloudflare API 管理 DNS 记录。
设置
将凭据存储在环境变量中,或通过标志传递:
bash
export CFAPITOKEN=your-api-token
export CFZONEID=your-zone-id # 可选,可从域名自动检测
获取 API 令牌:Cloudflare 仪表盘 → 我的个人资料 → API 令牌 → 创建令牌 → 编辑区域 DNS 模板。
获取区域 ID:Cloudflare 仪表盘 → 选择域名 → 概览 → 右侧边栏 区域 ID。
使用方法
脚本位于 scripts/cf-dns.sh。所有命令:
bash
列出区域(查找区域 ID)
cf-dns.sh zones
列出某个区域的所有记录
cf-dns.sh list
cf-dns.sh list --domain example.com
获取特定记录
cf-dns.sh get id> id>
创建记录
cf-dns.sh create --type A --name www --content 1.2.3.4 [--ttl 300] [--proxied]
cf-dns.sh create --type CNAME --name blog --content example.com
cf-dns.sh create --type TXT --name @ --content v=spf1 ...
cf-dns.sh create --type MX --name @ --content mail.example.com --priority 10
更新记录
cf-dns.sh update id> id> --content 5.6.7.8 [--ttl 600] [--proxied]
删除记录
cf-dns.sh delete id> id>
DDNS:将 A 记录更新为当前公网 IP
cf-dns.sh ddns --name home
cf-dns.sh ddns --domain example.com --name home
常见模式
添加指向 IP 的子域名:
bash
cf-dns.sh create --type A --name subdomain --content 203.0.113.50 --proxied
设置电子邮件(MX + SPF):
bash
cf-dns.sh create --type MX --name @ --content mail.example.com --priority 10
cf-dns.sh create id> --type TXT --name @ --content v=spf1 include:spf.google.com ~all
家庭服务器的动态 DNS:
bash
通过 cron 定期运行
cf-dns.sh ddns --domain example.com --name home
备注
- - --proxied 启用 Cloudflare 代理(橙色云朵)——隐藏源站 IP,增加 CDN
- TTL 以秒为单位;使用代理时,设置为 1 表示自动
- @ 表示根域名
- 脚本输出 JSON;可通过管道传递给 jq 进行解析