CentOS 7 防火墙FirewallD的常用命令

FirewallD 是 CentOS 7 服务器上默认可用的防火墙管理工具,它通过iptables命令充当Linux内核的netfilter框架的前端,提供防火墙功能,作为iptables服务的替代品。名称FirewallD通过附加字母“d”遵守命名系统守护进程的Unix约定,FirewallD是用Python编写的。

安装FirewallD

yum install firewalld

FirewallD基本命令:

# systemctl start firewalld # 启动,
# systemctl restart firewalld # 重启
# systemctl enable firewalld # 开机启动
# systemctl stop firewalld # 关闭
# systemctl disable firewalld # 取消开机启动
# firewall-cmd --state #查看运行状态
firewall-cmd --zone=public --list-ports # 查看打开端口

命令含义:

--zone #作用域
--add-port=80/tcp  #添加端口,格式为:端口/通讯协议
--permanent   #永久生效,没有此参数重启后失效

打开网站的日常端口

firewall-cmd --zone=public --add-port=22/tcp --permanent

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --zone=public --add-port=443/tcp --permanent

firewall-cmd --zone=public --add-port=3306/tcp --permanent

开启udp端口

firewall-cmd --zone=public --add-port=443/udp --permanent

firewalld对指定IP开放指定端口的配置

1.打开firewalld防火墙

systemctl start firewalld.service

2.添加防火墙规则(对指定ip开放指定端口)
(以下红色字体需要根据实际情况修改)

#(1) Postgresql端口设置。允许192.168.142.166访问5432端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="5432" accept"
#(2)redis端口设置。允许192.168.142.166访问6379端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="6379" accept"
#(3)beanstalkd端口设置。允许192.168.142.166访问11300端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept"

3.重启防火墙,使配置生效

systemctl restart firewalld.service

4.查看配置结果,验证配置

firewall-cmd --list-all

5.删除规则

示例:

firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept"
systemctl restart firewalld.service

Centos7防火墙firewalld配置常用命令

1. firewalld的基本使用方法

启动命令如下

systemctl start firewalld

状态查看

systemctl status firewalld

停止运行

systemctl stop firewalld

禁止开机运行

systemctl disable firewalld

允许开机允许

systemctl enable firewalld

2. 配置firewalld-cmd

查看版本

firewall-cmd --version

查看帮助

firewall-cmd --help

显示状态

firewall-cmd --state

查看所有打开的端口

firewall-cmd --zone=public --list-ports

更新防火墙规则

firewall-cmd --reload

查看区域信息

firewall-cmd --get-active-zones

查看指定接口所属区域

firewall-cmd --get-zone-of-interface=eth0

拒绝所有包

firewall-cmd --panic-on

取消拒绝状态

firewall-cmd --panic-off

查看是否拒绝

firewall-cmd --query-panic

3. 规则添加

单端口添加如下

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=80/udp --permanent

端口范围添加如下

firewall-cmd --zone=public --add-port=80-100/tcp --permanent
firewall-cmd --zone=public --add-port=80-100/udp --permanent

添加完毕,请执行如下命令,重新载入配置

firewall-cmd --reload

CentOS7使用FirewallD管理防火墙

FirewallD 提供了支持网络/防火墙区域(zone)定义网络链接以及接口安全等级的动态防火墙管理工具,它拥有运行时配置和永久配置选项。CentOS 7中防火墙是一个非常的强大的功能,本文主要介绍FirewallD常用管理命令。

安装firewalld
Centos7自带FirewallD防火墙工具,如果你的系统是最小化安装的,可能就需要自己动手安装firewalld服务。

yum install firewalld firewall-config

运行和停止firewalld

systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。我们使用systemctl命令来控制firewalld。

启动防火墙:

systemctl start firewalld.service

关闭防火墙:

systemctl stop firewalld.service

重启防火墙:

systemctl restart firewalld.service

开机时启动防火墙:

systemctl enable firewalld.service

开机时禁用防火墙:

systemctl disable firewalld.service

查看防火墙运行状态:

systemctl status firewalld

配置firewalld-cmd管理防火墙端口
我们使用firewalld的字符界面管理工具firewall-cmd很方便就可以管理firewalld。

显示防火墙状态:

firewall-cmd --state

显示防火墙状态:

firewall-cmd --state

添加80端口,允许访问80端口,并永久生效:

firewall-cmd --zone=public --add-port=80/tcp --permanent

添加服务,允许https访问,并永久生效:

