Linux 防火墙操作

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 开启防火墙

systemctl start firewalld
或者
systemctl start firewalld.service

3 关闭防火墙

systemctl stop firewalld
或者
systemctl stop firewalld.service

4 开机自动启动防火墙

systemctl enable firewalld
或者
systemctl enable firewalld.service

5 开机关闭防火墙

systemctl disenable firewalld
或者
systemctl disenable firewalld.service

6 重新加载配置

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

7 zone

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

 默认zone

  • 查看默认zone

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

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

 查看zone信息

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

firewall-cmd --get-active-zones

示例:

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

 查看网卡对应zone

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

示例:

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

 将网卡添加至zone中

示例:

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

查看指定zone打开的端口

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

示例:

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

[root@localhost ~]#

8 服务管理

设置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"
    

9 端口

  • 添加端口

    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
    

10 拒绝所有包

紧急情况下才会使用。

  • 查看

    firewall-cmd --query-panic
    
  • 开启

    firewall-cmd --panic-on
    
  • 关闭

    firewall-cmd --panic-off
    
  • firewalld 3.0 之前,使用的是–enable-panic 和 –disable-panic。
posted @ 2022-03-17 19:38  明明改变世界  阅读(297)  评论(0)    收藏  举报