firewalld 使用区域和服务而不是链式规则。
它动态管理规则集允许更新规则而不破坏现有会话和连接。
firewalld 是 iptables 的一个封装可以让你更容易地管理 iptables 规则 - 它并不是 iptables 的替代品。虽然 iptables 命令仍可用于 firewalld但建议使用 firewalld 时仅使用 firewalld 命令。
本指南将向您介绍 firewalld 的区域和服务的概念以及一些基本的配置步骤。
<a target="_blank"></a>
centos 7 和 fedora 20+ 已经包含了 firewalld但是默认没有激活。可以像其它的 systemd 单元那样控制它。
1、 启动服务并在系统引导时启动该服务
<code>sudo systemctl start firewalld</code>
<code>sudo systemctl enable firewalld</code>
要停止并禁用
<code>sudo systemctl stop firewalld</code>
<code>sudo systemctl disable firewalld</code>
2、 检查防火墙状态。输出应该是 <code>running</code> 或者 <code>not running</code>。
<code>sudo firewall-cmd --state</code>
3、 要查看 firewalld 守护进程的状态
<code>sudo systemctl status firewalld</code>
示例输出
<code>firewalld.service - firewalld - dynamic firewall daemon</code>
<code>loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled)</code>
<code>active: active (running) since wed 2015-09-02 18:03:22 utc; 1min 12s ago</code>
<code>main pid: 11954 (firewalld)</code>
<code>cgroup: /system.slice/firewalld.service</code>
<code>└─11954 /usr/bin/python -es /usr/sbin/firewalld --nofork --nopid</code>
4、 重新加载 firewalld 配置
<code>sudo firewall-cmd --reload</code>
firewalld 使用 xml 进行配置。除非是非常特殊的配置你不必处理它们而应该使用 <code>firewall-cmd</code>。
配置文件位于两个目录中
<code>/usr/lib/firewalld</code> 下保存默认配置如默认区域和公用服务。 避免修改它们因为每次 firewall 软件包更新时都会覆盖这些文件。
<code>/etc/firewalld</code> 下保存系统配置文件。 这些文件将覆盖默认配置。
firewalld 使用两个配置集“运行时”和“持久”。 在系统重新启动或重新启动 firewalld 时不会保留运行时的配置更改而对持久配置集的更改不会应用于正在运行的系统。
默认情况下<code>firewall-cmd</code> 命令适用于运行时配置但使用 <code>--permanent</code> 标志将保存到持久配置中。要添加和激活持久性规则你可以使用两种方法之一。
1、 将规则同时添加到持久规则集和运行时规则集中。
<code>sudo firewall-cmd --zone=public --add-service=http --permanent</code>
<code>sudo firewall-cmd --zone=public --add-service=http</code>
2、 将规则添加到持久规则集中并重新加载 firewalld。
<code>reload</code> 命令会删除所有运行时配置并应用永久配置。因为 firewalld 动态管理规则集所以它不会破坏现有的连接和会话。
“区域”是针对给定位置或场景例如家庭、公共、受信任等可能具有的各种信任级别的预构建规则集。不同的区域允许不同的网络服务和入站流量类型而拒绝其他任何流量。 首次启用 firewalld 后<code>public</code> 将是默认区域。
区域也可以用于不同的网络接口。例如要分离内部网络和互联网的接口你可以在 <code>internal</code> 区域上允许 dhcp但在<code>external</code> 区域仅允许 http 和 ssh。未明确设置为特定区域的任何接口将添加到默认区域。
要找到默认区域
<code>sudo firewall-cmd --get-default-zone</code>
要修改默认区域
<code>sudo firewall-cmd --set-default-zone=internal</code>
要查看你网络接口使用的区域
<code>sudo firewall-cmd --get-active-zones</code>
<code>public</code>
<code>interfaces: eth0</code>
要得到特定区域的所有配置
<code>sudo firewall-cmd --zone=public --list-all</code>
<code>public (default, active)</code>
<code>interfaces: ens160</code>
<code>sources:</code>
<code>services: dhcpv6-client http ssh</code>
<code>ports: 12345/tcp</code>
<code>masquerade: no</code>
<code>forward-ports:</code>
<code>icmp-blocks:</code>
<code>rich rules:</code>
要得到所有区域的配置
<code>sudo firewall-cmd --list-all-zones</code>
<code>block</code>
<code>interfaces:</code>
<code>services:</code>
<code>ports:</code>
<code></code>
<code>...</code>
<code>work</code>
<code>services: dhcpv6-client ipp-client ssh</code>
firewalld 可以根据特定网络服务的预定义规则来允许相关流量。你可以创建自己的自定义系统规则并将它们添加到任何区域。 默认支持的服务的配置文件位于 <code>/usr/lib /firewalld/services</code>用户创建的服务文件在 <code>/etc/firewalld/services</code> 中。
要查看默认的可用服务
<code>sudo firewall-cmd --get-services</code>
比如要启用或禁用 http 服务
<code>sudo firewall-cmd --zone=public --remove-service=http --permanent</code>
比如允许或者禁用 12345 端口的 tcp 流量。
<code>sudo firewall-cmd --zone=public --add-port=12345/tcp --permanent</code>
<code>sudo firewall-cmd --zone=public --remove-port=12345/tcp --permanent</code>
下面是在同一台服务器上将 80 端口的流量转发到 12345 端口。
<code>sudo firewall-cmd --zone="public" --add-forward-port=port=80:proto=tcp:toport=12345</code>
要将端口转发到另外一台服务器上
1、 在需要的区域中激活 masquerade。
<code>sudo firewall-cmd --zone=public --add-masquerade</code>
2、 添加转发规则。例子中是将本地的 80 端口的流量转发到 ip 地址为 123.456.78.9 的远程服务器上的 8080 端口。
<code>sudo firewall-cmd --zone="public" --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=123.456.78.9</code>
要删除规则用 <code>--remove</code> 替换 <code>--add</code>。比如
<code>sudo firewall-cmd --zone=public --remove-masquerade</code>
例如以下是如何使用 firewalld 为你的服务器配置基本规则如果您正在运行 web 服务器。
将 <code>eth0</code> 的默认区域设置为 <code>dmz</code>。 在所提供的默认区域中dmz非军事区是最适合于这个程序的因为它只允许 ssh 和 icmp。
<code>sudo firewall-cmd --set-default-zone=dmz</code>
<code>sudo firewall-cmd --zone=dmz --add-interface=eth0</code>
2、 把 http 和 https 添加永久的服务规则到 dmz 区域中
<code>sudo firewall-cmd --zone=dmz --add-service=http --permanent</code>
<code>sudo firewall-cmd --zone=dmz --add-service=https --permanent</code>
3、 重新加载 firewalld 让规则立即生效
如果你运行 <code>firewall-cmd --zone=dmz --list-all</code> 会有下面的输出
<code>dmz (default)</code>
<code>services: http https ssh</code>
这告诉我们<code>dmz</code> 区域是我们的默认区域它被用于 <code>eth0</code> 接口中所有网络的源地址和端口。 允许传入 http端口 80、https端口 443和 ssh端口 22的流量并且由于没有 ip 版本控制的限制这些适用于 ipv4 和 ipv6。 不允许ip 伪装以及端口转发。 我们没有 icmp 块所以 icmp 流量是完全允许的。没有丰富rich规则允许所有出站流量。
服务和端口适用于基本配置但对于高级情景可能会限制较多。 丰富rich规则和直接direct接口允许你为任何端口、协议、地址和操作向任何区域 添加完全自定义的防火墙规则。
这里有一些常见的例子
允许来自主机 192.168.0.14 的所有 ipv4 流量。
<code>sudo firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address=192.168.0.14 accept'</code>
拒绝来自主机 192.168.1.10 到 22 端口的 ipv4 的 tcp 流量。
<code>sudo firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.1.10" port port=22 protocol=tcp reject'</code>
允许来自主机 10.1.0.3 到 80 端口的 ipv4 的 tcp 流量并将流量转发到 6532 端口上。
<code>sudo firewall-cmd --zone=public --add-rich-rule 'rule family=ipv4 source address=10.1.0.3 forward-port port=80 protocol=tcp to-port=6532'</code>
将主机 172.31.4.2 上 80 端口的 ipv4 流量转发到 8080 端口需要在区域上激活 masquerade。
<code>sudo firewall-cmd --zone=public --add-rich-rule 'rule family=ipv4 forward-port port=80 protocol=tcp to-port=8080 to-addr=172.31.4.2'</code>
列出你目前的丰富规则
<code>sudo firewall-cmd --list-rich-rules</code>
对于最高级的使用或对于 iptables 专家firewalld 提供了一个直接direct接口允许你给它传递原始 iptables 命令。 直接接口规则不是持久的除非使用 <code>--permanent</code>。
要查看添加到 firewalld 的所有自定义链或规则
<code>firewall-cmd --direct --get-all-chains</code>
<code>firewall-cmd --direct --get-all-rules</code>
原文发布时间为2017-01-07
本文来自云栖社区合作伙伴“linux中国”