firewall-cmd --zone=public --add-service=https --permanent

移除80端口:

firewall-cmd --zone=public --remove-port=80/tcp --permanent

重新载入使配置生效:

firewall-cmd --reload

查看开放的端口:

firewall-cmd --list-ports

当然,你也可以到/etc/firewalld/zones/目录下直接修改xml配置文件。
习惯用centos7以前版本的伙伴都喜欢用iptable来管理防火墙,设置在使用CentOS7后还把系统自带的firewalld服务弃用,转而用老版本的iptable,可我觉得firewalld很好用啊,简单又实在,好东西为什么不用呢。

防火墙Firewalld 常用命令

运行、停止、禁用firewalld

启动:# systemctl start firewalld
查看状态:# systemctl status firewalld 或者 firewall-cmd –state
停止:# systemctl disable firewalld
禁用:# systemctl stop firewalld

配置firewalld

查看版本:$ firewall-cmd –version
查看帮助:$ firewall-cmd –help

查看设置:

显示状态:$ firewall-cmd –state
查看区域信息: $ firewall-cmd –get-active-zones
查看指定接口所属区域:$ firewall-cmd –get-zone-of-interface=eth0

拒绝所有包:# firewall-cmd –panic-on
取消拒绝状态:# firewall-cmd –panic-off
查看是否拒绝:$ firewall-cmd –query-panic
更新防火墙规则:# firewall-cmd –reload
# firewall-cmd –complete-reload
两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务
将接口添加到区域,默认接口都在public

# firewall-cmd –zone=public –add-interface=eth0

永久生效再加上 –permanent 然后reload防火墙
设置默认接口区域

# firewall-cmd –set-default-zone=public

立即生效无需重启

打开端口(貌似这个才最常用)
查看所有打开的端口:

# firewall-cmd –zone=dmz –list-ports(service)

加入一个端口到区域:

# firewall-cmd –zone=dmz –add-port=8080/tcp

若要永久生效方法同上

# firewall-cmd –zone=work –add-service=smtp

移除服务

# firewall-cmd –zone=work –remove-service=smtp

CentOS 7安装fail2ban+Firewalld防止SSH爆破与CC攻击

说明:差不多很多博主都会遇到被CC攻击和SSH爆破的情况,这个时候就需要做下防御措施了,前几天发过一个防CC脚本,参考: https://www.moerats.com/archives/484/ ,不过对于CentOS 7来说,由于防火墙的因素,配置起来还是有点麻烦的,这里争对CentOS 7再分享个简单防CC攻击和SSH爆破的方法。

前言

fail2ban可以监视你的系统日志,然后匹配日志的错误信息执行相应的屏蔽动作。网上大部分教程都是关于fail2ban+iptables组合,考虑到CentOS 7已经自带Firewalld,所以这里我们也可以利用fail2ban+Firewalld来防CC攻击和SSH爆破。

本教程编辑文件使用vi命令,如果不会用的,可以使用比较简单的nano,可以参考: https://www.moerats.com/archives/485/ ,或者使用FTP工具,如WinSCP登录VPS操作。

准备工作

1、检查Firewalld是否启用

#如果您已经安装iptables建议先关闭
service iptables stop
#查看Firewalld状态
firewall-cmd --state
#启动firewalld
systemctl start firewalld
#设置开机启动
systemctl enable firewalld.service

启用Firewalld后会禁止所有端口连接,因此请务必放行常用的端口,以免被阻挡在外,以下是放行SSH端口(22)示例,供参考:

#放行22端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
#重载配置
firewall-cmd --reload
#查看已放行端口
firewall-cmd --zone=public --list-ports

2、安装fail2ban

fail2ban可以监控系统日志,并且根据一定规则匹配异常IP后使用Firewalld将其屏蔽,尤其是针对一些爆破/扫描等非常有效。

#CentOS内置源并未包含fail2ban,需要先安装epel源
yum -y install epel-release
#安装fial2ban
yum -y install fail2ban

安装成功后fail2ban配置文件位于/etc/fail2ban,其中jail.conf为主配置文件,相关的匹配规则位于filter.d目录,其它目录/文件一般很少用到,如果需要详细了解可自行搜索。

3、配置规则

新建jail.local来覆盖fail2ban的一些默认规则:

