拥抱firewalld,但也别忘了iptables——下篇(firewalld详解)

CentOS7(RHEL7)开始,官方的标准防火墙设置软件从iptables变更为firewalld,相信不少习惯使用iptables的人会感到十分不习惯,但实际上firewalld更为简单易用。
大致用法就是:把可信任的IP地址添加到trusted区域,把不可信任的IP地址添加到block区域,把要公开的网络服务添加到public区域。

配置方式

firewalld的配置文件一般存放在下面两个目录:

  1. /etc/firewalld/
  2. /usr/lib/firewalld/

当需要一个文件时firewalld会优先使用第一个目录的。

这两个配置目录的结构很简单,主要是两个文件和三个目录:

  1. 文件:
    • firewalld.conf:主配置文件(只有5个配置项)
      • DefaultZone:默认使用的zone;
      • MinimalMark:使用标记的最小值;
      • CleanuupOnExit:退出后是否清除防火墙规则;
      • Lockdown:是否限制别的程序通过D-BUS接口直接操作firewalld
      • IPv6_rpfilter:判断所接受到的包是否是伪造的
    • lockdown-whitelist.xml:当Lockdown设置为yes时,规定哪些程序可以对firewalld进行操作
    • direct.xml:直接使用类似iptables的语法来进行规则的增删
  2. 目录:
    • zones:保存zone配置文件
    • services:保存service配置文件
    • icmptypes:保存和icmp类型相关的配置文件

什么是区域zone

所谓的区域就是一个信赖等级,某一等级下对应有一套规则集。划分方法包括:网络接口、IP地址、端口号等等。一般情况下,会有如下的这些默认区域:

  1. drop:丢弃所有进入的数据包
  2. block:拒绝所有进入的数据包
  3. public:只接受部分选定的数据包
  4. external:应用在NAT设定时的对外网络
  5. dmz:非军事区
  6. work:使用在公司环境
  7. home:使用在家庭环境
  8. internal:应用在NAT设定时的对内网络
  9. trusted:接受所有的数据包

比如,public区域由public.xml文件来配置:

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not 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"/>
</zone>

当然,你也可以定义自己的区域,只要在zones目录下新建一个同名的xml文件即可。

什么是服务service

iptables时代习惯使用端口号来匹配规则,但是如果某一个服务的端口号改变了,那就要同时更改iptables的规则,十分不方便,同时也不方便阅读理解。

service配置文件的命名为<服务名>.xml,比如ssh的配置文件是ssh.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>

配置实例

1.开启firewalld

# systemctl enable firewalld
# systemctl start firewalld

2.往trusted区域中注册IP地址

# firewall-cmd --zone=trusted --add-source=xxx.xxx.xxx.xxx

3.往public区域中注册网络接口

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

4.往public区域中注册新服务

# firewall-cmd --zone=public --add-service=http
# firewall-cmd --zone=public --add-service=https

5.删除public区域中不需要的服务

# firewall-cmd --zone=public --remove-service=dhcpv6-client
# firewall-cmd --zone=public --remove-service=ssh

6.查看各个区域的配置情况

# firewall-cmd --get-active-zones
public
  interfaces: eth0
trusted
  sources: xxx.xxx.xxx.xxx

7.查看某个区域的服务

# firewall-cmd --zone=public --list-services
# firewall-cmd --zone=trusted --list-services
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值