<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>DNS on HiDa</title><link>https://www.0niu.cn/tags/dns/</link><description>Recent content in DNS on HiDa</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Thu, 08 May 2025 10:16:00 +0800</lastBuildDate><atom:link href="https://www.0niu.cn/tags/dns/index.xml" rel="self" type="application/rss+xml"/><item><title>使用 resolvectl 临时指定 DNS 服务器</title><link>https://www.0niu.cn/posts/resolvectl-temporary-dns/</link><pubDate>Thu, 08 May 2025 10:16:00 +0800</pubDate><guid>https://www.0niu.cn/posts/resolvectl-temporary-dns/</guid><description>&lt;p>在日常运维中，我们经常需要临时切换 DNS 服务器来排查域名解析问题。传统做法是修改 &lt;code>/etc/resolv.conf&lt;/code>，但在使用 systemd-resolved 的现代 Linux 发行版中，&lt;code>resolvectl&lt;/code> 提供了更优雅的解决方案。&lt;/p></description><content>&lt;p>在日常运维中，我们经常需要临时切换 DNS 服务器来排查域名解析问题。传统做法是修改 &lt;code>/etc/resolv.conf&lt;/code>，但在使用 systemd-resolved 的现代 Linux 发行版中，&lt;code>resolvectl&lt;/code> 提供了更优雅的解决方案。&lt;/p>
&lt;h2 id="为什么用-resolvectl">为什么用 resolvectl&lt;/h2>
&lt;p>传统的修改 &lt;code>/etc/resolv.conf&lt;/code> 方式有几个痛点：&lt;/p>
&lt;ul>
&lt;li>&lt;strong>systemd-resolved 管理下会被覆盖&lt;/strong>：直接编辑 &lt;code>/etc/resolv.conf&lt;/code> 可能无效，因为该文件是一个指向 &lt;code>/run/systemd/resolve/stub-resolv.conf&lt;/code> 的符号链接&lt;/li>
&lt;li>&lt;strong>需要 root 权限&lt;/strong>：编辑系统文件必须 sudo&lt;/li>
&lt;li>&lt;strong>容易忘记还原&lt;/strong>：排查完问题后忘记改回来，可能影响业务&lt;/li>
&lt;/ul>
&lt;p>而 &lt;code>resolvectl&lt;/code> 的优势：&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>特性&lt;/th>
&lt;th>传统方式&lt;/th>
&lt;th>resolvectl&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>修改配置文件&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>❌&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>需要 root&lt;/td>
&lt;td>✅&lt;/td>
&lt;td>❌（per-link）&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>重启后恢复&lt;/td>
&lt;td>需手动还原&lt;/td>
&lt;td>✅ 自动恢复&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>影响范围&lt;/td>
&lt;td>全局&lt;/td>
&lt;td>可按网卡控制&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="查看当前-dns-状态">查看当前 DNS 状态&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 查看全局 DNS 配置&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl status
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 查看指定网卡&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl status eth0
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 查看当前生效的 DNS 服务器&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl dns
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>输出示例：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-fallback" data-lang="fallback">&lt;span style="display:flex;">&lt;span>Global
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Protocols: +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Resolving DNS Servers: 192.168.1.1
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Fallback DNS Servers: 8.8.8.8
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Link 2 (eth0)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Current Scopes: DNS
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> Protocols: +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Resolving DNS Servers: 192.168.1.1
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> DNS Domain: lan
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="临时指定-dnsper-link">临时指定 DNS（per-link）&lt;/h2>
&lt;h3 id="指定单网卡-dns">指定单网卡 DNS&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 将 eth0 的 DNS 指定为 Google DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth0 8.8.8.8
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 指定多个 DNS（主备）&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth0 8.8.8.8 1.1.1.1
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 指定国内 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth0 223.5.5.5 119.29.29.29
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="验证生效">验证生效&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 查看当前 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl dns eth0
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 测试解析&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl query example.com
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 使用指定 DNS 解析&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl query example.com --interface&lt;span style="color:#f92672">=&lt;/span>eth0
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="恢复默认-dns">恢复默认 DNS&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 恢复为空（回退到全局配置）&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth0
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 恢复为 DHCP 下发的 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl revert eth0
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="全局临时指定-dns">全局临时指定 DNS&lt;/h2>
&lt;p>如果需要全局修改（影响所有网卡）：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 设置全局 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns 8.8.8.8 1.1.1.1
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 恢复全局 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>⚠️ 全局修改同样会在重启后恢复，但会影响所有网卡的 DNS 解析。&lt;/p>
&lt;/blockquote>
&lt;h2 id="常用国内公共-dns">常用国内公共 DNS&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>DNS 服务商&lt;/th>
&lt;th>地址&lt;/th>
&lt;th>说明&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>阿里&lt;/td>
&lt;td>223.5.5.5 / 223.6.6.6&lt;/td>
&lt;td>稳定快速&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>腾讯&lt;/td>
&lt;td>119.29.29.29&lt;/td>
&lt;td>DNSPod&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>百度&lt;/td>
&lt;td>180.76.76.76&lt;/td>
&lt;td>-&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>114 DNS&lt;/td>
&lt;td>114.114.114.114&lt;/td>
&lt;td>老牌公共 DNS&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Google&lt;/td>
&lt;td>8.8.8.8 / 8.8.4.4&lt;/td>
&lt;td>海外推荐&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Cloudflare&lt;/td>
&lt;td>1.1.1.1 / 1.0.0.1&lt;/td>
&lt;td>海外&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="实战场景">实战场景&lt;/h2>
&lt;h3 id="场景一排查域名解析故障">场景一：排查域名解析故障&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 1. 查看当前 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl dns
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 2. 临时切换到公共 DNS 验证&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth0 223.5.5.5
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 3. 测试解析&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl query api.example.com
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 4. 如果公共 DNS 正常，说明内网 DNS 有问题&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 5. 恢复原配置&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl revert eth0
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="场景二测试-dohdot">场景二：测试 DoH/DoT&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 查看是否支持 DNS over TLS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl status | grep -i tls
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 开启 DNS over TLS（需要 DNS 服务器支持）&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns-over-tls eth0 yes
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth0 tls://dns.google
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="场景三多网卡环境指定-dns">场景三：多网卡环境指定 DNS&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># eth0 走内网 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth0 192.168.1.1
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># eth1 走公共 DNS&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo resolvectl dns eth1 223.5.5.5
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 查询时指定网卡&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl query internal.example.com --interface&lt;span style="color:#f92672">=&lt;/span>eth0
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>resolvectl query www.0niu.cn --interface&lt;span style="color:#f92672">=&lt;/span>eth1
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="注意事项">注意事项&lt;/h2>
&lt;ol>
&lt;li>&lt;strong>临时性&lt;/strong>：&lt;code>resolvectl&lt;/code> 的修改在系统重启后会自动恢复为配置文件中的默认值&lt;/li>
&lt;li>&lt;strong>与 NetworkManager 冲突&lt;/strong>：如果使用 NetworkManager 管理 DNS，可能会被 NM 覆盖，可以设置 &lt;code>NM_CONTROLLED=no&lt;/code> 或在 NM 配置中指定 DNS&lt;/li>
&lt;li>&lt;strong>systemd-resolved 必须运行&lt;/strong>：确认服务状态 &lt;code>systemctl status systemd-resolved&lt;/code>&lt;/li>
&lt;li>&lt;strong>域名搜索后缀&lt;/strong>：可以使用 &lt;code>resolvectl domain eth0 example.com&lt;/code> 临时添加搜索域&lt;/li>
&lt;/ol>
&lt;h2 id="总结">总结&lt;/h2>
&lt;p>&lt;code>resolvectl&lt;/code> 是 systemd-resolved 提供的 DNS 管理工具，用于临时切换 DNS 非常方便：&lt;/p>
&lt;ul>
&lt;li>✅ 无需修改配置文件&lt;/li>
&lt;li>✅ 重启自动恢复&lt;/li>
&lt;li>✅ 可按网卡粒度控制&lt;/li>
&lt;li>✅ 支持 DNS over TLS&lt;/li>
&lt;/ul>
&lt;p>下次排查 DNS 问题时，不妨试试 &lt;code>resolvectl&lt;/code>。&lt;/p></content></item></channel></rss>