#新建配置
vi /etc/fail2ban/jail.local
#默认配置
[DEFAULT]
ignoreip = 127.0.0.1/8
bantime  = 86400
findtime = 600
maxretry = 5
#这里banaction必须用firewallcmd-ipset,这是fiewalll支持的关键,如果是用Iptables请不要这样填写
banaction = firewallcmd-ipset
action = %(action_mwl)s

参数说明:

ignoreip:IP白名单,白名单中的IP不会屏蔽,可填写多个以(,)分隔
bantime:屏蔽时间,单位为秒(s)
findtime:时间范围
maxretry:最大次数
banaction:屏蔽IP所使用的方法,上面使用firewalld屏蔽端口

防止SSH爆破

如果您还在使用默认SSH端口(22),可能每天都会被扫描,我们可以修改端口尽量避免被扫,参考: https://www.moerats.com/archives/394/ ,或者可以使用fail2ban将恶意IP屏蔽。

继续修改jail.local这个配置文件,在后面追加如下内容:

[sshd]
enabled = true
filter  = sshd
port    = 22
action = %(action_mwl)s
logpath = /var/log/secure

参数说明:

[sshd]:名称,可以随便填写
filter:规则名称,必须填写位于filter.d目录里面的规则,sshd是fail2ban内置规则
port:对应的端口
action:采取的行动
logpath:需要监视的日志路径

到这一步,我们jail.local的规则看起来可能像下面这样子:

[DEFAULT]
ignoreip = 127.0.0.1/8
bantime  = 86400
findtime = 600
maxretry = 5
banaction = firewallcmd-ipset
action = %(action_mwl)s

[sshd]
enabled = true
filter  = sshd
port    = 22
action = %(action_mwl)s
logpath = /var/log/secure

上面的配置意思是如果同一个IP,在10分钟内,如果连续超过5次错误,则使用Firewalld将他IP ban了。输入systemctl start fail2ban启动fail2ban来试试效果。

使用另一台服务器不断尝试连接SSH,并且不断的将密码输入错误,你会发现连续超过5次后直接连不上,说明IP被ban了,可以输入:fail2ban-client status sshd查看被ban的IP,如下截图。

未分类

防止CC攻击

这里仅以Nginx为例,使用fail2ban来监视nginx日志,匹配短时间内频繁请求的IP,并使用firewalld将其IP屏蔽,达到CC防护的作用。

#需要先新建一个nginx日志匹配规则
vi /etc/fail2ban/filter.d/nginx-cc.conf
#填写如下内容
[Definition]
failregex =  -.*- .*HTTP/1.* .* .*$
ignoreregex =

继续修改jail.local追加如下内容:

[nginx-cc]
enabled = true
port = http,https
filter = nginx-cc
action = %(action_mwl)s
maxretry = 20
findtime = 60
bantime = 3600
logpath = /usr/local/nginx/logs/access.log

上面的配置意思是如果在60s内,同一IP达到20次请求,则将其IP ban 1小时,上面只是为了测试,请根据自己的实际情况修改。logpath为nginx日志路径。

防止Wordpress爆破

如果您经常分析日志会发现有大量机器人在扫描wordpress登录页面wp-login.php,虽然对方可能没成功,但是为了避免万一还是将他IP干掉为好。

#需要先新建一个nginx日志匹配规则
vi /etc/fail2ban/filter.d/wordpress.conf
#填写如下内容
[Definition]
failregex = ^ -.* /wp-login.php.* HTTP/1.."
ignoreregex =

继续修改jail.local追加如下内容:

[wordpress]
enabled = true
port = http,https
filter = wordpress
action = %(action_mwl)s
maxretry = 20
findtime = 60
bantime = 3600
logpath = /usr/local/nginx/logs/access.log

当然,别忘记输入systemctl restart fail2ban重启fail2ban使其生效。

常用命令

#启动
systemctl start fail2ban
#停止
systemctl stop fail2ban
#开机启动
systemctl enable fail2ban
#查看被ban IP,其中sshd为名称,比如上面的[wordpress]
fail2ban-client status sshd
#删除被ban IP
fail2ban-client set sshd delignoreip 192.168.111.111
#查看日志
tail /var/log/fail2ban.log

总结

fail2ban已经内置很多匹配规则,位于filter.d目录下,包含了常见的SSH/FTP/Nginx/Apache等日志匹配,如果都还无法满足您的需求,您也可以自行新建规则来匹配异常IP。使用fail2ban+Firewalld来阻止恶意IP是行之有效的办法,可极大提高服务器安全。

