<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>安全 on HiDa</title><link>https://www.0niu.cn/tags/%E5%AE%89%E5%85%A8/</link><description>Recent content in 安全 on HiDa</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Mon, 18 Mar 2024 09:44:00 +0800</lastBuildDate><atom:link href="https://www.0niu.cn/tags/%E5%AE%89%E5%85%A8/index.xml" rel="self" type="application/rss+xml"/><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>