CentOS7 系统防火墙firewalld工具手册

CentOS7 系统防火墙firewalld工具手册

96 国猫 关注

2017.03.06 17:41* 字数 2388 阅读 1228评论 1喜欢 8

0、前言

0.1 firewalld的动态防火墙

Firewalld提供了一个动态管理的防火墙,支持网络/区域来定义网络连接或接口的信任级别。它支持IPv4、IPv6防火墙设置和以太网网桥,并且具有运行时和永久配置选项的分离。它还支持服务或应用程序直接添加防火墙规则的接口。

旧的防火墙使用system-config-防火墙 / lokkit模型是静态的,每次更改都需要完全重新启动防火墙。这还包括卸载防火墙netfilter内核模块,并加载新配置所需的模块。模块的卸载破坏了状态防火墙和建立的连接。

防火墙守护程序在另外的层面动态管理防火墙并应用更改,而不需要重新启动整个防火墙。因此,不需要重新加载所有防火墙内核模块。但是使用防火墙守护程序需要使用该守护程序进行所有防火墙修改,以确保守护程序中的状态和内核中的防火墙保持同步。防火墙守护程序无法解析由 ip*tables 和 ebtables 命令行工具添加的防火墙规则。

守护程序通过D-BUS提供有关当前活动防火墙设置的信息,并通过使用PolicyKit身份验证方法的D-BUS接受更改。

Firewalld 的官方主页是 (www.firewalld.org)[www.firewalld.org]

0.2 守护进程

应用程序,守护程序和用户可以请求通过D-BUS启用防火墙功能。 功能可以是预定义的防火墙功能之一,如服务,端口和协议组合,端口/数据包转发,伪装或icmp阻止。 该功能可以启用一段时间,或者可以再次禁用。

使用所谓的直接接口,其他服务(例如libvirt)能够使用iptables参数和参数添加自己的规则。

netfilter防火墙帮助程序,例如用于amanda,ftp,samba和tftp服务,也由守护程序处理,只要它们是预定义服务的一部分。 加载附加助手不是当前界面的一部分。 对于某些帮助器,只有在模块处理的所有连接都关闭后才能卸载。 因此,连接跟踪信息在这里很重要,需要考虑。

0.3 静态防火墙(system-config-firewall/lokkit)

使用 system-config-firewall 和 lokkit 的静态防火墙模型实际上仍然可用并将继续提供,但却不能与“守护进程”同时使用。用户或者管理员可以决定使用哪一种方案。
在软件安装,初次启动或者是首次联网时,将会出现一个选择器。通过它你可以选择要使用的防火墙方案。其他的解决方案将保持完整,可以通过更换模式启用。
firewall daemon 独立于 system-config-firewall,但二者不能同时使用。


1. firewalld常用命令

  • 启动firewalld
    systemctl start firewalld

  • 停止firewalld
    systemctl stop firewalld

  • 设置firewalld开机启动
    sysemctl enable firewalld

  • 设置firewalld开机不启动
    systemctl disable firewalld

  • 查看firewalld当前状态
    firewall-cmd --list-all

  • 重新读取配置文件
    firewall--cmd --reload

  • 临时关掉一个已开放的服务端口,在public区域下(如关闭dncpv6-client) (即时生效)
    firewall-cmd --zone=public --remove-service dhcpv6-client

  • 永久关闭一个已开放的服务端口public区域下(必须reload或者重启服务、重启系统才会生效)(区别在于--permanent)
    firewall-cmd --zone=public --remove-service dhcpv6-client --permanent

  • 临时启用一个服务规则(如启用dns)(即时生效)

> 前提,必须存在相关配置文件/usr/lib/firewalld/services/dns.xml
firewall-cmd --add-service=dns

  • 永久开启一个已开放的服务端口public区域下(必须reload或者重启服务、重启系统才会生效)
    firewall-cmd --add-service=dns --permanent

2. 区域

所谓的 zone 就表示主机位于何种环境,需要设定哪些规则,在 firewalld 里共有 7 個zones
修改区域
vi /etc/firewalld/firewalld.conf 修改 DefaultZone=public

先決定主機要設定在哪个区域 zone >> 再进入相关 zone 配置文件设定规则 >>> 重新加载设定档生效配置 sudo firewall-cmd --reload

  • public: 公开的场所,不信任网域内所有访问请求,只有被允许的访问请求才能进入,一般比较常用这种zone。