提示:搬瓦工年付$18可换CN2机房的套餐已补货,7机房随意切换,支持支付宝,优惠码:BWH1ZBPVK,购买: https://bwh1.net/cart.php?a=confproduct&i=0

IPTABLES 和 FIREWALLD 防火墙简单使用介绍

未分类

iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分。可以直接配置,也可以通过许多前端和图形界面配置。

安装IPTable IPTable-service

使用root用户登录,运行以下命令:

#先检查是否安装了iptables
service iptables status
#安装iptables
yum install -y iptables

#升级iptables
yum update iptables

#安装iptables-services
yum install iptables-services

开启IPTables服务

使用root用户登录,运行以下命令:

#开机自启动iptables服务
systemctl enable iptables.service
#启动服务
systemctl start iptables.service

#重启服务
systemctl restart iptables.service

#查看状态
systemctl status iptables.service

禁用/停止自带的firewalld服务

使用root用户登录,运行以下命令:

#停止firewalld服务
systemctl stop firewalld
#禁用firewalld服务
systemctl mask firewalld

设置IPTables现有规则

使用root用户登录,运行以下命令:

#查看iptables现有规则
iptables -L -n
#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT

#清空所有默认规则
iptables -F

#清空所有自定义规则
iptables -X

开放端口

使用root用户登录,运行以下命令:

#开放22端口
iptables -A INPUT -p tcp –dport 22 -j ACCEPT

保存规则设定

使用root用户登录,运行以下命令:

#保存上述规则
service iptables save

centos 7版本以后默认使用firewalld,所以使用firewalld是趋势,要及时跟上脚步。

开启firewalld服务

使用root用户登录,运行以下命令:

#启动
systemctl start firewalld
#查看状态
systemctl status firewalld 或者 firewall-cmd –state

#停止
systemctl disable firewalld

#禁用
systemctl stop firewalld

开放、禁用端口

使用root用户登录,运行以下命令:

#开放端口
firewall-cmd –zone=public –add-port=8080/tcp –permanent

#禁用端口
firewall-cmd –zone=public –remove-port=80/tcp –permanent

设置firewalld

使用root用户登录,运行以下命令:

#查看所有打开的端口
firewall-cmd –zone=public –list-all

#更新防火墙规则
firewall-cmd –reload

firewalld的9个zone、firewalld关于zone和service的操作

firewalld的9个zone

centos7 默认的防火墙工具为firewalld

  • 打开firewalld
[root@localhost ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@localhost ~]# systemctl stop iptables
[root@localhost ~]# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@localhost ~]# systemctl start firewalld

将之前打开的iptables关掉,启动firewalld。可以用iptables查看表的规则。

  • firewalld的9个zone
[root@localhost ~]# firewall-cmd --get-zones     #查看是所有zone
block dmz drop external home internal public trusted work
[root@localhost ~]# firewall-cmd --get-default-zone      #查看默认zone
public

block dmz drop external home internal public trusted work 是firewalld的9个zone,默认的zone为public。

  • firewall的9个zone的区别
    未分类

firewalld关于zone的操作

  • 修改默认的zone
[root@localhost ~]# firewall-cmd --get-default-zone      #查看默认zone
public
[root@localhost ~]# firewall-cmd --set-default-zone=work     #将默认的zone修改为work
success
[root@localhost ~]# firewall-cmd --get-default-zone      #查看默认的zone
work 
  • 查看指定网卡的zone
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33   #查看ens33网卡的zone
work
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens37   #查看ens37网卡的zone
work
[root@localhost ~]# firewall-cmd --get-zone-of-interface=lo  #查看lo的zone
no zone
  • 给指定网卡增加zone
[root@localhost ~]# firewall-cmd --zone=public --add-interface=lo    #给lo网卡指定zone为public
success
[root@localhost ~]# firewall-cmd --get-zone-of-interface=lo  #查看lo网卡的zone
public
  • 修改指定网卡的zone
[root@localhost ~]# firewall-cmd --zone=block --change-interface=ens37   #将网卡ens37的zone改成block
The interface is under control of NetworkManager, setting zone to 'block'.
success
[root@localhost ~]# firewall-cmd     --get-zone-of-interface=ens37
block
  • 删除指定网卡的zone
[root@localhost ~]# firewall-cmd --zone=block --remove-interface=ens37   #删除ens37网卡的block zone
The interface is under control of NetworkManager, setting zone to default.
success
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens37
work

