Linux(CentOS / RHEL 7) 防火墙

CentOS / RHEL 7 防火墙

CentOS / RHEL 7 防火墙

1 简述

CentOS 7 与 CentOS 6 对于系统功能的管理与维护方式上发生了巨大的变化,当然CentOS 7 仍兼容CentOS 6中的管理方式。

CentOS 7 中使用的是 systemctl 命令来管理与维护系统功能。而CentOS 6 及之前的版本 中使用的是service命令。

CentOS 7 中防火墙的名字是firewalld[.service] . 防火墙本身也提供了管理命令: firewall-cmd. firewalld 是 iptables的前端控制器, 是 iptables 的一个封装,可以让你更容易地管理 iptables 规则,虽然 iptables 命令仍可用于 FirewallD,但建议使用 FirewallD 时仅 使用 FirewallD 命令。

与iptables相比,区别有以下两点:

  • FirewallD 使用区域和服务而不是链式规则。
  • 它动态管理规则集,允许更新规则而不破坏现有会话和连接。

根据RHEL 官网中的描述,防火墙按照网络的可信任度,分为了几个级别,根据不同的信任级别对 数据包选择不同的处理方式.

firewalld网络分类(zone)
  • drop 丢弃所有进入的包,而不给出任何响应
  • block 拒绝所有外部发起的连接,允许内部发起的连接
  • public 允许指定的进入连接
  • external 同上,对伪装的进入连接,一般用于路由转发
  • dmz 允许受限制的进入连接
  • work/home 允许受信任的计算机被限制的进入连接,类似于工作组
  • internal 同上,范围针对所有互联网用户
  • trusted 信任所有连接

2 常用基本操作

 

2.1 查看防火墙状态

firewall-cmd --state
或者
systemctl status firewalld

示例:

[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@localhost ~]# firewall-cmd --state
not running

2.2 开启防火墙

systemctl start firewalld
或者
systemctl start firewalld.service

2.3 关闭防火墙

systemctl stop firewalld
或者
systemctl stop firewalld.service

2.4 开机自动启动防火墙

systemctl enable firewalld
或者
systemctl enable firewalld.service

2.5 开机关闭防火墙

systemctl disenable firewalld
或者
systemctl disenable firewalld.service

3 firewalld配置

关于firewalld 命令的使用非常简单,使用/firewall-cmd –help/ 查看帮助文档。 下面只是列出其中几个常用的功能。

3.1 重新加载配置

firewall-cmd --reload # 更新规则,不重启服务
firewall-cmd --complete-reload # 更新规则,重启服务

3.2 zone

zone的配置文件位于/etc/firewalld/zones中,默认只有public类的配置文件。

3.2.1 默认zone

  • 查看默认zone

    firewall-cmd --get-default-zone
    
  • 设置默认zone

    firewall-cmd --set-default-zone=<zone_name>
    

3.2.2 查看zone信息

zone信息中包含了对应的网卡.

firewall-cmd --get-active-zones

示例:

[root@localhost ~]# firewall-cmd --get-active-zones
public
  interfaces: eth0 eth1

3.2.3 查看网卡对应zone

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

示例:

[root@localhost ~]# firewall-cmd --get-zone-of-interface=eth0
public

3.2.4 将网卡添加至zone中

示例:

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

3.2.5 查看指定zone打开的端口

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

示例:

[root@localhost ~]# firewall-cmd --zone=public --list-ports

[root@localhost ~]#

3.3 服务管理

