Ubuntu上启用或禁用UFW防火墙的2种实用方法
<p>UFW(Uncomplicated Firewall)是 Ubuntu 系统中预装的一款防火墙管理工具。它设计简洁,本质上是一个 iptables 的前端,为用户提供了一个更友好、更直观的方式来管理 iptables 规则。</p><p>无论你是 Ubuntu 桌面版用户,还是管理 Ubuntu Server 的系统管理员,UFW 都是一个可靠且高效的防火墙管理工具。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451001.jpg" /></p>
<p>UFW 的主要特点包括:</p>
<ul><li><strong>简便易用</strong>:UFW 的命令行语法非常直观,即使你不是防火墙专家,也能轻松上手。</li><li><strong>默认策略</strong>:UFW 提供了默认的安全策略,在启用防火墙时就能获得基本的安全保护。</li><li><strong>灵活性高</strong>:通过 UFW,你可以轻松地添加、修改和删除规则,满足不同场景的需求。</li><li><strong>日志记录</strong>:UFW 支持详细的日志记录,方便你查看防火墙活动并排除故障。</li></ul>
<p>接下来,本文将通过简单的命令或图形界面,介绍如何在 Ubuntu 系统(包括服务器和桌面版)上启用或禁用 UFW 防火墙。</p>
<blockquote><p><strong>推荐阅读:</strong><a href="https://www.jb51.net/os/Ubuntu/991394.html" target="_blank">如何安装、配置和使用 ClamAV 杀毒软件</a></p></blockquote>
<h3>方法 1:使用命令行启用或禁用 UFW 防火墙</h3>
<blockquote><p>适用于 Ubuntu 桌面版和 Ubuntu Server。</p></blockquote>
<p>在 Ubuntu 上启用 UFW 可以保护你的网络安全。当启用 UFW 时,默认会阻止所有入站连接,同时允许所有出站连接。</p>
<blockquote><p>新安装的 Ubuntu 系统默认不启用 UFW。</p></blockquote>
<h4>放行 SSH 连接</h4>
<p>如果你的 Ubuntu 系统需要 SSH 远程连接,特别是 Ubuntu Server,在启用 UFW 之前,一定要先放行 SSH 的入站连接,确保不会丢失服务器的远程访问权限:</p>
<div class="jb51code"><pre class="brush:xhtml;">sudo ufw allow ssh</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451002.jpg" /></p>
<p style="text-align:center">通过 ufw 放行入站连接</p>
<h4>启用 UFW 防火墙</h4>
<p>1、在「终端」中执行以下命令启用 UFW:</p>
<div class="jb51code"><pre class="brush:xhtml;">sudo ufw enable</pre></div>
<p>2、启用 UFW 后,可以通过以下命令检查防火墙状态:</p>
<div class="jb51code"><pre class="brush:xhtml;">sudo ufw status</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451003.jpg" /></p>
<p style="text-align:center">启用 ufw 并查看状态</p>
<p>3、要获得更全面的信息,包括默认策略和日志级别,可以执行:</p>
<div class="jb51code"><pre class="brush:xhtml;">sudo ufw status verbose</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451004.jpg" /></p>
<p style="text-align:center">查看 ufw 状态及全面信息</p>
<h4>禁用 UFW 防火墙</h4>
<p>要禁用 UFW 防火墙,请使用以下命令:</p>
<div class="jb51code"><pre class="brush:xhtml;">sudo ufw disable</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451005.jpg" /></p>
<p style="text-align:center">禁用 ufw 并查看状态</p>
<blockquote><p>禁用 UFW 之后,相当于所有入站和出站连接全开,完全不设防。</p></blockquote>
<h4>UFW 常用命令汇总表</h4>
<table><tbody><tr><th>命令</th><th>作用</th><th>示例</th></tr><tr><td><code>ufw status</code></td><td>查看防火墙状态</td><td><code>sudo ufw status</code></td></tr><tr><td><code>ufw allow [规则]</code></td><td>添加允许规则</td><td><code>sudo ufw allow ssh</code><code>sudo ufw allow http</code></td></tr><tr><td><code>ufw deny [规则]</code></td><td>添加拒绝规则</td><td><code>sudo ufw deny 25</code><code>sudo ufw deny from 192.168.1.100</code></td></tr><tr><td><code>ufw delete [规则编号]</code></td><td>删除指定编号的规则</td><td>规则编号可在<code>ufw status</code>命令的输出中找到</td></tr><tr><td><code>ufw reset</code></td><td>重置所有规则为默认状态</td><td><code>sudo ufw reset</code></td></tr><tr><td><code>ufw default [策略]</code></td><td>设置默认策略</td><td><code>sudo ufw default deny</code><code>sudo ufw default allow</code></td></tr><tr><td><code>ufw logging [级别]</code></td><td>设置日志记录级别</td><td><code>sudo ufw logging of</code><code>sudo ufw logging medium</code></td></tr><tr><td><code>ufw profile [配置文件名称]</code></td><td>创建或切换配置文件</td><td><code>sudo ufw profile myprofile</code></td></tr><tr><td><code>sudo ufw profile default</code></td><td></td><td></td></tr><tr><td><code>ufw help</code></td><td>查看 UFW 命令帮助信息</td><td><code>sudo ufw help</code></td></tr><tr><td><code>man ufw</code></td><td>查看 UFW 命令手册</td><td><code>man ufw</code></td></tr></tbody></table>
<h3>方法 2:使用 GUFW 防火墙配置工具</h3>
<blockquote><p>适用于 Ubuntu 桌面版。</p></blockquote>
<p>1、GUFW 是一个用户友好的 UFW 图形界面,相当于给 UFW 又套了一个图形界面的皮,可以通过以下命令来安装:</p>
<div class="jb51code"><pre class="brush:xhtml;">sudo apt install gufw</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451006.jpg" /></p>
<p style="text-align:center">安装 gufw 图形界面</p>
<p>2、安装完成后,在「显示应用」中找到并打开「防火墙配置」。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451007.jpg" /></p>
<p style="text-align:center">打开 gufw(防火墙配置)工具</p>
<p>3、GUFW 提供了直观的用户界面,方便你轻松管理各种防火墙设置,例如:</p>
<ul><li>调整默认的入站和出站连接设置</li><li>查看防火墙当前的状态</li><li>自定义防火墙规则</li><li>查看日志和报告</li></ul>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202507/202507041032451008.jpg" /></p>
<p style="text-align:center">使用 GUFW 防火墙配置工具</p>
<p>GUFW 图形界面让防火墙管理变得更加简单,非常适合不熟悉命令行操作的 Ubuntu 桌面版用户。</p>
<p>UFW 为 Ubuntu 用户提供了一个便捷而强大的防火墙管理工具。无论你是通过命令行快速设置基本规则,还是使用 GUFW 图形界面进行更直观的管理,UFW 都能满足你的需求。</p>
頁:
[1]