删除指定网卡修改的zone之后,它的zone会变回默认的zone

  • 查看系统中所有网卡所在的zone
[root@localhost ~]# firewall-cmd --get-active-zones  #查看系统中所有网卡所在的zone
work
    interfaces: ens33 ens37
public
    interfaces: lo

firewalld关于service的操作

service 就是zone下面的一个子单元,可以理解成指定的一个端口。

  • 查看所有的servie
[root@localhost ~]# firewall-cmd --get-service
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
  • 查看当前zone下的service
[root@localhost ~]# firewall-cmd --list-service
ssh dhcpv6-client
  • 查看指定zone下的service
[root@localhost ~]# firewall-cmd --zone=public --list-service    #指定查看zone=public 下的service
dhcpv6-client ssh
  • 把服务增加到public的zone下(临时添加)
[root@localhost ~]# firewall-cmd --zone=public --add-service=http    #把http服务临时添加到public的zone下
success
[root@localhost ~]# firewall-cmd --zone=public --list-service
dhcpv6-client ssh http

临时添加的service是在内存中,配置文件中没有,重启之后

  • 把服务增加到public的zone下(永久添加,即更改配置文件)
    zone的配置文件路径/etc/firewalld/zones/
[root@localhost ~]# firewall-cmd --zone=public --add-service=http     --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --list-service
dhcpv6-client ssh http

–permanent 将配置写到配置文件中

[root@localhost ~]# ls /etc/firewalld/zones
public.xml  public.xml.old

每当永久修改完配置文件之后,系统都会将修改之前的配置文件备份一份,后缀名是old。

  • zone的配置文件模板
zone,service配置文件的模板存放路径:
/usr/lib/firewalld/zones
/usr/lib/firewalld/services
  • 删除zone下的某服务
[root@localhost ~]# firewall-cmd --zone=public --remove-service=http     #临时删除
[root@localhost ~]# firewall-cmd --zone=public --remove-service=http --permanent     #永久删除
  • 需求:ftp服务自定义端口1121,需要在work zone下面放行ftp
第一步:复制ftp的配置文件到/etc/firewalld/services/

[root@localhost ~]# cp /usr/lib/firewalld/services/ftp.xml  /etc/firewalld/services/

第二步:编辑该文件,将port="21"改为port="1121"

[root@localhost ~]# vim /etc/firewalld/services/ftp.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
 <short>FTP</short>
 <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
 <port protocol="tcp" port="1121"/>
 <module name="nf_conntrack_ftp"/>
</service>

第三步:复制workzone的配置文件到/etc/firewalld/zones/

[root@localhost ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

第四步:编辑该文件,增加“<service name="ftp"/>”

[root@localhost ~]# vim /etc/firewalld/zones/work.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Work</short>
 <description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
 <service name="ssh"/>
 <service name="dhcpv6-client"/>
 <service name="ftp"/>
</zone>

第六步:重新加载

[root@localhost ~]# firewall-cmd --reload
success

CentOS7防火墙firewalld配置

启动:

systemctl start firewalld

停止:

systemctl stop firewalld

重启:

systemctl restart firewalld.service

查看状态:

systemctl status firewalld

开机启动:

systemctl enable firewalld.service

开机禁用:

systemctl disable firewalld

查看服务是否开机启动:

systemctl is-enabled firewalld.service

查看已启动的服务列表:

systemctl list-unit-files|grep enabled

查看启动失败的服务列表:

systemctl --failed

查看版本:

firewall-cmd --version

查看帮助:

firewall-cmd --help

显示状态:

firewall-cmd --state

查看所有打开的端口:

firewall-cmd --zone=public --list-ports

更新防火墙规则:

firewall-cmd --reload

查看区域信息:

firewall-cmd --get-active-zones

查看指定接口所属区域:

firewall-cmd --get-zone-of-interface=eth0

拒绝所有包:

firewall-cmd --panic-on

取消拒绝状态:

firewall-cmd --panic-off

查看是否拒绝:

firewall-cmd --query-panic

开放80端口:

firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)

查看80端口:

firewall-cmd --zone=public --query-port=80/tcp

删除80端口:

firewall-cmd --zone=public --remove-port=80/tcp --permanent

开放端口区间:

firewall-cmd --permanent --zone=public --add-port=8080-9999/tcp //永久

重新载入使配置立即生效:

firewall-cmd --reload

用活firewalld防火墙中的zone

