<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linux on HiDa</title><link>https://www.0niu.cn/tags/linux/</link><description>Recent content in Linux on HiDa</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Mon, 18 May 2026 19:22:32 +0800</lastBuildDate><atom:link href="https://www.0niu.cn/tags/linux/index.xml" rel="self" type="application/rss+xml"/><item><title>firewalld 配置 internal 到 public 区域的流量转发</title><link>https://www.0niu.cn/posts/firewalld-forward-internal-to-public/</link><pubDate>Mon, 18 May 2026 19:22:32 +0800</pubDate><guid>https://www.0niu.cn/posts/firewalld-forward-internal-to-public/</guid><description>&lt;p>firewalld 支持通过 Policy 对象实现跨区域流量转发。本文介绍如何配置策略，允许 internal 区域的流量转发到 public 区域，使内网主机可以通过防火墙访问外部网络。&lt;/p>
&lt;h2 id="前提条件">前提条件&lt;/h2>
&lt;ul>
&lt;li>firewalld 已安装并运行（&lt;code>systemctl status firewalld&lt;/code>）&lt;/li>
&lt;li>有 root 或 sudo 权限&lt;/li>
&lt;li>了解 firewalld 的 Zone 和 Policy 概念&lt;/li>
&lt;/ul></description><content>&lt;p>firewalld 支持通过 Policy 对象实现跨区域流量转发。本文介绍如何配置策略，允许 internal 区域的流量转发到 public 区域，使内网主机可以通过防火墙访问外部网络。&lt;/p>
&lt;h2 id="前提条件">前提条件&lt;/h2>
&lt;ul>
&lt;li>firewalld 已安装并运行（&lt;code>systemctl status firewalld&lt;/code>）&lt;/li>
&lt;li>有 root 或 sudo 权限&lt;/li>
&lt;li>了解 firewalld 的 Zone 和 Policy 概念&lt;/li>
&lt;/ul>
&lt;h2 id="创建转发策略">创建转发策略&lt;/h2>
&lt;p>firewalld 的 Zone 控制接口上的流量方向，而 Policy 用于定义跨区域的流量转发规则。我们需要创建一个 Policy，将 internal 作为入口区域，public 作为出口区域。&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"># 创建新策略&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo firewall-cmd --permanent --new-policy allow-internal-to-public
&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"># 设置入口区域（源）为 internal，出口区域（目标）为 public&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo firewall-cmd --permanent --policy allow-internal-to-public --add-ingress-zone internal
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo firewall-cmd --permanent --policy allow-internal-to-public --add-egress-zone public
&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"># 设置动作为 ACCEPT，并给予高优先级（确保不被其他策略覆盖）&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo firewall-cmd --permanent --policy allow-internal-to-public --set-target ACCEPT
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo firewall-cmd --permanent --policy allow-internal-to-public --set-priority -100
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>关键参数说明：&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>参数&lt;/th>
&lt;th>说明&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>--add-ingress-zone&lt;/code>&lt;/td>
&lt;td>流量进入的区域（源区域）&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>--add-egress-zone&lt;/code>&lt;/td>
&lt;td>流量出去的区域（目标区域）&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>--set-target ACCEPT&lt;/code>&lt;/td>
&lt;td>匹配该策略的流量默认放行&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>--set-priority -100&lt;/code>&lt;/td>
&lt;td>优先级数值越小越优先，负数确保优先于默认策略&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="开启伪装masquerade">开启伪装（MASQUERADE）&lt;/h2>
&lt;p>为了让内网 IP 的流量通过 public 区域的出口 IP 访问外部网络，需要为 public 区域开启源地址伪装（SNAT）：&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>sudo firewall-cmd --permanent --zone&lt;span style="color:#f92672">=&lt;/span>public --add-masquerade
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="启用-ip-转发">启用 IP 转发&lt;/h2>
&lt;p>Linux 内核默认关闭 IP 转发功能，需要手动开启：&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"># 临时启用（重启后失效）&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo sysctl -w net.ipv4.ip_forward&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span>
&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>echo net.ipv4.ip_forward&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span> | sudo tee -a /etc/sysctl.d/99-ipforward.conf
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="重载防火墙">重载防火墙&lt;/h2>
&lt;p>以上所有 &lt;code>--permanent&lt;/code> 操作需要重载后才会生效：&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>sudo firewall-cmd --reload
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="验证配置">验证配置&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"># 查看策略列表&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo firewall-cmd --get-policies
&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>sudo firewall-cmd --info-policy allow-internal-to-public
&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"># 查看 public 区域是否开启伪装&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo firewall-cmd --zone&lt;span style="color:#f92672">=&lt;/span>public --query-masquerade
&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"># 查看 IP 转发状态&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sysctl net.ipv4.ip_forward
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="常见问题">常见问题&lt;/h2>
&lt;p>&lt;strong>内网主机无法访问外部？&lt;/strong> 检查以下几点：&lt;/p>
&lt;ol>
&lt;li>确认 &lt;code>net.ipv4.ip_forward&lt;/code> 已设为 &lt;code>1&lt;/code>&lt;/li>
&lt;li>确认 public 区域的伪装已开启&lt;/li>
&lt;li>确认 internal 区域的接口绑定正确：&lt;code>sudo firewall-cmd --zone=internal --list-all&lt;/code>&lt;/li>
&lt;li>确认内网主机的默认网关指向防火墙的 internal 接口 IP&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>策略优先级被覆盖？&lt;/strong> 使用更小的优先级数值（如 &lt;code>-200&lt;/code>），firewalld 优先级范围是 &lt;code>-32768&lt;/code> 到 &lt;code>32767&lt;/code>，数值越小越优先。&lt;/p>
&lt;h2 id="参考">参考&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://firewalld.org/documentation/concepts/policies.html">firewalld 官方文档 - Policies&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://firewalld.org/documentation/man-pages/firewalld.zone.html">firewalld.zone(5) man page&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://firewalld.org/documentation/man-pages/firewalld.policy.html">firewalld.policy(5) man page&lt;/a>&lt;/li>
&lt;/ul></content></item><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><item><title>修改 SSH 监听端口与 SELinux 配置</title><link>https://www.0niu.cn/posts/ssh%E7%AB%AF%E5%8F%A3%E4%BF%AE%E6%94%B9%E4%B8%8Eselinux%E9%85%8D%E7%BD%AE/</link><pubDate>Mon, 18 Mar 2024 09:44:00 +0800</pubDate><guid>https://www.0niu.cn/posts/ssh%E7%AB%AF%E5%8F%A3%E4%BF%AE%E6%94%B9%E4%B8%8Eselinux%E9%85%8D%E7%BD%AE/</guid><description>&lt;h2 id="概述">概述&lt;/h2>
&lt;p>出于安全考虑，修改 SSH 默认端口（22）是一个常见的安全加固措施。本文介绍如何修改 SSH 监听端口，并正确配置 SELinux 以允许新端口的访问。&lt;/p>
&lt;h2 id="修改-ssh-监听端口">修改 SSH 监听端口&lt;/h2>
&lt;h3 id="1-编辑-ssh-配置文件">1. 编辑 SSH 配置文件&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>sudo vi /etc/ssh/sshd_config
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>找到 &lt;code>#Port 22&lt;/code> 这一行，取消注释并修改为需要的端口：&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Port 22&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Port 2222&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>建议同时保留默认端口和自定义端口，便于测试。确认新端口正常工作后，再删除默认端口配置。&lt;/p>
&lt;h3 id="2-重启-ssh-服务">2. 重启 SSH 服务&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>sudo systemctl restart sshd
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="3-验证新端口">3. 验证新端口&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"># 检查 SSH 服务监听端口&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo ss -tlnp | grep sshd
&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"># 或使用 netstat&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo netstat -tlnp | grep sshd
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>应该看到新端口（如 2222）已开始监听。&lt;/p></description><content>&lt;h2 id="概述">概述&lt;/h2>
&lt;p>出于安全考虑，修改 SSH 默认端口（22）是一个常见的安全加固措施。本文介绍如何修改 SSH 监听端口，并正确配置 SELinux 以允许新端口的访问。&lt;/p>
&lt;h2 id="修改-ssh-监听端口">修改 SSH 监听端口&lt;/h2>
&lt;h3 id="1-编辑-ssh-配置文件">1. 编辑 SSH 配置文件&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>sudo vi /etc/ssh/sshd_config
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>找到 &lt;code>#Port 22&lt;/code> 这一行，取消注释并修改为需要的端口：&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Port 22&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Port 2222&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>建议同时保留默认端口和自定义端口，便于测试。确认新端口正常工作后，再删除默认端口配置。&lt;/p>
&lt;h3 id="2-重启-ssh-服务">2. 重启 SSH 服务&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>sudo systemctl restart sshd
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="3-验证新端口">3. 验证新端口&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"># 检查 SSH 服务监听端口&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo ss -tlnp | grep sshd
&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"># 或使用 netstat&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo netstat -tlnp | grep sshd
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>应该看到新端口（如 2222）已开始监听。&lt;/p>
&lt;h2 id="配置-selinux">配置 SELinux&lt;/h2>
&lt;p>如果在启用了 SELinux 的系统上修改 SSH 端口，必须更新 SELinux 策略，否则 SSH 服务无法正常绑定到新端口。&lt;/p>
&lt;h3 id="1-检查当前-selinux-状态">1. 检查当前 SELinux 状态&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"># 查看 SELinux 状态&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sestatus
&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"># 查看 SSH 允许的端口&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo semanage port -l | grep ssh
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>默认情况下，SELinux 只允许 SSH 使用端口 22。&lt;/p>
&lt;h3 id="2-添加新端口到-selinux-策略">2. 添加新端口到 SELinux 策略&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"># 添加新端口（如 2222）到 SSH 服务&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo semanage port -a -t ssh_port_t -p tcp &lt;span style="color:#ae81ff">2222&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>参数说明：&lt;/p>
&lt;ul>
&lt;li>&lt;code>-a&lt;/code>：添加&lt;/li>
&lt;li>&lt;code>-t ssh_port_t&lt;/code>：指定端口类型为 SSH 端口&lt;/li>
&lt;li>&lt;code>-p tcp&lt;/code>：协议为 TCP&lt;/li>
&lt;li>&lt;code>2222&lt;/code>：端口号&lt;/li>
&lt;/ul>
&lt;h3 id="3-验证-selinux-配置">3. 验证 SELinux 配置&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"># 再次查看 SSH 允许的端口&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo semanage port -l | grep ssh
&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>ssh_port_t tcp 2222, 22
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="4-删除不需要的端口可选">4. 删除不需要的端口（可选）&lt;/h3>
&lt;p>如果需要从 SELinux 策略中移除某个端口：&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>sudo semanage port -d -t ssh_port_t -p tcp &lt;span style="color:#ae81ff">2222&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="配置防火墙">配置防火墙&lt;/h2>
&lt;p>根据系统使用的防火墙，需要开放新端口。&lt;/p>
&lt;h3 id="firewalldrhelcentosfedora">firewalld（RHEL/CentOS/Fedora）&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 firewall-cmd --permanent --add-port&lt;span style="color:#f92672">=&lt;/span>2222/tcp
&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>sudo firewall-cmd --reload
&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>sudo firewall-cmd --list-ports
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="ufwubuntudebian">ufw（Ubuntu/Debian）&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 ufw allow 2222/tcp
&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>sudo ufw status
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="iptables">iptables&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 iptables -A INPUT -p tcp --dport &lt;span style="color:#ae81ff">2222&lt;/span> -j ACCEPT
&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>sudo service iptables save
&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>sudo netfilter-persistent save
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="测试连接">测试连接&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>ssh -p &lt;span style="color:#ae81ff">2222&lt;/span> username@server_ip
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>如果连接成功，可以回到 SSH 配置文件中删除默认端口（22），然后重启 SSH 服务：&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Port 2222&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&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>sudo systemctl restart sshd
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="常见问题">常见问题&lt;/h2>
&lt;h3 id="问题-1修改端口后无法连接">问题 1：修改端口后无法连接&lt;/h3>
&lt;p>&lt;strong>可能原因&lt;/strong>：&lt;/p>
&lt;ul>
&lt;li>SELinux 未配置允许新端口&lt;/li>
&lt;li>防火墙未开放新端口&lt;/li>
&lt;li>SSH 服务未正确重启&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>排查步骤&lt;/strong>：&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"># 检查 SELinux 日志&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo ausearch -m avc -ts recent | grep sshd
&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"># 检查 SSH 服务状态&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl status sshd
&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>sudo ss -tlnp | grep &lt;span style="color:#ae81ff">2222&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="问题-2semanage-命令不存在">问题 2：semanage 命令不存在&lt;/h3>
&lt;p>&lt;strong>解决方法&lt;/strong>：&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"># RHEL/CentOS/Fedora&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo dnf install policycoreutils-python-utils
&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"># Ubuntu/Debian&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo apt install policycoreutils-python-utils
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="问题-3selinux-阻止-ssh-绑定端口">问题 3：SELinux 阻止 SSH 绑定端口&lt;/h3>
&lt;p>查看 SELinux 拒绝日志：&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>sudo ausearch -m avc -ts recent | grep sshd | grep denied
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>如果看到类似 &lt;code>bind&lt;/code> 操作被拒绝，说明 SELinux 策略未正确配置，按照上述步骤添加端口即可。&lt;/p>
&lt;h2 id="安全建议">安全建议&lt;/h2>
&lt;ol>
&lt;li>&lt;strong>选择非标准端口&lt;/strong>：避免使用容易被扫描的端口，如 2222、22222 等&lt;/li>
&lt;li>&lt;strong>定期更换端口&lt;/strong>：在高安全要求的环境中，定期更换 SSH 端口&lt;/li>
&lt;li>&lt;strong>结合其他安全措施&lt;/strong>：
&lt;ul>
&lt;li>启用密钥认证，禁用密码登录&lt;/li>
&lt;li>配置 fail2ban 防止暴力破解&lt;/li>
&lt;li>限制允许访问的 IP 地址&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>保持 SELinux 启用&lt;/strong>：SELinux 提供了额外的安全防护层，建议保持启用状态&lt;/li>
&lt;/ol>
&lt;h2 id="参考资料">参考资料&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://docs.redhat.com/zh-cn/documentation/red_hat_enterprise_linux/9/html/using_selinux/configuring-selinux-policies_configuring-selinux">Red Hat SELinux 端口管理文档&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.openssh.com/manual.html">OpenSSH 官方文档&lt;/a>&lt;/li>
&lt;/ul></content></item><item><title>Linux 笔记本电脑合上盖子不休眠设置</title><link>https://www.0niu.cn/posts/linux-laptop-lid-close-no-suspend/</link><pubDate>Sun, 25 Nov 2018 16:13:02 +0800</pubDate><guid>https://www.0niu.cn/posts/linux-laptop-lid-close-no-suspend/</guid><description>&lt;h2 id="概述">概述&lt;/h2>
&lt;p>默认情况下，Linux 笔记本电脑在合上盖子时会进入休眠/挂起状态。但在某些场景下，我们希望合上盖子后系统继续运行，例如：&lt;/p>
&lt;ul>
&lt;li>连接外接显示器使用笔记本作为主机&lt;/li>
&lt;li>运行下载任务或服务器服务&lt;/li>
&lt;li>合盖后继续编译或渲染任务&lt;/li>
&lt;li>作为家庭服务器使用&lt;/li>
&lt;/ul>
&lt;h2 id="解决方法">解决方法&lt;/h2>
&lt;p>通过修改 &lt;code>systemd-logind&lt;/code> 配置文件即可实现合盖不休眠。&lt;/p>
&lt;h3 id="步骤">步骤&lt;/h3>
&lt;p>&lt;strong>1. 编辑配置文件&lt;/strong>&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>sudo vi /etc/systemd/logind.conf
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>2. 修改配置项&lt;/strong>&lt;/p>
&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">#HandleLidSwitch=suspend&lt;/span>
&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">HandleLidSwitch&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>3. 可选：同时配置其他相关选项&lt;/strong>&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-ini" data-lang="ini">&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>&lt;span style="color:#a6e22e">HandleLidSwitch&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&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>&lt;span style="color:#a6e22e">HandleLidSwitchExternalPower&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&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>&lt;span style="color:#a6e22e">HandleLidSwitchDocked&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>4. 重启系统或重启 systemd-logind 服务&lt;/strong>&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"># 方法一：重启系统&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo reboot
&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"># 方法二：重启 systemd-logind 服务（无需重启）&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl restart systemd-logind
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description><content>&lt;h2 id="概述">概述&lt;/h2>
&lt;p>默认情况下，Linux 笔记本电脑在合上盖子时会进入休眠/挂起状态。但在某些场景下，我们希望合上盖子后系统继续运行，例如：&lt;/p>
&lt;ul>
&lt;li>连接外接显示器使用笔记本作为主机&lt;/li>
&lt;li>运行下载任务或服务器服务&lt;/li>
&lt;li>合盖后继续编译或渲染任务&lt;/li>
&lt;li>作为家庭服务器使用&lt;/li>
&lt;/ul>
&lt;h2 id="解决方法">解决方法&lt;/h2>
&lt;p>通过修改 &lt;code>systemd-logind&lt;/code> 配置文件即可实现合盖不休眠。&lt;/p>
&lt;h3 id="步骤">步骤&lt;/h3>
&lt;p>&lt;strong>1. 编辑配置文件&lt;/strong>&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>sudo vi /etc/systemd/logind.conf
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>2. 修改配置项&lt;/strong>&lt;/p>
&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">#HandleLidSwitch=suspend&lt;/span>
&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">HandleLidSwitch&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>3. 可选：同时配置其他相关选项&lt;/strong>&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-ini" data-lang="ini">&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>&lt;span style="color:#a6e22e">HandleLidSwitch&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&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>&lt;span style="color:#a6e22e">HandleLidSwitchExternalPower&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&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>&lt;span style="color:#a6e22e">HandleLidSwitchDocked&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">ignore&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>4. 重启系统或重启 systemd-logind 服务&lt;/strong>&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"># 方法一：重启系统&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo reboot
&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"># 方法二：重启 systemd-logind 服务（无需重启）&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl restart systemd-logind
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="验证配置">验证配置&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"># 查看当前 logind 配置&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>loginctl show | grep HandleLid
&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>systemctl show systemd-logind | grep HandleLid
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>预期输出应包含 &lt;code>HandleLidSwitch=ignore&lt;/code>。&lt;/p>
&lt;h2 id="恢复默认设置">恢复默认设置&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-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">HandleLidSwitch&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">suspend&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>然后重启服务或系统即可。&lt;/p>
&lt;h2 id="注意事项">注意事项&lt;/h2>
&lt;p>⚠️ &lt;strong>重要提醒：&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>&lt;strong>散热问题&lt;/strong>：合盖后继续运行可能导致散热不良，尤其是高性能任务时&lt;/li>
&lt;li>&lt;strong>电池消耗&lt;/strong>：不休眠会持续消耗电池电量&lt;/li>
&lt;li>&lt;strong>屏幕保护&lt;/strong>：合盖后屏幕会自动关闭，这是正常现象&lt;/li>
&lt;li>&lt;strong>外接显示器&lt;/strong>：如使用外接显示器，建议同时设置 &lt;code>HandleLidSwitchExternalPower=ignore&lt;/code>&lt;/li>
&lt;/ol>
&lt;h2 id="其他相关配置">其他相关配置&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>配置项&lt;/th>
&lt;th>可选值&lt;/th>
&lt;th>说明&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>HandleLidSwitch&lt;/td>
&lt;td>ignore/suspend/hibernate/lock&lt;/td>
&lt;td>合盖时的行为&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>HandleLidSwitchExternalPower&lt;/td>
&lt;td>ignore/suspend/hibernate/lock&lt;/td>
&lt;td>连接电源时合盖的行为&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>HandleLidSwitchDocked&lt;/td>
&lt;td>ignore/suspend/hibernate/lock&lt;/td>
&lt;td>对接状态下合盖的行为&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="故障排查">故障排查&lt;/h2>
&lt;p>如果修改后仍然休眠，检查：&lt;/p>
&lt;ol>
&lt;li>配置是否正确保存（无语法错误）&lt;/li>
&lt;li>是否重启了 &lt;code>systemd-logind&lt;/code> 服务或系统&lt;/li>
&lt;li>是否有其他电源管理工具冲突（如 TLP、powertop）&lt;/li>
&lt;li>查看系统日志：&lt;code>journalctl -u systemd-logind&lt;/code>&lt;/li>
&lt;/ol></content></item></channel></rss>