Firewalld基本使用

snow chuai汇总、整理、撰写—2020/2/28


1. 开启防火墙及基本操作
1) 开启防火墙
[root@srv1 ~]# systemctl enable --now firewalld
[root@srv1 ~]# systemctl is-active firewalld
active
2) 显示默认zone [root@srv1 ~]# firewall-cmd --get-default-zone public
3) 显示默认zone的防火墙规则 [root@srv1 ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
4) 显示所有zone的防火墙规则 [root@srv1 ~]# firewall-cmd --list-all-zones block target: %%REJECT%% icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
dmz target: default icmp-block-inversion: no interfaces: sources: services: ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
drop target: DROP icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
external target: default icmp-block-inversion: no interfaces: sources: services: ssh ports: protocols: masquerade: yes forward-ports: source-ports: icmp-blocks: rich rules:
home target: default icmp-block-inversion: no interfaces: sources: services: dhcpv6-client mdns samba-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
internal target: default icmp-block-inversion: no interfaces: sources: services: dhcpv6-client mdns samba-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
trusted target: ACCEPT icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
work target: default icmp-block-inversion: no interfaces: sources: services: dhcpv6-client ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
5) 显示指定zone的防火墙规则 [root@srv1 ~]# firewall-cmd --list-service --zone=external ssh
6) 更改默认zone [root@srv1 ~]# firewall-cmd --set-default-zone=external success
7) 更改网络接口到指定zone-临时 [root@srv1 ~]# firewall-cmd --change-interface=eth1 --zone=external success [root@srv1 ~]# firewall-cmd --list-all --zone=external external (active) interfaces: eth1 sources: services: ssh ports: masquerade: yes forward-ports: icmp-blocks: rich rules:
8) 更改网络接口到指定zone-永久 [root@srv1 ~]# nmcli c mod eth1 connection.zone external [root@srv1 ~]# firewall-cmd --get-active-zone external interfaces: eth1 public interfaces: eth0
9) 显示Firewalld默认定义的服务 [root@srv1 ~]# firewall-cmd --get-services RH-Satellite-6 amanda-client amanda-k5-client amqp amqps apcupsd audit bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client distcc dns docker-registry docker-swarm dropbox-lansync elasticsearch etcd-client etcd-server finger freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target isns jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls lightning-network llmnr managesieve matrix mdns minidlna mongodb mosh mountd mqtt mqtt-tls ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp nut openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole plex pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd rtsp salt-master samba samba-client samba-dc sane sip sips slp smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh steam-streaming svdrp svn syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-http wbem-https wsman wsmans xdmcp xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server
10) Firewalld定义的服务所在目录 [root@srv1 ~]# ls /usr/lib/firewalld/services amanda-client.xml freeipa-ldap.xml libvirt.xml pop3s.xml ssh.xml amanda-k5-client.xml freeipa-replication.xml lightning-network.xml pop3.xml steam-streaming.xml amqps.xml freeipa-trust.xml llmnr.xml postgresql.xml svdrp.xml amqp.xml ftp.xml managesieve.xml privoxy.xml svn.xml apcupsd.xml ganglia-client.xml matrix.xml proxy-dhcp.xml ...... ......
2. 添加或移除指定的规则
2.1 添加或移除指定的服务规则
1) 添加http服务,允许通过防火墙--临时
[root@srv1 ~]# firewall-cmd --add-service=http
success
[root@srv1 ~]# firewall-cmd --list-service
dhcpv6-client http ssh
2) 删除http服务,不允许通过防火墙--临时 [root@srv1 ~]# firewall-cmd --remove-service=http success [root@srv1 ~]# firewall-cmd --list-service dhcpv6-client ssh
3) 添加http服务,允许通过防火墙--永久 [root@srv1 ~]# firewall-cmd --add-service=http --permanent success [root@srv1 ~]# firewall-cmd --reload success [root@srv1 ~]# firewall-cmd --list-service dhcpv6-client http ssh<
2.2 添加或移除指定的端口规则
1) 添加465端口,允许通过防火墙--临时
[root@srv1 ~]# firewall-cmd --add-port=465/tcp
success
[root@srv1 ~]# firewall-cmd --list-port
465/tcp
2) 移除465端口--临时 [root@srv1 ~]# firewall-cmd --remove-port=465/tcp success [root@srv1 ~]# firewall-cmd --list-port
3) 添加永久465端口--临时 [root@srv1 ~]# firewall-cmd --add-port=465/tcp --permanent success [root@srv1 ~]# firewall-cmd --reload success [root@srv1 ~]# firewall-cmd --list-port 465/tcp
2.3 添加或移除ICMP相关规则
1) 添加echo-request类型,拒绝通过防火墙--临时
[root@srv1 ~]# firewall-cmd --add-icmp-block=echo-request
success
[root@srv1 ~]# firewall-cmd --list-icmp-blocks
echo-request
2) 移除echo-request类型--临时 [root@srv1 ~]# firewall-cmd --remove-icmp-block=echo-request success [root@srv1 ~]# firewall-cmd --list-icmp-blocks
3) 显示ICMP相关协议类型 [root@srv1 ~]# firewall-cmd --get-icmptypes destination-unreachable echo-reply echo-request parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded
3. IP Masquerade
3.1 拓扑
          ---------+---------
            Gateway|172.16.0.1
                   |
      External     |
               eth1|172.16.10.1
          +--------+---------+
          |                  |
          | srv1.1000cc.net  |
          |                  |
          +--------+---------+
               eth0|192.168.10.1
      Internal     |