firewalld中zone的含义学生前面已经给大家介绍过了,说白了一个zone就是一套规则集。可是什么时候该用哪个zone、每个zone中的规则具体是怎么设置呢?下面学生就来给大家详细讲解。

名词解释

在具体介绍zone之前学生先给大家介绍几个相关的名词,因为如果不理解这几个名词zone就无从入手。

  • target:目标,这个前面学生也已经给大家介绍过了,可以理解为默认行为,有四个可选值:default、ACCEPT、%%REJECT%%、DROP,如果不设置默认为default

  • service:这个在前面学生已经给大家解释过了,他表示一个服务

  • port:端口,使用port可以不通过service而直接对端口进行设置

  • interface:接口,可以理解为网卡

  • source:源地址,可以是ip地址也可以是ip地址段

  • icmp-block:icmp报文阻塞,可以按照icmp类型进行设置

  • masquerade:ip地址伪装,也就是按照源网卡地址进行NAT转发

  • forward-port:端口转发

  • rule:自定义规则

哪个zone在起作用

我们知道每个zone就是一套规则集,但是有那么多zone,对于一个具体的请求来说应该使用哪个zone(哪套规则)来处理呢?这个问题至关重要,如果这点不弄明白其他的都是空中楼阁,即使规则设置的再好,不知道怎样用、在哪里用也不行。

对于一个接受到的请求具体使用哪个zone,firewalld是通过三种方法来判断的:

1、source,也就是源地址

2、interface,接收请求的网卡

3、firewalld.conf中配置的默认zone

这三个的优先级按顺序依次降低,也就是说如果按照source可以找到就不会再按interface去查找,如果前两个都找不到才会使用第三个,也就是学生在前面给大家讲过的在firewalld.conf中配置的默认zone。

好了,我们现在知道其原理了,下面学生就给大家介绍每一种方式所对应的配置方法。

配置source

source是在zone的xml文件中配置的,其格式为

<zone>
<source address="address[/mask]"/>
</zone>

只要我们将source节点放入相应的zone配置文件中就可以了,节点的address属性就是源地址,不过我们要注意相同的source节点只可以在一个zone中进行配置,也就是说同一个源地址只能对于一个zone,另外,直接编辑xml文件之后还需要reload才可以起作用,这些学生前面已经给大家讲过,这里就不再重述了。

另外,我们当然也可以使用firewall-cmd命令进行配置,这里主要有五个相关命令(参数)

firewall-cmd [--permanent] [--zone=zone] --list-sources
firewall-cmd [--permanent] [--zone=zone] --query-source=source[/mask]
firewall-cmd [--permanent] [--zone=zone] --add-source=source[/mask]
firewall-cmd [--zone=zone] --change-source=source[/mask]
firewall-cmd [--permanent] [--zone=zone] --remove-source=source[/mask]

我们分别来介绍一下

  • –list-sources:用于列出指定zone的所有绑定的source地址

  • –query-source:用于查询指定zone是否跟指定source地址进行了绑定

  • –add-source:用于将一个source地址绑定到指定的zone(只可绑定一次,第二次绑定到不同的zone会报错)

  • –change-source:用于改变source地址所绑定的zone,如果原来没有绑定则进行绑定,这样就跟–add-source的作用一样了

  • –remove-source:用于删除source地址跟zone的绑定

另外,大家可以看到上面的命令中有两个可选参数:–permanent和–zone,–permanent学生在前面已经给大家介绍过了,表示是否存储到配置文件中(如果存储到配置文件中这不会立即生效),–zone用于指定所要设置的zone,如果不指定则使用默认zone。

我们来看个例子

[[email protected] ~]# firewall-cmd --zone=drop --change-source=1.2.3.4

这样就可以将1.2.3.4绑定到drop这个zone中了,如果没有修改过drop规则的话所有来自1.2.3.4这个ip的连接将会被drop。

至于什么时候使用add什么时候使用change,如果我们就是想将某源地址绑定到指定的zone那么最好使用change,而如果想在源地址没绑定的时候进行绑定,如果已经绑定过则不绑定那么就使用add。

配置interface

interface有两个可以配置的位置:1、zone所对应的xml配置文件2、网卡配置文件(也就是ifcfg-*文件)。

第一种配置跟source大同小异,学生这里就不再细述了,interface在zone配置文件中的节点为

<zone>
<interface name="string"/>
</zone>

相关的firewall-cmd命令为

