$npx -y skills add chaterm/terminal-skills --skill dnsDNS 配置与排查
| 1 | # DNS 配置与排查 |
| 2 | |
| 3 | ## 概述 |
| 4 | DNS 配置、解析排查、BIND/CoreDNS 等技能。 |
| 5 | |
| 6 | ## DNS 查询工具 |
| 7 | |
| 8 | ### dig |
| 9 | ```bash |
| 10 | # 基础查询 |
| 11 | dig example.com |
| 12 | dig example.com A |
| 13 | dig example.com AAAA |
| 14 | dig example.com MX |
| 15 | dig example.com NS |
| 16 | dig example.com TXT |
| 17 | dig example.com ANY |
| 18 | |
| 19 | # 简短输出 |
| 20 | dig +short example.com |
| 21 | |
| 22 | # 指定 DNS 服务器 |
| 23 | dig @8.8.8.8 example.com |
| 24 | dig @1.1.1.1 example.com |
| 25 | |
| 26 | # 追踪解析过程 |
| 27 | dig +trace example.com |
| 28 | |
| 29 | # 反向解析 |
| 30 | dig -x 8.8.8.8 |
| 31 | |
| 32 | # 查询特定记录 |
| 33 | dig example.com SOA |
| 34 | dig example.com CNAME |
| 35 | |
| 36 | # 禁用递归 |
| 37 | dig +norecurse example.com |
| 38 | ``` |
| 39 | |
| 40 | ### nslookup |
| 41 | ```bash |
| 42 | # 基础查询 |
| 43 | nslookup example.com |
| 44 | nslookup example.com 8.8.8.8 |
| 45 | |
| 46 | # 查询特定类型 |
| 47 | nslookup -type=mx example.com |
| 48 | nslookup -type=ns example.com |
| 49 | nslookup -type=txt example.com |
| 50 | |
| 51 | # 反向解析 |
| 52 | nslookup 8.8.8.8 |
| 53 | ``` |
| 54 | |
| 55 | ### host |
| 56 | ```bash |
| 57 | # 基础查询 |
| 58 | host example.com |
| 59 | host -t mx example.com |
| 60 | host -t ns example.com |
| 61 | |
| 62 | # 反向解析 |
| 63 | host 8.8.8.8 |
| 64 | |
| 65 | # 详细输出 |
| 66 | host -v example.com |
| 67 | ``` |
| 68 | |
| 69 | ## 本地 DNS 配置 |
| 70 | |
| 71 | ### /etc/resolv.conf |
| 72 | ```bash |
| 73 | # 查看配置 |
| 74 | cat /etc/resolv.conf |
| 75 | |
| 76 | # 配置示例 |
| 77 | nameserver 8.8.8.8 |
| 78 | nameserver 8.8.4.4 |
| 79 | search example.com |
| 80 | options timeout:2 attempts:3 |
| 81 | |
| 82 | # 临时修改(可能被覆盖) |
| 83 | echo "nameserver 8.8.8.8" > /etc/resolv.conf |
| 84 | ``` |
| 85 | |
| 86 | ### /etc/hosts |
| 87 | ```bash |
| 88 | # 查看 |
| 89 | cat /etc/hosts |
| 90 | |
| 91 | # 添加记录 |
| 92 | echo "192.168.1.100 myserver.local" >> /etc/hosts |
| 93 | |
| 94 | # 格式 |
| 95 | 127.0.0.1 localhost |
| 96 | 192.168.1.100 myserver myserver.local |
| 97 | ``` |
| 98 | |
| 99 | ### systemd-resolved |
| 100 | ```bash |
| 101 | # 查看状态 |
| 102 | systemd-resolve --status |
| 103 | resolvectl status |
| 104 | |
| 105 | # 查询 |
| 106 | resolvectl query example.com |
| 107 | |
| 108 | # 刷新缓存 |
| 109 | systemd-resolve --flush-caches |
| 110 | resolvectl flush-caches |
| 111 | |
| 112 | # 配置文件 |
| 113 | /etc/systemd/resolved.conf |
| 114 | ``` |
| 115 | |
| 116 | ## BIND DNS 服务器 |
| 117 | |
| 118 | ### 安装与管理 |
| 119 | ```bash |
| 120 | # 安装 |
| 121 | apt install bind9 bind9utils # Debian/Ubuntu |
| 122 | yum install bind bind-utils # CentOS/RHEL |
| 123 | |
| 124 | # 服务管理 |
| 125 | systemctl start named |
| 126 | systemctl enable named |
| 127 | systemctl status named |
| 128 | |
| 129 | # 检查配置 |
| 130 | named-checkconf |
| 131 | named-checkzone example.com /etc/bind/zones/db.example.com |
| 132 | ``` |
| 133 | |
| 134 | ### 主配置 |
| 135 | ```bash |
| 136 | # /etc/bind/named.conf.options |
| 137 | options { |
| 138 | directory "/var/cache/bind"; |
| 139 | |
| 140 | forwarders { |
| 141 | 8.8.8.8; |
| 142 | 8.8.4.4; |
| 143 | }; |
| 144 | |
| 145 | dnssec-validation auto; |
| 146 | |
| 147 | listen-on { any; }; |
| 148 | listen-on-v6 { any; }; |
| 149 | |
| 150 | allow-query { any; }; |
| 151 | allow-recursion { 192.168.0.0/16; 10.0.0.0/8; }; |
| 152 | |
| 153 | recursion yes; |
| 154 | }; |
| 155 | ``` |
| 156 | |
| 157 | ### 区域配置 |
| 158 | ```bash |
| 159 | # /etc/bind/named.conf.local |
| 160 | zone "example.com" { |
| 161 | type master; |
| 162 | file "/etc/bind/zones/db.example.com"; |
| 163 | allow-transfer { 192.168.1.2; }; |
| 164 | }; |
| 165 | |
| 166 | zone "1.168.192.in-addr.arpa" { |
| 167 | type master; |
| 168 | file "/etc/bind/zones/db.192.168.1"; |
| 169 | }; |
| 170 | ``` |
| 171 | |
| 172 | ### 区域文件 |
| 173 | ```bash |
| 174 | # /etc/bind/zones/db.example.com |
| 175 | $TTL 604800 |
| 176 | @ IN SOA ns1.example.com. admin.example.com. ( |
| 177 | 2024011501 ; Serial |
| 178 | 604800 ; Refresh |
| 179 | 86400 ; Retry |
| 180 | 2419200 ; Expire |
| 181 | 604800 ) ; Negative Cache TTL |
| 182 | |
| 183 | ; Name servers |
| 184 | @ IN NS ns1.example.com. |
| 185 | @ IN NS ns2.example.com. |
| 186 | |
| 187 | ; A records |
| 188 | @ IN A 192.168.1.10 |
| 189 | ns1 IN A 192.168.1.1 |
| 190 | ns2 IN A 192.168.1.2 |
| 191 | www IN A 192.168.1.10 |
| 192 | mail IN A 192.168.1.20 |
| 193 | |
| 194 | ; CNAME records |
| 195 | ftp IN CNAME www.example.com. |
| 196 | |
| 197 | ; MX records |
| 198 | @ IN MX 10 mail.example.com. |
| 199 | ``` |
| 200 | |
| 201 | ## CoreDNS |
| 202 | |
| 203 | ### 配置文件 |
| 204 | ```bash |
| 205 | # Corefile |
| 206 | .:53 { |
| 207 | forward . 8.8.8.8 8.8.4.4 |
| 208 | cache 30 |
| 209 | log |
| 210 | errors |
| 211 | } |
| 212 | |
| 213 | example.com:53 { |
| 214 | file /etc/coredns/db.example.com |
| 215 | log |
| 216 | errors |
| 217 | } |
| 218 | ``` |
| 219 | |
| 220 | ### Kubernetes CoreDNS |
| 221 | ```yaml |
| 222 | # ConfigMap |
| 223 | apiVersion: v1 |
| 224 | kind: ConfigMap |
| 225 | metadata: |
| 226 | name: coredns |
| 227 | namespace: kube-system |
| 228 | data: |
| 229 | Corefile: | |
| 230 | .:53 { |
| 231 | errors |
| 232 | health { |
| 233 | lameduck 5s |
| 234 | } |
| 235 | ready |
| 236 | kubernetes cluster.local in-addr.arpa ip6.arpa { |
| 237 | pods insecure |
| 238 | fallthrough in-addr.arpa ip6.arpa |
| 239 | ttl 30 |
| 240 | } |
| 241 | prometheus :9153 |
| 242 | forward . /etc/resolv.conf { |
| 243 | max_concurrent 1000 |
| 244 | } |
| 245 | cache 30 |
| 246 | loop |
| 247 | reload |
| 248 | loadbalance |
| 249 | } |
| 250 | ``` |
| 251 | |
| 252 | ## 常见场景 |
| 253 | |
| 254 | ### 场景 1:DNS 解析排查 |
| 255 | ```bash |
| 256 | # 1. 检查本地配置 |
| 257 | cat /etc/resolv.conf |
| 258 | |
| 259 | # 2. 测试 DNS 服务器连通性 |
| 260 | ping 8.8.8.8 |
| 261 | |
| 262 | # 3. 查询解析 |
| 263 | dig example.com |
| 264 | dig @8.8.8.8 example.com |
| 265 | |
| 266 | # 4. 追踪解析路径 |
| 267 | dig +trace example.com |
| 268 | |
| 269 | # 5. 检查 DNS 缓存 |
| 270 | systemd-resolve --statistics |
| 271 | ``` |
| 272 | |
| 273 | ### 场景 2:清除 DNS 缓存 |
| 274 | ```bash |
| 275 | # systemd-resolved |
| 276 | systemd-resolve --flush-caches |
| 277 | |
| 278 | # nscd |
| 279 | systemctl restart nscd |
| 280 | |
| 281 | # dnsmasq |
| 282 | systemctl restart dnsmasq |
| 283 | |
| 284 | # BIND |
| 285 | rndc flush |
| 286 | |
| 287 | # macOS |
| 288 | sudo dscacheutil -flushcache |
| 289 | sudo killall -HUP mDNSResponder |
| 290 | ``` |
| 291 | |
| 292 | ### 场景 3:测试 DNS 性能 |
| 293 | ```bash |
| 294 | # 使用 dig 测试响应时间 |
| 295 | dig example.com | grep "Query time" |
| 296 | |
| 297 | # 批量测试 |
| 298 | for i in {1..10}; do |
| 299 | dig +noall +stats example.com | grep "Query time" |
| 300 | done |
| 301 | |
| 302 | # 使用 dnsperf |
| 303 | dnsperf -s 8.8.8.8 -d queries.txt |
| 304 | ``` |
| 305 | |
| 306 | ### 场景 4:配置内部 DNS |
| 307 | ```bash |
| 308 | # 添加内部域名解析 |
| 309 | # /etc/hosts |
| 310 | 192.168.1.100 app.internal |
| 311 | 192.168.1.101 db.internal |
| 312 | |
| 313 | # 或配置 dnsmasq |
| 314 | # /etc/dnsmasq.conf |
| 315 | address=/internal/192.168.1.100 |
| 316 | server=8.8.8.8 |
| 317 | ``` |
| 318 | |
| 319 | ## 故障排查 |
| 320 | |
| 321 | | 问题 | 排查方法 | |
| 322 | |------|-------- |