For use in public areas. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.

  • external: 公开的场所,应用在IP是NAT的网络。在外部网络上使用,特别是为路由器启用伪装。不信任网络上的其他计算机。只接受选定的传入连接。

For use on external networks with masquerading enabled especially for routers. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.

  • dmz: (Demilitarized Zone) 非军事区,对于您的非军事区域中的计算机,可以通过有限访问您的内部网络进行公开访问。只接受选定的传入连接。

For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.

  • work: 用于工作区域。你大多信任网络上的其他计算机不会伤害你的计算机。只接受选定的传入连接。

For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.

  • home: 用于家庭环境。你大多信任网络上的其他计算机不会伤害你的计算机。只接受选定的传入连接。

For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.

  • internal: 内部网路,应用在NAT设定时的对内网络。你大多信任网络上的其他计算机不会伤害你的计算机。只接受选定的传入连接。

For use on internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted.

  • trusted: 接受所有的连接。

All network connections are accepted.

  • drop: 任何进入的封包全部丢弃,只有往外的连线是允许的。

Any incoming network packets are dropped, there is no reply. Only outgoing network connections are possible.

  • block: 任何进入的封包全部拒绝,并以icmp回覆对方,只有往外的连线是允许的

Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated from within the system are possible.


3. 配置文件

3.1. 配置文件路径(重启后生效)

  • 主配置目录,被用于用户创建和自定义配置文件
    /etc/firewalld/
  • 主配置文件,被用于用户创建和自定义配置文件
    /etc/firewalld/firewalld.conf
  • 服务类型配置文件目录,被用于用户创建和自定义配置文件
    /etc/firewalld/services/
  • 区域配置目录,被用于用户创建和自定义配置文件
    /etc/firewalld/zone/
  • 预设参考配置路径(xml格式),子目录与主配置目录结构相同,被用于默认和备用配置(配置文件库)
    /usr/lib/firewalld/

3.2. 配置实例

3.2.1.例1 修改预设的zone 改为dmz
  • 从配置库中拷贝一份dmz配置文件至系统配置内
    cp /usr/lib/firewalld/zones/dmz.xml /etc/firewalld/zones/
  • 修改firewalld.conf配置文件
    vi /etc/firewalld/firewalld.conf
    DefaultZone=dmz
    #将等号右边的部分修改为你想要使用的zone
  • 重新载入配置,生效配置
    firewall-cmd --complete-reload
3.2.2.例2 开放sshd远程端口(默认22口)
  • 修改zone配置(假设这里使用的zone是public)
    vi /etc/firewalld/zones/public.xml
    在配置文件中加入 一行:<service name="ssh"/>
    如下:

<?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"/>
</zone>

  • 重新载入配置,配置生效
    firewall-cmd --complete-reload
    备注:默认firewalld会优先读取/etc/firewalld/services/ssh.xml配置文件,当该配置文件不存在时,将会读取配置库中的配置/lib/firewalld/services/ssh.xml作为生效配置。
3.2.3.例3 开放自己定义的服务与端口
  • 从配置库中拷贝一份service配置文件至系统配置内
    cp /lib/firewalld/services/snmp.xml /etc/firewalld/services/
  • 将配置文件名称修改为自己需要的服务名(假设需要开放shadowsocks)
    cd /etc/firewalld/services/
    mv snmp.xml shadowsocks.xml
  • 修改service配置内容
    vi shadowsocks.xml
    • 将服务名称改为我要开放的服务:
      <short>SNMP</short>
      改为:
      <short>shadowsocks</short>
    • 将端口改为我需要的端口:
      <port protocol="tcp" port="161”/>
      改为:
      <port protocol="tcp" port="443"/>
    • 删除无用的端口:
      删除
      <port protocol="udp" port="161"/>
  • 修改zone配置(假设这里使用的zone是public)
    vi /etc/firewalld/zones/public.xml
    在配置文件中加入 一行:<service name="shadowsocks"/>
    如下:

<?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="shadowsocks"/>
</zone>

  • 重新载入配置,配置生效
    firewall-cmd --complete-reload

转载于:https://my.oschina.net/u/3367404/blog/3006258

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值