firewall-cmd [--permanent] [--zone=zone] --list-interfaces
firewall-cmd [--permanent] [--zone=zone] --add-interface=interface
firewall-cmd [--zone=zone] --change-interface=interface
firewall-cmd [--permanent] [--zone=zone] --query-interface=interface
firewall-cmd [--permanent] [--zone=zone] --remove-interface=interface

另外,我们还可以在网卡配置文件中进行配置,比如可以在ifcfg-em1文件中添加下面的配置

ZONE=public

这行配置就相当于下面的命令

[[email protected] ~]# firewall-cmd --zone=public --change-interface=em1

这样配置之后来自em1的连接就会使用public这个zone进行管理(如果source匹配了其他的zone除外)。

配置默认zone

默认zone的配置学生前面已经给大家介绍过了,他是通过firewalld.conf配置文件的DefaultZone配置项来配置的,当然也可以使用firewall-cmd命令来配置

firewall-cmd --set-default-zone=zone

另外还可以通过–get-default-zone来获取默认zone的值。

查看当前起作用的zone

我们可以使用下面的命令来查看当前所有起作用的zone

firewall-cmd --get-active-zones

这个命令会返回所有绑定了source、interface以及默认的zone,并会说明在什么情况下使用。

反向查询

firewalld还给我们提供了反向查询的命令,也就是根据source或者interface查询所对应的zone,其命令如下

firewall-cmd --get-zone-of-interface=interface
firewall-cmd --get-zone-of-source=source[/mask]

有了这两个命令我们就可以检查我们的设置是否正确了。

好了,现在大家就明白了一个接收到的请求具体使用哪个zone了,那么zone具体的规则怎么配置呢?下面学生就来给大家详细介绍。

zone规则配置

target

zone规则中首先最重要的是target的设置,他默认可以取四个值:default、ACCEPT、%%REJECT%%、DROP,其含义很容易理解,这里学生就不介绍了,下面来说怎么配置。

在xml文件中target是zone节点的一个属性,比如drop.xml中为

<zone target="DROP">

block.xml中为

<zone target="%%REJECT%%">

如果使用firewall-cmd命令来操作,命令如下

firewall-cmd --permanent [--zone=zone] --get-target
firewall-cmd --permanent [--zone=zone] --set-target=target

我们要特别注意,这里的–permanent不是可选的,也就是说使用firewall-cmd命令也不可以让他直接生效,也需要reload才可以。

service

service学生在前面也已经给大家介绍过了,他的配置和我们上面所介绍的source基本相同,只不过同一个service可以配置到多个不同的zone中,当然也就不需要–change命令了,他在zone配置文件中的节点为

<zone>
<service name="string"/>
</zone>

相应的配置命令为