设置zone的服务时,所做的修改最终会保存到/etc/firewalld/zones/<zone_name>.xml 所以,我们可以直接修改zone的配置文件,然后再执行 firewall-cmd –reload 重新加载

  • 查看支持的服务

    firewall-cmd --get-service
    

    结果中,不同的服务之间使用空格分隔。实际上,这些服务的名字来源于/usr/lib/firewalld/services/ 和/etc/firewalld/services,firewalld 先在后者中查找服务,然后再在前者中查找服务。 服务的配置文件保存为xml格式。如下:

    [root@localhost ~]# ls /usr/lib/firewalld/services/
    amanda-client.xml        dhcpv6-client.xml        ganglia-master.xml     jenkins.xml      minidlna.xml   ovirt-imageio.xml         puppetmaster.xml     smtps.xml              tftp.xml
    amanda-k5-client.xml     dhcpv6.xml               git.xml                kadmin.xml       mongodb.xml    ovirt-storageconsole.xml  quassel.xml          smtp.xml               tinc.xml
    bacula-client.xml        dhcp.xml                 gre.xml                kerberos.xml     mosh.xml       ovirt-vmconsole.xml       radius.xml           snmptrap.xml           tor-socks.xml
    bacula.xml               dns.xml                  high-availability.xml  kibana.xml       mountd.xml     pmcd.xml                  redis.xml            snmp.xml               transmission-client.xml
    bgp.xml                  docker-registry.xml      https.xml              klogin.xml       mssql.xml      pmproxy.xml               RH-Satellite-6.xml   spideroak-lansync.xml  upnp-client.xml
    bitcoin-rpc.xml          docker-swarm.xml         http.xml               kpasswd.xml      ms-wbt.xml     pmwebapis.xml             rpc-bind.xml         squid.xml              vdsm.xml
    bitcoin-testnet-rpc.xml  dropbox-lansync.xml      imaps.xml              kprop.xml        murmur.xml     pmwebapi.xml              rsh.xml              ssh.xml                vnc-server.xml
    bitcoin-testnet.xml      elasticsearch.xml        imap.xml               kshell.xml       mysql.xml      pop3s.xml                 rsyncd.xml           syncthing-gui.xml      wbem-https.xml
    bitcoin.xml              freeipa-ldaps.xml        ipp-client.xml         ldaps.xml        nfs3.xml       pop3.xml                  samba-client.xml     syncthing.xml          xmpp-bosh.xml
    ceph-mon.xml             freeipa-ldap.xml         ipp.xml                ldap.xml         nfs.xml        postgresql.xml            samba.xml            synergy.xml            xmpp-client.xml
    ceph.xml                 freeipa-replication.xml  ipsec.xml              libvirt-tls.xml  nmea-0183.xml  privoxy.xml               sane.xml             syslog-tls.xml         xmpp-local.xml
    cfengine.xml             freeipa-trust.xml        ircs.xml               libvirt.xml      nrpe.xml       proxy-dhcp.xml            sips.xml             syslog.xml             xmpp-server.xml
    condor-collector.xml     ftp.xml                  irc.xml                managesieve.xml  ntp.xml        ptp.xml                   sip.xml              telnet.xml             zabbix-agent.xml
    ctdb.xml                 ganglia-client.xml       iscsi-target.xml       mdns.xml         openvpn.xml    pulseaudio.xml            smtp-submission.xml  tftp-client.xml        zabbix-server.xml
    

    此时,如果我们在这个路径里添加一个xml文件,再执行/firewall-cmd –get-service/看看,

    [root@localhost services]# cd /usr/lib/firewalld/services/
    [root@localhost services]# cp ftp.xml ftp1.xml
    [root@localhost services]# systemctl stop firewalld
    [root@localhost services]# systemctl start firewalld
    [root@localhost services]# firewall-cmd --get-service
    RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ftp1 ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server
    

    从结果中看, 我们添加了一个ftp1.xml文件,重启firewalld服务后,该文件被识为一个服务。 因此, 如果我们可以在这个路径中添加firewalld支持的服务。

    修改服务配置后,使用 firewall-cmd –reload 重新加载。

  • 启用某个服务

    firewall-cmd --zone=public --add-service=ftp //临时
    firewall-cmd --permanent --zone=public --add-service=ftp //永久
    
  • 查看开启的服务

    firewall-cmd --permanent --zone=public --list-services
    
  • 设置IP访问服务

    firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="10.235.0.10/24" service name="http" accept"
    

3.4 端口

  • 添加端口

    firewall-cmd --permanent --zone=public --add-port=8080-8081/tcp //永久
    firewall-cmd --zone=public --add-port=8080-8081/tcp //临时
    
  • 删除端口

    firewall-cmd --zone=public --remove-port=80/tcp --permanent
    
  • 查看开启的端口

    firewall-cmd --permanent --zone=public --list-ports
    
  • IP伪装 端口转发需要先开启masquerade,可以理解成ip伪装,一般情况下,也就是用于端口转发. 下面是该功能的的基本用法:

    # 查看当前是否开启IP伪装功能
    firewall-cmd --zone=public --query-masquerade
    #打开伪装
    firewall-cmd --zone=public --add-masquerade
    #关闭伪装
    firewall-cmd --zone=public --remove-masquerade
    

    示例如下:

    [root@localhost zones]# firewall-cmd --get-default-zone
    public
    [root@localhost zones]# firewall-cmd --zone=public --query-masquerade
    no
    [root@localhost zones]# firewall-cmd --zone=public --add-masquerade
    success
    [root@localhost zones]# firewall-cmd --zone=public --query-masquerade
    yes
    [root@localhost zones]# firewall-cmd --zone=public --remove-masquerade
    success
    
  • 端口转发 在进行端口转发前,需要先开启IP伪装功能。然后再配置转发。

    # 开启端口转发
    firewall-cmd --zone=external --add-masquerade
    
    # 转发 tcp 22 端口至 8181端口
    
    firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=8181
    # 转发 22 端口数据至另一个ip的相同端口上
    firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toaddr=10.10.100.10
    
    # 转发 22 端口数据至另一ip的1919端口上
    firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=1919:toaddr=10.10.100.10
    

3.5 拒绝所有包

紧急情况下才会使用。

  • 查看

    firewall-cmd --query-panic
    
  • 开启

    firewall-cmd --panic-on
    
  • 关闭

    firewall-cmd --panic-off
    
  • firewalld 3.0 之前,使用的是–enable-panic 和 –disable-panic。

Author: halberd.lee

Created: 2019-08-22 Thu 22:33

Validate

猜你喜欢

转载自www.cnblogs.com/halberd-lee/p/11397242.html