<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>SELinux on HiDa</title><link>https://www.0niu.cn/tags/selinux/</link><description>Recent content in SELinux on HiDa</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Thu, 06 Mar 2025 10:43:00 +0800</lastBuildDate><atom:link href="https://www.0niu.cn/tags/selinux/index.xml" rel="self" type="application/rss+xml"/><item><title>Prometheus 监控平台搭建</title><link>https://www.0niu.cn/posts/prometheus%E7%9B%91%E6%8E%A7%E5%B9%B3%E5%8F%B0%E6%90%AD%E5%BB%BA/</link><pubDate>Thu, 06 Mar 2025 10:43:00 +0800</pubDate><guid>https://www.0niu.cn/posts/prometheus%E7%9B%91%E6%8E%A7%E5%B9%B3%E5%8F%B0%E6%90%AD%E5%BB%BA/</guid><description>&lt;p>本文介绍如何在 Linux 系统上搭建 Prometheus 监控平台。&lt;/p>
&lt;h2 id="安装步骤">安装步骤&lt;/h2>
&lt;h3 id="1-下载并解压-prometheus">1. 下载并解压 Prometheus&lt;/h3>
&lt;p>下载预编译的 Prometheus tar 包并解压到 &lt;code>/opt/prometheus&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>&lt;span style="color:#75715e"># 下载最新版本的 Prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.linux-amd64.tar.gz
&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>tar xzf prometheus-*.linux-amd64.tar.gz
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>mkdir -p /opt/prometheus
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cd prometheus-*.linux-amd64
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp prometheus promtool /opt/prometheus/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp -r consoles prometheus.yml /opt/prometheus/
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="2-创建系统用户和数据目录">2. 创建系统用户和数据目录&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"># 创建 prometheus 用户&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo useradd --no-create-home --shell /bin/false prometheus
&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 mkdir -p /app/prometheus/data
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo chown -R prometheus:prometheus /app/prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description><content>&lt;p>本文介绍如何在 Linux 系统上搭建 Prometheus 监控平台。&lt;/p>
&lt;h2 id="安装步骤">安装步骤&lt;/h2>
&lt;h3 id="1-下载并解压-prometheus">1. 下载并解压 Prometheus&lt;/h3>
&lt;p>下载预编译的 Prometheus tar 包并解压到 &lt;code>/opt/prometheus&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>&lt;span style="color:#75715e"># 下载最新版本的 Prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.linux-amd64.tar.gz
&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>tar xzf prometheus-*.linux-amd64.tar.gz
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>mkdir -p /opt/prometheus
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cd prometheus-*.linux-amd64
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp prometheus promtool /opt/prometheus/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp -r consoles prometheus.yml /opt/prometheus/
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="2-创建系统用户和数据目录">2. 创建系统用户和数据目录&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"># 创建 prometheus 用户&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo useradd --no-create-home --shell /bin/false prometheus
&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 mkdir -p /app/prometheus/data
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo chown -R prometheus:prometheus /app/prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="3-编写-systemd-服务文件">3. 编写 Systemd 服务文件&lt;/h3>
&lt;p>创建服务文件 &lt;code>/etc/systemd/system/prometheus.service&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:#66d9ef">[Unit]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Description&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">Prometheus Monitoring&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">After&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">network-online.target&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Wants&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">network-online.target&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:#66d9ef">[Service]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">User&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">WorkingDirectory&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">/app/prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Restart&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">on-failure&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">RestartSec&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">5s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ExecStart&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">/opt/prometheus/prometheus \
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74"> --config.file=/opt/prometheus/prometheus.yml \
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74"> --storage.tsdb.path=/app/prometheus/data&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"># Security hardening&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">NoNewPrivileges&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">true&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ProtectSystem&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">strict&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ProtectHome&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">true&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ReadWritePaths&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">/app/prometheus/data&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"># Resource limits&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">MemoryMax&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">4G&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:#66d9ef">[Install]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">WantedBy&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">multi-user.target&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="4-修改-selinux-配置">4. 修改 SELinux 配置&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 restorecon -Rv /etc/systemd/system/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo chcon -t bin_t /opt/prometheus/prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="5-启动服务">5. 启动服务&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"># 重新加载 systemd 配置&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl daemon-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"># 启用并启动 Prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl enable --now prometheus
&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 systemctl status prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="验证安装">验证安装&lt;/h2>
&lt;p>访问 &lt;code>http://服务器IP:9090&lt;/code> 查看 Prometheus Web 界面。&lt;/p>
&lt;h2 id="配置文件">配置文件&lt;/h2>
&lt;p>主配置文件位于 &lt;code>/opt/prometheus/prometheus.yml&lt;/code>，根据需要添加监控目标和告警规则。&lt;/p>
&lt;h2 id="参考资料">参考资料&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://prometheus.io/docs/">Prometheus 官方文档&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://github.com/prometheus/prometheus/releases">Prometheus GitHub Releases&lt;/a>&lt;/li>
&lt;/ul></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></channel></rss>