firewall-cmd [--permanent] [--zone=zone] --list-services
firewall-cmd [--permanent] [--zone=zone] --add-service=service [--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-service=service
firewall-cmd [--permanent] [--zone=zone] --query-service=service

具体每个命令的含义大家对照上面的source很容易就理解了,不过这里的–add命令中多了一个–timeout选项,学生这里给大家介绍一下。

–add-service中的–timeout的含义是这样的:添加一个服务,但是不是一直生效而是生效一段时间,过期之后自动删除。

这个选项非常有用,比如我们想暂时开放一个端口进行一些特殊的操作(比如远程调试),等处理完成后再关闭,不过有时候我们处理完之后就忘记关闭了,而现在的–timeout选项就可以帮我们很好地解决这个问题,我们在打开的时候就可以直接设置一个时间,到时间之后他自动就可以关闭了。另外,这个参数还有更有用的用法,学生会在下面给大家讲到。当然–timeout和–permanent是不可以一起使用的。

另外,这里我们主要讲的是怎么在zone中使用service,而service自己的配置学生下节再给大家详细介绍。

port

port是直接对端口的操作,他和service非常相似,所以这里也不详细介绍了,port在zone中的配置节点为

<zone>
<port port="portid[-portid]" protocol="tcp|udp"/>
</zone>

相应命令为

firewall-cmd [--permanent] [--zone=zone] --list-ports
firewall-cmd [--permanent] [--zone=zone] --add-port=portid[-portid]/protocol [--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-port=portid[-portid]/protocol
firewall-cmd [--permanent] [--zone=zone] --query-port=portid[-portid]/protocol
icmp-block

icmp-block是按照icmp的类型进行设置阻塞,比如我们不想接受ping报文就可以使用下面的命令来设置

[[email protected] ~]# firewall-cmd --add-icmp-block=echo-request

当然,如果需要长久保存就需要加–permanent选项,不过那样就需要reload才能生效。

icmp-block在zone配置文件中的节点为

<zone>
<icmp-block name="string"/>
</zone>

相应操作命令为

firewall-cmd [--permanent] [--zone=zone] --list-icmp-blocks
firewall-cmd [--permanent] [--zone=zone] --add-icmp-block=icmptype [--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-icmp-block=icmptype
firewall-cmd [--permanent] [--zone=zone] --query-icmp-block=icmptype

masquerade

masquerade大家应该都比较熟悉,其作用就是ip地址伪装,也就是NAT转发中的一种,具体处理方式是将接收到的请求的源地址设置为转发请求网卡的地址,这在路由器等相关设备中非常重要,比如大家很多都使用的是路由器连接的局域网,而想上互联网就得将我们的ip地址给修改一下,要不大家都是192.168.1.XXX的内网地址,那请求怎么能正确返回呢?所以在路由器中将请求实际发送到互联网的时候就会将请求的源地址设置为路由器的外网地址,这样请求就能正确地返回给路由器了,然后路由器再根据记录返回给我们发送请求的主机了,这就是masquerade。

其设置非常简单,在zone中是一个没有参数(属性)的节点

<zone>
<masquerade/>
</zone>

操作命令为

firewall-cmd [--permanent] [--zone=zone] --add-masquerade [--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-masquerade
firewall-cmd [--permanent] [--zone=zone] --query-masquerade

forward-port

这项也非常容易理解,他是进行端口转发的,比如我们要将在80端口接收到tcp请求转发到8080端口可以使用下面的命令

[[email protected] ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080

forward-port还支持范围转发,比如我们还可以将80到85端口的所有请求都转发到8080端口,这时只需要将上面命令中的port修改为80-85即可。

在zone配置文件中节点如下

<zone>
<forward-port port="portid[-portid]" protocol="tcp|udp" [to-port="portid[-portid]"] [to-addr="ipv4address"]/>
</zone>

相关操作命令如下

firewall-cmd [--permanent] [--zone=zone] --list-forward-ports
firewall-cmd [--permanent] [--zone=zone] --add-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]][--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]]
firewall-cmd [--permanent] [--zone=zone] --query-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]]

rule

rule可以用来定义一条复杂的规则,其在zone配置文件中的节点定义如下

<zone>
<rule [family="ipv4|ipv6"]>
[ <source address="address[/mask]" [invert="bool"]/> ]
[ <destination address="address[/mask]" [invert="bool"]/> ]
[
<service name="string"/> |
<port port="portid[-portid]" protocol="tcp|udp"/> |
<protocol value="protocol"/> |
<icmp-block name="icmptype"/> |
<masquerade/> |
<forward-port port="portid[-portid]" protocol="tcp|udp" [to-port="portid[-portid]"] [to-addr="address"]/>
]
[ <log [prefix="prefixtext"] [level="emerg|alert|crit|err|warn|notice|info|debug"]/> [<limit value="rate/duration"/>] </log> ]
[ <audit> [<limit value="rate/duration"/>] </audit> ]
[ <accept/> | <reject [type="rejecttype"]/> | <drop/> ]
</rule>
</zone>

可以看到这里一条rule的配置的配置项非常多,比zone本身还多出了destination、log、audit等配置项。其实这里的rule就相当于使用iptables时的一条规则。rule的操作命令如下

firewall-cmd [--permanent] [--zone=zone] --list-rich-rules
firewall-cmd [--permanent] [--zone=zone] --add-rich-rule='rule' [--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-rich-rule='rule'
firewall-cmd [--permanent] [--zone=zone] --query-rich-rule='rule'

这里的参数’rule’代表一条规则语句,语句结构就是直接按照上面学生给大家的节点结构去掉尖括号来书写就可以了,比如要设置地址为1.2.3.4的source就可以写成source address=”1.2.3.4″,也就是直接写标签名,然后跟着写属性就可以了,我们来看个例子

[[email protected] ~]# firewall-cmd --add-rich-rule='rule family="ipv4" source address="1.2.3.4" drop'

这条规则就会将1.2.3.4这个源地址的连接全部给drop掉。

使用rule结合–timeout我们可以实现一些非常好玩和有用的功能,比如我们可以写个自动化脚本,当发现有异常的连接时就可以添加一条rule将其相应的地址drop掉,而且还可以使用–timeout给设置个时间段,过了之后再自动开放!