dns-management
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDNS Management
DNS管理
Configure and manage DNS zones, records, and resolution for production infrastructure.
为生产环境基础设施配置和管理DNS区域、记录与解析。
When to Use
适用场景
- Setting up domains for web applications, APIs, and email.
- Migrating DNS providers or consolidating zones.
- Configuring DNS for CDN, load balancers, and cloud services.
- Troubleshooting resolution failures, propagation delays, or misconfigurations.
- Implementing DNSSEC, SPF, DKIM, and DMARC for email security.
- 为Web应用、API和邮箱配置域名。
- 迁移DNS服务商或整合DNS区域。
- 为CDN、负载均衡器和云服务配置DNS。
- 排查解析失败、传播延迟或配置错误问题。
- 为邮箱安全部署DNSSEC、SPF、DKIM和DMARC。
Prerequisites
前置条件
- Domain registered with a registrar (Namecheap, Route53, Google Domains, Cloudflare).
- Access to DNS provider dashboard or API.
- AWS CLI configured (for Route53 examples).
- and
digavailable locally (included in most OS installs).nslookup
- 已通过域名注册商(Namecheap、Route53、Google Domains、Cloudflare)注册域名。
- 拥有DNS服务商控制台或API访问权限。
- 已配置AWS CLI(用于Route53示例)。
- 本地环境已安装和
dig(多数操作系统默认包含)。nslookup
DNS Record Types Reference
DNS记录类型参考
| Type | Purpose | Example Value |
|---|---|---|
| A | IPv4 address | |
| AAAA | IPv6 address | |
| CNAME | Alias to another domain | |
| MX | Mail server with priority | |
| TXT | Arbitrary text (SPF, DKIM, verification) | |
| NS | Authoritative name servers | |
| SRV | Service location (host, port, priority) | |
| CAA | Certificate Authority Authorization | |
| PTR | Reverse DNS lookup | |
| 类型 | 用途 | 示例值 |
|---|---|---|
| A | IPv4地址 | |
| AAAA | IPv6地址 | |
| CNAME | 指向其他域名的别名 | |
| MX | 带优先级的邮件服务器 | |
| TXT | 任意文本(SPF、DKIM、验证) | |
| NS | 权威域名服务器 | |
| SRV | 服务位置(主机、端口、优先级) | |
| CAA | 证书颁发机构授权 | |
| PTR | 反向DNS查询 | |
AWS Route 53
AWS Route 53
Hosted Zone Management
托管区域管理
bash
undefinedbash
undefinedCreate a hosted zone
创建托管区域
aws route53 create-hosted-zone
--name example.com
--caller-reference "$(date +%s)"
--name example.com
--caller-reference "$(date +%s)"
aws route53 create-hosted-zone
--name example.com
--caller-reference "$(date +%s)"
--name example.com
--caller-reference "$(date +%s)"
List hosted zones
列出托管区域
aws route53 list-hosted-zones
aws route53 list-hosted-zones
Get name servers for a zone (update at your registrar)
获取区域的域名服务器(需在注册商处更新)
aws route53 get-hosted-zone --id Z1234567890ABC
--query 'DelegationSet.NameServers'
--query 'DelegationSet.NameServers'
undefinedaws route53 get-hosted-zone --id Z1234567890ABC
--query 'DelegationSet.NameServers'
--query 'DelegationSet.NameServers'
undefinedCreate and Manage Records
创建与管理记录
bash
undefinedbash
undefinedCreate an A record
创建A记录
aws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "app.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "93.184.216.34"}] } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "app.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "93.184.216.34"}] } }] }'
aws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "app.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "93.184.216.34"}] } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "app.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "93.184.216.34"}] } }] }'
Create a CNAME record
创建CNAME记录
aws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "www.example.com", "Type": "CNAME", "TTL": 300, "ResourceRecords": [{"Value": "example.com"}] } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "www.example.com", "Type": "CNAME", "TTL": 300, "ResourceRecords": [{"Value": "example.com"}] } }] }'
aws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "www.example.com", "Type": "CNAME", "TTL": 300, "ResourceRecords": [{"Value": "example.com"}] } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "www.example.com", "Type": "CNAME", "TTL": 300, "ResourceRecords": [{"Value": "example.com"}] } }] }'
Create an alias record (no TTL, Route53-specific)
创建别名记录(无TTL,Route53专属)
aws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "example.com", "Type": "A", "AliasTarget": { "HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": "d1234567890.cloudfront.net", "EvaluateTargetHealth": false } } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "example.com", "Type": "A", "AliasTarget": { "HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": "d1234567890.cloudfront.net", "EvaluateTargetHealth": false } } }] }'
aws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "example.com", "Type": "A", "AliasTarget": { "HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": "d1234567890.cloudfront.net", "EvaluateTargetHealth": false } } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "CREATE", "ResourceRecordSet": { "Name": "example.com", "Type": "A", "AliasTarget": { "HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": "d1234567890.cloudfront.net", "EvaluateTargetHealth": false } } }] }'
List records in a zone
列出区域内的记录
aws route53 list-resource-record-sets --hosted-zone-id Z1234567890ABC
aws route53 list-resource-record-sets --hosted-zone-id Z1234567890ABC
Delete a record (Action: DELETE with exact match)
删除记录(Action设为DELETE并完全匹配记录)
aws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "DELETE", "ResourceRecordSet": { "Name": "old.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "1.2.3.4"}] } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "DELETE", "ResourceRecordSet": { "Name": "old.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "1.2.3.4"}] } }] }'
undefinedaws route53 change-resource-record-sets
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "DELETE", "ResourceRecordSet": { "Name": "old.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "1.2.3.4"}] } }] }'
--hosted-zone-id Z1234567890ABC
--change-batch '{ "Changes": [{ "Action": "DELETE", "ResourceRecordSet": { "Name": "old.example.com", "Type": "A", "TTL": 300, "ResourceRecords": [{"Value": "1.2.3.4"}] } }] }'
undefinedRoute 53 Health Checks
Route 53健康检查
bash
undefinedbash
undefinedCreate a health check
创建健康检查
aws route53 create-health-check --caller-reference "$(date +%s)"
--health-check-config '{ "IPAddress": "93.184.216.34", "Port": 443, "Type": "HTTPS", "ResourcePath": "/health", "RequestInterval": 30, "FailureThreshold": 3 }'
--health-check-config '{ "IPAddress": "93.184.216.34", "Port": 443, "Type": "HTTPS", "ResourcePath": "/health", "RequestInterval": 30, "FailureThreshold": 3 }'
undefinedaws route53 create-health-check --caller-reference "$(date +%s)"
--health-check-config '{ "IPAddress": "93.184.216.34", "Port": 443, "Type": "HTTPS", "ResourcePath": "/health", "RequestInterval": 30, "FailureThreshold": 3 }'
--health-check-config '{ "IPAddress": "93.184.216.34", "Port": 443, "Type": "HTTPS", "ResourcePath": "/health", "RequestInterval": 30, "FailureThreshold": 3 }'
undefinedCloudflare DNS
Cloudflare DNS
Manage Records via API
通过API管理记录
bash
undefinedbash
undefinedGet zone ID
获取区域ID
ZONE_ID=$(curl -s "https://api.cloudflare.com/client/v4/zones?name=example.com"
-H "Authorization: Bearer $CF_API_TOKEN" | jq -r '.result[0].id')
-H "Authorization: Bearer $CF_API_TOKEN" | jq -r '.result[0].id')
ZONE_ID=$(curl -s "https://api.cloudflare.com/client/v4/zones?name=example.com"
-H "Authorization: Bearer $CF_API_TOKEN" | jq -r '.result[0].id')
-H "Authorization: Bearer $CF_API_TOKEN" | jq -r '.result[0].id')
Create an A record (proxied through Cloudflare)
创建A记录(通过Cloudflare代理)
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
-H "Authorization: Bearer $CF_API_TOKEN"
-H "Content-Type: application/json"
-d '{"type":"A","name":"app","content":"93.184.216.34","proxied":true,"ttl":1}'
-H "Authorization: Bearer $CF_API_TOKEN"
-H "Content-Type: application/json"
-d '{"type":"A","name":"app","content":"93.184.216.34","proxied":true,"ttl":1}'
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
-H "Authorization: Bearer $CF_API_TOKEN"
-H "Content-Type: application/json"
-d '{"type":"A","name":"app","content":"93.184.216.34","proxied":true,"ttl":1}'
-H "Authorization: Bearer $CF_API_TOKEN"
-H "Content-Type: application/json"
-d '{"type":"A","name":"app","content":"93.184.216.34","proxied":true,"ttl":1}'
Create a CNAME record (DNS only, not proxied)
创建CNAME记录(仅DNS,不代理)
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
-H "Authorization: Bearer $CF_API_TOKEN"
-d '{"type":"CNAME","name":"docs","content":"docs.readthedocs.io","proxied":false,"ttl":3600}'
-H "Authorization: Bearer $CF_API_TOKEN"
-d '{"type":"CNAME","name":"docs","content":"docs.readthedocs.io","proxied":false,"ttl":3600}'
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
-H "Authorization: Bearer $CF_API_TOKEN"
-d '{"type":"CNAME","name":"docs","content":"docs.readthedocs.io","proxied":false,"ttl":3600}'
-H "Authorization: Bearer $CF_API_TOKEN"
-d '{"type":"CNAME","name":"docs","content":"docs.readthedocs.io","proxied":false,"ttl":3600}'
List all records
列出所有记录
curl -s "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
-H "Authorization: Bearer $CF_API_TOKEN" | jq '.result[] | {name, type, content, proxied}'
-H "Authorization: Bearer $CF_API_TOKEN" | jq '.result[] | {name, type, content, proxied}'
curl -s "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
-H "Authorization: Bearer $CF_API_TOKEN" | jq '.result[] | {name, type, content, proxied}'
-H "Authorization: Bearer $CF_API_TOKEN" | jq '.result[] | {name, type, content, proxied}'
Delete a record
删除记录
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID"
-H "Authorization: Bearer $CF_API_TOKEN"
-H "Authorization: Bearer $CF_API_TOKEN"
undefinedcurl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID"
-H "Authorization: Bearer $CF_API_TOKEN"
-H "Authorization: Bearer $CF_API_TOKEN"
undefinedTerraform DNS Management
Terraform DNS管理
Route 53 with Terraform
使用Terraform管理Route 53
hcl
undefinedhcl
undefineddns.tf
dns.tf
resource "aws_route53_zone" "main" {
name = "example.com"
}
resource "aws_route53_record" "app" {
zone_id = aws_route53_zone.main.zone_id
name = "app.example.com"
type = "A"
ttl = 300
records = ["93.184.216.34"]
}
resource "aws_route53_record" "www" {
zone_id = aws_route53_zone.main.zone_id
name = "www.example.com"
type = "CNAME"
ttl = 300
records = ["example.com"]
}
resource "aws_route53_zone" "main" {
name = "example.com"
}
resource "aws_route53_record" "app" {
zone_id = aws_route53_zone.main.zone_id
name = "app.example.com"
type = "A"
ttl = 300
records = ["93.184.216.34"]
}
resource "aws_route53_record" "www" {
zone_id = aws_route53_zone.main.zone_id
name = "www.example.com"
type = "CNAME"
ttl = 300
records = ["example.com"]
}
Alias record for CloudFront
CloudFront的别名记录
resource "aws_route53_record" "cdn" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com"
type = "A"
alias {
name = aws_cloudfront_distribution.main.domain_name
zone_id = aws_cloudfront_distribution.main.hosted_zone_id
evaluate_target_health = false
}
}
resource "aws_route53_record" "cdn" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com"
type = "A"
alias {
name = aws_cloudfront_distribution.main.domain_name
zone_id = aws_cloudfront_distribution.main.hosted_zone_id
evaluate_target_health = false
}
}
Email records
邮箱记录
resource "aws_route53_record" "mx" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com"
type = "MX"
ttl = 3600
records = [
"1 aspmx.l.google.com",
"5 alt1.aspmx.l.google.com",
"5 alt2.aspmx.l.google.com",
]
}
resource "aws_route53_record" "spf" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com"
type = "TXT"
ttl = 3600
records = ["v=spf1 include:_spf.google.com ~all"]
}
resource "aws_route53_record" "dmarc" {
zone_id = aws_route53_zone.main.zone_id
name = "_dmarc.example.com"
type = "TXT"
ttl = 3600
records = ["v=DMARC1; p=quarantine; rua=mailto:dmarc@example.com; pct=100"]
}
undefinedresource "aws_route53_record" "mx" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com"
type = "MX"
ttl = 3600
records = [
"1 aspmx.l.google.com",
"5 alt1.aspmx.l.google.com",
"5 alt2.aspmx.l.google.com",
]
}
resource "aws_route53_record" "spf" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com"
type = "TXT"
ttl = 3600
records = ["v=spf1 include:_spf.google.com ~all"]
}
resource "aws_route53_record" "dmarc" {
zone_id = aws_route53_zone.main.zone_id
name = "_dmarc.example.com"
type = "TXT"
ttl = 3600
records = ["v=DMARC1; p=quarantine; rua=mailto:dmarc@example.com; pct=100"]
}
undefinedCloudflare with Terraform
使用Terraform管理Cloudflare
hcl
resource "cloudflare_record" "app" {
zone_id = var.cloudflare_zone_id
name = "app"
content = "93.184.216.34"
type = "A"
proxied = true
}
resource "cloudflare_record" "mail" {
zone_id = var.cloudflare_zone_id
name = "@"
content = "aspmx.l.google.com"
type = "MX"
priority = 1
}hcl
resource "cloudflare_record" "app" {
zone_id = var.cloudflare_zone_id
name = "app"
content = "93.184.216.34"
type = "A"
proxied = true
}
resource "cloudflare_record" "mail" {
zone_id = var.cloudflare_zone_id
name = "@"
content = "aspmx.l.google.com"
type = "MX"
priority = 1
}DNS Troubleshooting Commands
DNS故障排查命令
dig
dig
bash
undefinedbash
undefinedQuery A record
查询A记录
dig app.example.com A +short
dig app.example.com A +short
Query from a specific DNS server
指定DNS服务器查询
dig @8.8.8.8 app.example.com A
dig @8.8.8.8 app.example.com A
Show full answer with TTL
显示包含TTL的完整响应
dig app.example.com A +noall +answer
dig app.example.com A +noall +answer
Query MX records
查询MX记录
dig example.com MX +short
dig example.com MX +short
Trace the full resolution path
追踪完整解析路径
dig app.example.com +trace
dig app.example.com +trace
Check DNSSEC validation
检查DNSSEC验证
dig example.com +dnssec +short
dig example.com +dnssec +short
Query TXT records (SPF, DKIM)
查询TXT记录(SPF、DKIM)
dig example.com TXT +short
dig default._domainkey.example.com TXT +short
undefineddig example.com TXT +short
dig default._domainkey.example.com TXT +short
undefinednslookup
nslookup
bash
undefinedbash
undefinedBasic lookup
基础查询
nslookup app.example.com
nslookup app.example.com
Specify DNS server
指定DNS服务器
nslookup app.example.com 8.8.8.8
nslookup app.example.com 8.8.8.8
Query specific record type
查询特定记录类型
nslookup -type=MX example.com
nslookup -type=TXT example.com
undefinednslookup -type=MX example.com
nslookup -type=TXT example.com
undefinedCheck DNS Propagation
检查DNS传播情况
bash
undefinedbash
undefinedQuery multiple public resolvers
查询多个公共解析器
for dns in 8.8.8.8 1.1.1.1 9.9.9.9 208.67.222.222; do
echo "=== $dns ==="
dig @$dns app.example.com A +short
done
undefinedfor dns in 8.8.8.8 1.1.1.1 9.9.9.9 208.67.222.222; do
echo "=== $dns ==="
dig @$dns app.example.com A +short
done
undefinedEmail Security Records
邮箱安全记录
bash
undefinedbash
undefinedSPF — authorize sending servers
SPF — 授权发送服务器
TXT record on example.com
example.com的TXT记录
"v=spf1 include:_spf.google.com include:sendgrid.net -all"
"v=spf1 include:_spf.google.com include:sendgrid.net -all"
DKIM — email signing verification
DKIM — 邮件签名验证
TXT record on google._domainkey.example.com
google._domainkey.example.com的TXT记录
(value provided by your email provider)
(值由邮箱服务商提供)
DMARC — policy for failed SPF/DKIM
DMARC — SPF/DKIM验证失败后的策略
TXT record on _dmarc.example.com
_dmarc.example.com的TXT记录
"v=DMARC1; p=reject; rua=mailto:dmarc-reports@example.com; pct=100"
undefined"v=DMARC1; p=reject; rua=mailto:dmarc-reports@example.com; pct=100"
undefinedTTL Strategies
TTL策略
| Scenario | Recommended TTL | Rationale |
|---|---|---|
| Stable production records | 3600-86400 (1h-24h) | Reduce DNS queries, faster resolution |
| Pre-migration warmup | 60-300 (1-5 min) | Lower TTL days before migration |
| During migration/failover | 60 | Fast propagation of changes |
| Post-migration cooldown | Gradually increase to 3600+ | Return to normal after confirming stability |
| Load-balanced records | 60-300 | Allow health-check-driven failover |
| 场景 | 推荐TTL | 理由 |
|---|---|---|
| 稳定生产环境记录 | 3600-86400(1小时-24小时) | 减少DNS查询,加快解析速度 |
| 迁移预热阶段 | 60-300(1-5分钟) | 迁移前几天降低TTL |
| 迁移/故障切换期间 | 60 | 快速传播变更 |
| 迁移后冷却阶段 | 逐步提升至3600+ | 确认稳定后恢复正常设置 |
| 负载均衡记录 | 60-300 | 支持基于健康检查的故障切换 |
Troubleshooting
故障排查
| Symptom | Cause | Fix |
|---|---|---|
| DNS changes not visible | TTL not expired on recursive resolvers | Wait for old TTL to expire; lower TTL before next change |
| DNSSEC validation failure or broken delegation | Check NS records at registrar; verify DNSSEC signatures |
| Wrong hosted zone or missing record | Verify record exists with |
| CNAME at zone apex returns error | CNAME not allowed at zone apex per RFC | Use ALIAS (Route53) or proxied A record (Cloudflare) |
| Email going to spam | Missing or broken SPF/DKIM/DMARC | Verify TXT records with |
| Slow resolution | Recursive resolver far from authoritative NS | Use Anycast DNS providers (Cloudflare, Route53) |
| Inconsistent results across resolvers | Partial propagation or cache poisoning | Query authoritative NS directly; check for conflicting records |
| 症状 | 原因 | 解决方法 |
|---|---|---|
| DNS变更未生效 | 递归解析器的TTL未过期 | 等待旧TTL过期;下次变更前降低TTL |
返回 | DNSSEC验证失败或委托关系断裂 | 检查注册商处的NS记录;验证DNSSEC签名 |
有效记录返回 | 托管区域错误或记录缺失 | 使用 |
| 区域顶点的CNAME返回错误 | RFC规定区域顶点不允许使用CNAME | 使用Route53的ALIAS记录或Cloudflare的代理A记录 |
| 邮件进入垃圾邮箱 | SPF/DKIM/DMARC缺失或配置错误 | 使用 |
| 解析速度慢 | 递归解析器距离权威NS过远 | 使用Anycast DNS服务商(Cloudflare、Route53) |
| 不同解析器结果不一致 | 部分传播或缓存中毒 | 直接查询权威NS;检查是否存在冲突记录 |
Related Skills
相关技能
- cdn-setup - CDN CNAME and alias record configuration
- load-balancing - DNS-based load balancing and health checks
- cloudflare-zero-trust - Tunnel DNS routing
- reverse-proxy - Connecting domains to backend services
- cdn-setup - CDN CNAME和别名记录配置
- load-balancing - 基于DNS的负载均衡与健康检查
- cloudflare-zero-trust - Tunnel DNS路由
- reverse-proxy - 将域名连接到后端服务