Centos7 firewalld防火墙基本操作

前言

在centos6的版本中,防火墙使用的iptables,iptables是一个静态防火墙,就是说它不能够动态的添加开启端口,必须在配置文件中添加开启端口,然后重启iptables后才能生效。而centos7的防火墙使用的是firewalld,它是动态的,可以通过命令添加开启端口,不用重启服务就可以使改变生效。

防火墙管理

systemctl enable firewalld       #设置开机启动
systemctl start firewalld        #开启服务
systemctl status firewalld 或者 firewall-cmd --state    #查看状态
systemctl disable firewalld    #停止
systemctl stop firewalld       #禁用

区域管理

通过将网络划分成不同的区域,制定出不同区域之间的访问控制策略来控制不同程序区域间传送的数据流。例如,互联网是不可信任的区域,而内部网络是高度信任的区域。网络安全模型可以在安装,初次启动和首次建立网络连接时选择初始化。该模型描述了主机所连接的整个网络环境的可信级别,并定义了新连接的处理方式。有如下几种不同的初始化区域:
除了trusted区域以外,所有区域默认都是拒绝所有。

  • 阻塞区域(block):任何传入的网络数据包都将被阻止。(默认拒绝所有的连接)
  • 工作区域(work):相信网络上的其他计算机,不会损害你的计算机。
  • 家庭区域(home):相信网络上的其他计算机,不会损害你的计算机。
  • 公共区域(public):不相信网络上的任何计算机,只有选择接受传入的网络连接。(默认区域)
  • 隔离区域(DMZ):隔离区域也称为非军事区域,内外网络之间增加的一层网络,起到缓冲作用。对于隔离区域,只有选择接受传入的网络连接。
  • 信任区域(trusted):所有的网络连接都可以接受。(默认接受所有的数据包)
  • 丢弃区域(drop):任何传入的网络连接都被拒绝。(默认丢弃所有收到的数据包)
  • 内部区域(internal):信任网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。
  • 外部区域(external):不相信网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。
  • 常用操作

    注:规则后没有加--zone=区域的时候,默认添加到默认区域
    注:permanent是永久生效,reload之后规则还存在;如果不加,reload之后规则失效。
    1.查看所有区域和规则

    [root@lianst ~]# firewall-cmd --list-all-zones
    work
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
    .....<其他省略>
    

    2.查看所有区域

    [root@lianst ~]# firewall-cmd --get-zones
    work drop internal external trusted home dmz public block
    

    3.查看所有生效区域

    [root@lianst ~]# firewall-cmd --get-active-zones
    public
      interfaces: venet0
    

    4.从200.1.1.0/24网段收到的数据进入work区域进行检查(permant是永久写入到配置文件中的意思)

    [root@lianst ~]# firewall-cmd --permanent --add-source=200.1.1.0/24 --zone=work 
    

    5.重新加载firewalld配置文件

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

    6.从eth0接收到的数据进入dmz区域接受检查

    [root@lianst ~]# firewall-cmd --permanent --add-interface=eth0 --zone=dmz
    

    7.把trusted区域设置为默认区域

    [root@lianst ~]# firewall-cmd --set-default-zone=trusted
    

    8.向work区域添加允许对http服务访问

    [root@lianst ~]# firewall-cmd --permanent --add-service=http --zone=work
    

    9.向work区域添加允许对tcp 3260端口访问的规则

    [root@lianst ~]# firewall-cmd --permanent --add-port=3260/tcp --zone=work
    

    10.向默认区域加入规则,访问8080端口时自动转换为80端口

    [root@lianst ~]# firewall-cmd --permant --add-forwardpord=port=8080:proto=tcp:toport=80 
    

    11.允许ssh服务通过防火墙

    [root@lianst ~]# firewall-cmd --enable service=ssh  (--disable:禁止)
    

    12.阻止111.73.157.199访问

    [root@niaoyun software]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="111.73.157.199" drop'
    

    直接模式

    FirewallD包括一种直接模式,使用它可以完成一些工作,例如打开TCP协议的443端口

    [root@lianst ~]# firewall-cmd --direct -add-rule ipv4 filter INPUT 0 -p tcp --dport 443 -j ACCEPT
    [root@lianst ~]# firewall-cmd --reload
    

    其他命令

    1.查看服务列表

    [root@lianst ~]# firewall-cmd --get-services
    RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap 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
    

    2.临时允许samba服务通过600秒

    [root@lianst ~]# firewall-cmd --enable service=samba --timeout=600
    

    3.从区域中将接口删除

    [root@lianst ~]# firewall-cmd trusted --remove-interface=eht0
    

    4.从区域中删除服务

    [root@lianst ~]# firewall-cmd --zone=public --remove-service=http