防火墙状态及规则
查看防火墙状态:firewall-cmd --state
1[root@localhost ~]# firewall-cmd --state
2running
查看防火墙:firewall-cmd --list-all
1[root@localhost ~]# firewall-cmd --list-all
2public (active)
3 target: default
4 icmp-block-inversion: no
5 interfaces: eth0
6 sources:
7 services: dhcpv6-client http
8 ports: 999/tcp
9 protocols:
10 masquerade: no
11 forward-ports:
12 source-ports:
13 icmp-blocks:
14 rich rules:
更新防火墙规则:firewall-cmd --reload
1[root@localhost ~]# firewall-cmd --reload
2success
端口
临时增加(reload之后消失):不需要reload
1[root@localhost ~]# firewall-cmd --add-port=999/tcp
2success
3[root@localhost ~]# firewall-cmd --query-port=999/tcp
4yes
5[root@localhost ~]# firewall-cmd --reload
6success
7[root@localhost ~]# firewall-cmd --query-port=999/tcp
8no
永久增加:reload后生效
1[root@localhost ~]# firewall-cmd --permanent --add-port=999/tcp
2success
3[root@localhost ~]# firewall-cmd --reload
4success
5[root@localhost ~]# firewall-cmd --query-port=999/tcp
6yes
临时删除端口:reload后消失
1[root@localhost ~]# firewall-cmd --remove-port=999/tcp
2success
3[root@localhost ~]# firewall-cmd --query-port=999/tcp
4no
5[root@localhost ~]# firewall-cmd --reload
6success
7[root@localhost ~]# firewall-cmd --query-port=999/tcp
8yes
永久删除端口:reload后生效
1[root@localhost ~]# firewall-cmd --permanent --remove-port=999/tcp
2success
3[root@localhost ~]# firewall-cmd --query-port=999/tcp
4yes
5[root@localhost ~]# firewall-cmd --reload
6success
7[root@localhost ~]# firewall-cmd --query-port=999/tcp
8no
查询端口
1[root@localhost ~]# firewall-cmd --query-port=999/tcp
2yes
查看所有放行的端口
1[root@localhost ~]# firewall-cmd --list-ports
2999/tcp
IP
防火墙添加允许访问的IP
注:添加允许访问的IP后,需要reload才能生效
1[root@localhost logs]# firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="10.45.2.35" accept' --permanent
2success
3[root@localhost logs]# firewall-cmd --reload
4success
服务
防火墙服务的状态
1[root@localhost ~]# systemctl status firewalld.service
2● firewalld.service - firewalld - dynamic firewall daemon
3 Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
4 Active: active (running) since 五 2021-10-29 11:09:30 CST; 3 days ago
5 Docs: man:firewalld(1)
6 Main PID: 42215 (firewalld)
7 CGroup: /system.slice/firewalld.service
8 └─42215 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
9
1010月 29 11:09:30 localhost.localdomain systemd[1]: Starting firewalld - dyna...
1110月 29 11:09:30 localhost.localdomain systemd[1]: Started firewalld - dynam...
1211月 02 10:22:44 localhost.localdomain firewalld[42215]: WARNING: ALREADY_EN...
1311月 02 10:23:29 localhost.localdomain firewalld[42215]: WARNING: ALREADY_EN...
14Hint: Some lines were ellipsized, use -l to show in full.
启动/关闭防火墙
1[root@localhost ~]# systemctl stop firewalld.service
2[root@localhost ~]# systemctl start firewalld.service
获取所有支持的服务
1[root@localhost ~]# firewall-cmd --get-services
2RH-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 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
增加服务(临时增加)
1[root@localhost ~]# firewall-cmd --add-service=https
2success
3[root@localhost ~]# firewall-cmd --query-service=https
4yes
5[root@localhost ~]# firewall-cmd --reload
6success
7[root@localhost ~]# firewall-cmd --query-service=https
8no
删除服务(临时删除)
1[root@localhost ~]# firewall-cmd --query-service=https
2yes
3[root@localhost ~]# firewall-cmd --remove-service=https
4success
5[root@localhost ~]# firewall-cmd --query-service=https
6no
7[root@localhost ~]# firewall-cmd --reload
8success
9[root@localhost ~]# firewall-cmd --query-service=https
10no
查询服务
1[root@localhost ~]# firewall-cmd --query-service=https
2yes
增加服务(永久)
1[root@localhost bin]# firewall-cmd --permanent --add-service=http
2success
3[root@localhost bin]# firewall-cmd --reload
4success
5[root@localhost bin]# firewall-cmd --query-service=http
6yes
删除服务(永久)
1[root@localhost bin]# firewall-cmd --permanent --remove-service=ssh
2success
3[root@localhost bin]# firewall-cmd --reload
4success
5[root@localhost bin]# firewall-cmd --query-servicessh
6no
封禁IP
查看所有的规则:
1firewall-cmd --list-all-zones
2# drop: 丢弃所有进入的包,而不给出任何响应
3# block: 拒绝所有外部发起的连接,允许内部发起的连接
4# public: 允许指定的进入连接
5# external: 同上,对伪装的进入连接,一般用于路由转发
6# dmz: 允许受限制的进入连接
7# work: 允许受信任的计算机被限制的进入连接,类似 workgroup
8# home: 同上,类似 homegroup
9# internal: 同上,范围针对所有互联网用户
10# trusted: 信任所有连接
管理端口
1#列出 dmz 级别的被允许的进入端口
2firewall-cmd --zone=dmz --list-ports
3# 允许 tcp 端口 8080 至 dmz 级别
4firewall-cmd --zone=dmz --add-port=8080/tcp
5# 允许某范围的 udp 端口至 public 级别,并永久生效
6firewall-cmd --zone=public --add-port=5060-5059/udp --permanent
网卡接口
1# 列出 public zone 所有网卡
2firewall-cmd --zone=public --list-interfaces
3# 将 eth0 添加至 public zone,永久
4firewall-cmd --zone=public --permanent --add-interface=eth0
5# eth0 存在与 public zone,将该网卡添加至 work zone,并将之从 public zone 中删除
6firewall-cmd --zone=work --permanent --change-interface=eth0
7# 删除 public zone 中的 eth0,永久
8firewall-cmd --zone=public --permanent --remove-interface=eth0
管理服务
1# 添加 smtp 服务至 work zone
2firewall-cmd --zone=work --add-service=smtp
3# 移除 work zone 中的 smtp 服务
4firewall-cmd --zone=work --remove-service=smtp
配置 external zone 中的 ip 地址伪装
1# 查看
2firewall-cmd --zone=external --query-masquerade
3# 打开伪装
4firewall-cmd --zone=external --add-masquerade
5# 关闭伪装
6firewall-cmd --zone=external --remove-masquerade
配置 public zone 的端口转发
1# 要打开端口转发,则需要先
2firewall-cmd --zone=public --add-masquerade
3# 然后转发 tcp 22 端口至 3753
4firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=3753
5# 转发 22 端口数据至另一个 ip 的相同端口上
6firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toaddr=192.168.1.100
7# 转发 22 端口数据至另一 ip 的 2055 端口上
8firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=2055:toaddr=192.168.1.100
配置 public zone 的 icmp
1# 查看所有支持的 icmp 类型
2firewall-cmd --get-icmptypes
3destination-unreachable echo-reply echo-request parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded
4# 列出
5firewall-cmd --zone=public --list-icmp-blocks
6# 添加 echo-request 屏蔽
7firewall-cmd --zone=public --add-icmp-block=echo-request [--timeout=seconds]
8# 移除 echo-reply 屏蔽
9firewall-cmd --zone=public --remove-icmp-block=echo-reply
IP 封禁 (这个是我们平时用得最多的)
1# 单个IP
2firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='222.222.222.222' reject"
3# IP段
4firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='222.222.222.0/24' reject"
5# 单个IP的某个端口
6firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=192.168.1.2 port port=80 protocol=tcp accept"
7# 这个是我们用得最多的,封一个IP和一个PORT。reject:拒绝;accept:允许。当然,我们仍然可以通过 ipset 来封禁 ip
8# 封禁 ip
9firewall-cmd --permanent --zone=public --new-ipset=blacklist --type=hash:ip
10firewall-cmd --permanent --zone=public --ipset=blacklist --add-entry=222.222.222.222
11# 封禁网段
12firewall-cmd --permanent --zone=public --new-ipset=blacklist --type=hash:net
13firewall-cmd --permanent --zone=public --ipset=blacklist --add-entry=222.222.222.0/24
14# 导入 ipset 规则
15firewall-cmd --permanent --zone=public --new-ipset-from-file=/path/blacklist.xml
16# 然后封禁 blacklist
17firewall-cmd --permanent --zone=public --add-rich-rule='rule source ipset=blacklist drop'
IP封禁和端口
1firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=192.168.1.2 port port=80 protocol=tcp accept"
2# 只对192.168.1.2这个IP只能允许80端口访问 (拒绝访问只需把 accept 换成 reject、删除该规则把 –add-rich-rule 改成 –remove-rich-rule即可)
3-----------------------------------------
4firewall-cmd --permanent --add-rich-rule="rule family=ipv4 source address=192.168.1.2/24 port port=80 protocol=tcp accept"
5# 只对192.168.1.2这个IP段只能允许80端口访问(拒绝访问只需把 accept 换成 reject、删除该规则把 –add-rich-rule 改成 –remove-rich-rule即可
双网卡内网网卡不受防火墙限制
1# 公网网卡–zone=public默认区域
2firewall-cmd --permanent --zone=public --add-interface=eth1
3
4# 内网网卡–zone=trusted是受信任区域 可接受所有的网络连接
5firewall-cmd --permanent --zone=trusted --add-interface=eth2
重新载入以生效
1firewall-cmd --reload