使用Firewalld管理CentOS 7上的防火墙
Yangqingyan
・13 分钟阅读
FirewallD是动态管理的防火墙,可为基于RHEL 7的服务器上提供的IPv4和IPv6防火墙规则提供支持。它直接替换iptables
,并与内核代码的netfilter
一起工作。
在本文中,我们将简要介绍,如何使用firewall-cmd命令管理CentOS 7上的防火墙。
检查防火墙是否正在运行
第一步是检查防火墙是否已安装和运行,可以通过运行以下命令通过systemd
完成这个操作:
$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2016-03-10 15:07:00 UTC; 1min 30s ago
...
或者,你也可以使用firewall-cmd
工具进行检查:
$ firewall-cmd --state
running
管理zones(区域)
防火墙使用zones
的概念操作,zones定义了用于连接的信任级别,为了对每个接口应用特定的防火墙规则,你可以将不同的网络接口分割为不同的zones,或者你可以对所有接口使用一个。
开箱即用,一切都在默认的public zone里面完成,但是,也可以应用其他几个预先配置的区域。
列出所有可用zones
你可能需要获得所有可用zones的列表,其中有几个出现在框中,同样,可以使用firewall-cmd
来完成这个操作:
$ firewall-cmd --get-zones
block dmz drop external home internal public trusted work
检查默认zones
你可以发现当前使用firewall-cmd
配置的缺省zones:
$ firewall-cmd --get-default-zone
public
如果希望更改默认zones(例如,home
),可以通过运行以下命令来完成这个操作:
$ firewall-cmd --set-default-zone=home
success
在主配置文件/etc/firewalld/firewalld.conf中,但是,建议不要手动修改此文件,而是使用firewall-cmd
。
检查当前分配的zones
你可以通过运行以下命令获取已分配给它接口的zones列表:
$ firewall-cmd --get-active-zones
public
interfaces: eth0
还可以通过运行以下命令检查单个接口(eth0
中)的zones:
$ firewall-cmd --get-zone-of-interface=eth0
public
创建zones
如果缺省配置zones不适合你的需要,则创建新zones(zone1 ),最简单方法是通过firewall-cmd :
$ firewall-cmd --permanent --new-zone=zone1
success
创建后,需要重新加载:
$ firewall-cmd --reload
success
将zones应用于接口
为了永久地将网络接口分配给zones,可以使用firewall-cmd --permanent标志以保持更改,如果使用NetworkManager
,也应该确保使用nmcli
来设置连接zones。
$ firewall-cmd --permanent --zone=internal --change-interface=eth1`
success
获取zones的永久配置
检查zones(public中)的永久配置,包括分配的接口,允许的服务,端口设置:
$ firewall-cmd --permanent --zone=public --list-all
public (default)
interfaces:
sources:
services: dhcpv6-client ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
管理服务(services)
分配并配置所需zones后,即可开始向zones添加服务,
列出现有服务
防火墙中预先配置了许多公共服务,这些可以列出:
$ firewall-cmd --get-services
RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imaps ipp ipp-client ipsec iscsi-target kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind rsyncd samba samba-client smtp ssh telnet tftp tftp-client transmission-client vdsm vnc-server wbem-https
还可以获取为默认zones启用的服务的列表:
$ firewall-cmd --list-services
dhcpv6-client ssh
向zones中添加服务
你可以使用--add-service
标志永久地为zones(public
)启用给定的服务:
$ firewall-cmd --permanent --zone=public --add-service=http
success
然后重新加载当前防火墙会话:
$ firewall-cmd --reload
success
然后,验证是否已添加:
$ firewall-cmd --zone=public --list-services
dhcpv6-client http ssh
从zones中删除服务
你可以使用--remove-service
标志永久删除zones(public
)的给定服务:
$ firewall-cmd --permanent --zone=public --remove-service=http
success
然后重新加载当前防火墙会话:
$ firewall-cmd --reload
success
然后,验证是否已添加:
$ firewall-cmd --zone=public --list-services
dhcpv6-client ssh
添加/删除zones中的多个服务
可以一次从zones中添加或删除多个服务(例如,http
和https
),或者通过在大括号({
,}
)中包装所需的服务名称一次:
$ firewall-cmd --permanent --zone=public --add-service=
success
$ firewall-cmd --permanent --zone=public --list-services
dhcpv6-client http https ssh
创建新服务
有时你可能需要添加新的定制服务-例如,如果你已经更改了SSH守护进程的端口,使用简单的XML文件定义服务,并在/usr/lib/firewalld/services
中找到缺省文件:
$ tree /usr/lib/firewalld/services
/usr/lib/firewalld/services
├── amanda-client.xml
├── bacula-client.xml
├── bacula.xml
├── dhcpv6-client.xml
├── dhcpv6.xml
├── dhcp.xml
├── dns.xml
├── freeipa-ldaps.xml
├── freeipa-ldap.xml
├── freeipa-replication.xml
├── ftp.xml
├── high-availability.xml
├── https.xml
├── http.xml
...
创建新服务的最简单方法是复制这些现有服务文件,并且修改它,自定义服务应驻留在/etc/firewalld/services
中,例如要自定义SSH服务:
$ cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh-custom.xml
此复制文件的内容应类似于:
$ cat /etc/firewalld/services/ssh-custom.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="22"/>
</service>
为了更改端口,你应该更改服务的名称,然后更改端口,在本例中,我将端口更改为1234 :
$ nano /etc/firewalld/services/ssh-custom.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH-Custom</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="1234"/>
</service>
保存后,需要重新加载防火墙,然后你可以将规则应用到你的zones:
$ firewall-cmd --reload
success
$ firewall-cmd --permanent --zone=public --add-service=ssh-custom
success
端口管理
除了使用服务之外,还可以通过协议手动允许端口,若要允许public
zones使用TCP端口7777
,请执行下列操作:
$ firewall-cmd --permanent --zone=public --add-port=7777/tcp
success
你还可以添加端口范围:
$ firewall-cmd --permanent --zone=public --add-port=7000-8000/tcp
success
要删除public
zones的(因此拒绝)TCP端口7777
,请执行以下操作:
$ firewall-cmd --permanent --zone=public --remove-port=7777/tcp
success
你还可以在重新加载当前防火墙会话后,为给定zones(public
)列出当前允许的端口:
$ firewall-cmd --zone=public --list-ports
7000-8000/tcp
启用防火墙
一旦你按照自己的喜好配置了防火墙,你应该确保通过systemd启用它,以确保它在系统引导时自动启动:
$ systemctl enable firewalld
结束语
防火墙中还有很多设置和选项,比如,端口转发,伪装和通过D-Bus与防火墙通信,希望本指南能够帮助你掌握基础知识,下面的一些额外阅读将帮助你充分利用防火墙。
- 使用Fail2ban和FirewallD - fedora Wiki
- FirewallD - Fedora Wiki
- FirewallD简介-RedHat 7 Security Guide