3.2 配置IP Masquerade
1) 查看当前配置
[root@srv1 ~]# firewall-cmd --get-active-zone
public
  interfaces: eth0 eth1
2) 更改接口所在区域 [root@srv1 ~]# nmcli c mod eth0 connection.zone internal [root@srv1 ~]# nmcli c mod eth1 connection.zone external
[root@srv1 ~]# firewall-cmd --get-active-zone internal interfaces: eth0 external interfaces: eth1
3) 实现IP Masquerde [root@srv1 ~]# firewall-cmd --zone=external --add-masquerade --permanent success [root@srv1 ~]# firewall-cmd --reload success
[root@srv1 ~]# firewall-cmd --zone=external --query-masquerade yes
[root@srv1 ~]# cat /proc/sys/net/ipv4/ip_forward 1
3.3 添加服务转发
1) 指定转发至本地端口
# 访问本地的80端口,就将请求转到本地的8222端口
[root@srv1 ~]# firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toport=8222
success
[root@srv1 ~]# firewall-cmd --list-all --zone=external external (active) interfaces: eth1 sources: services: ssh ports: masquerade: yes forward-ports: port=80:proto=tcp:toport=8222:toaddr= icmp-blocks: rich rules:
2) 指定转发至指定主机的指定端口 # 访问本地的80端口,就将请求转到192.168.10.222的8222端口 [root@srv1 ~]# firewall-cmd --zone=external \ --add-forward-port=port=80:proto=tcp:toport=8222:toaddr=192.168.10.222
success
[root@srv1 ~]# firewall-cmd --list-all --zone=external external (active) interfaces: eth1 sources: services: ssh ports: masquerade: yes forward-ports: port=80:proto=tcp:toport=8222:toaddr=192.168.10.222 icmp-blocks: rich rules:
3.4 DMZ--允许服务器从Intranl将数据转发到External[添加富规则]
[root@srv1 ~]# firewall-cmd --zone=internal --add-masquerade --permanent
success
[root@srv1 ~]# firewall-cmd --reload
success
[root@srv1 ~]# firewall-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -o eth1 -j MASQUERADE [root@srv1 ~]# firewall-cmd --direct --add-rule ipv4 filter FORWARD 0 -i eth0 -o eth1 -j ACCEPT [root@srv1 ~]# firewall-cmd --direct --add-rule ipv4 filter FORWARD 0 -i eth1 -o eth0 -m state \ --state RELATED,ESTABLISHED -j ACCEPT

 

如对您有帮助,请随缘打个赏。^-^

gold