Linux防火墙firewall命令

发布时间 2023-10-13 16:47:10作者: theSummerDay

systemctl操作firewalld

启动

# 启动
systemctl start firewalld
# 关闭
systemctl stop firewalld
# 重启
systemctl restart firewalld
# 查看状态
systemctl status firewalld

开机启动

# 开启开机启动
systemctl enable firewalld
# 关闭开机启动
systemctl disable firewalld
# 查看是否开机启动
systemctl list-unit-files | grep firewalld

firewall-cmd命令

查看状态

[root@localhost ~]# firewall-cmd --state
running

查看默认区域的设置

[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default #目标
  icmp-block-inversion: no #ICMP协议类型黑白名单开关(yes/no)
  interfaces: ens33 #关联的网卡接口
  sources: #来源,可以是IP地址,也可以是mac地址
  services: dhcpv6-client ssh #允许的服务,全部服务见firewall-cmd --get-services
  ports:  #本地的目标端口,即本地开放的端口
  protocols:  #允许通过的协议
  masquerade: no #是否允许伪装(yes/no),可改写来源IP地址及mac地址
  forward-ports:  #允许转发的端口
  source-ports:  #允许的来源端口
  icmp-blocks:  #可添加ICMP类型,当icmp-block-inversion为no时,这些ICMP类型被拒绝,当icmp-block-inversion为yes时,这些ICMP类型被允许
  rich rules:  #富规则,即更细致、更详细的防火墙规则策略,其优先级在所有的防火墙策略中是最高的
	

查看指定区域的所有设置

firewall-cmd --zone=public --list-all

查看所有可用区域

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

查看默认区域

[root@localhost ~]# firewall-cmd --get-default-zone
public

设置默认区域

firewall-cmd --set-default-zone=public

查看所有区域的设置

firewall-cmd --list-all-zones

查看所有预设服务

[root@localhost ~]# firewall-cmd --get-services
RH-Satellite-6 RH-Satellite-6-capsule 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

这样将列出 /usr/lib/firewalld/services/ 中的服务名称,注意配置文件是以服务本身命名的service-name.xml, 如http.xml, https.xml

重新加载

firewall-cmd --reload

--relaod 不会中断已经建立的连接,如果打算中断,可以使用 --complete-reload

运行时配置和永久配置

firewalld 配置的防火墙策略默认为运行时(Runtime)模式,只能临时生效,是立刻生效的,会随着系统的重启会失效
如果想让配置策略永久生效,就需要使用永久(Permanent)模式了,方法就是在 firewall-cmd 命令后面添加 --permanent 参数
但是永久生效模式有一个特点,就是使用它设置的策略只有在系统重启后才会生效
如果想让配置策略永久并立即生效,需要手动执行 firewall-cmd --reload 重载命令
注意,remove 掉 ssh 服务或者 ssh 端口,当前远程登陆会话不会断开,退出后就无法远程连接了

来源地址source

确定使用的区域(zone):

public 是默认区域(zone)。

对于一个接收到的请求具体使用哪个 zone,firewalld 是通过下面三种方式来判断的:

source:来源地址。

Interface:接收请求的网卡。

firewalld:配置的默认区域(zone)。

这三个方式的优先级按顺序依次降低,也就是说如果按照 source 可以找到就不会再按 interface 去找,如果前两个都找不到才会使用第三个默认区域。

source相关命令

#查询
firewall-cmd --list-sources
#增加
firewall-cmd --permanent --zone=public --add-source=192.168.0.0/16
#删除
firewall-cmd --permanent --zone=public --remove-source=192.168.0.0/16


# --permanent模式修改后都需要 --reload 重新加载下才会生效
[root@localhost ~]# firewall-cmd --reload
success

端口port

#查询端口是否放开
firewall-cmd --query-port=8080/tcp
#列出端口
firewall-cmd --list-ports
#新增端口, 端口可以一个端口,也可以是连续的端口范围, 协议可以是tcp或者udp
firewall-cmd --permanent --zone=public --add-port=8080/tcp
firewall-cmd --permanent --zone=public --add-port=8080-8088/tcp
#删除端口
firewall-cmd --permanent --zone=public --remove-port=8080/tcp
firewall-cmd --permanent --zone=public --remove-port=8080-8088/tcp

# --permanent模式修改后都需要 --reload 重新加载下才会生效
[root@localhost ~]# firewall-cmd --reload
success

富规则rich-rule

富规则结构说明

rich-rule结构:

rule
  [source]
  [destination]
  service|port|protocol|icmp-block|icmp-type|masquerade|forward-port|source-port
  [log|nflog]
  [audit]
  [accept|reject|drop|mark]

每部分格式说明
    Rule
            rule [family="ipv4|ipv6"] [priority="priority"]
    Source
            source [not] address="address[/mask]"|mac="mac-address"|ipset="ipset"
    Destination
            destination [not] address="address[/mask]"|ipset="ipset"
    Service
            service name="service name"
    Port
            port port="port value" protocol="tcp|udp|sctp|dccp"
    Protocol
            protocol value="protocol value"
    ICMP-Block
            icmp-block name="icmptype name"           
    ICMP-Type
            icmp-type name="icmptype name"
    Masquerade
            masquerade
    Forward-Port
            forward-port port="port value" protocol="tcp|udp|sctp|dccp" to-port="port value" to-addr="address"
    Source-Port
            source-port port="port value" protocol="tcp|udp|sctp|dccp"
    Log
            log [prefix="prefix text"] [level="log level"] [limit value="rate/duration"]
    NFLog
            nflog [group="group id"] [prefix="prefix text"] [queue-size="threshold"] [limit value="rate/duration"]
    Audit
            audit [limit value="rate/duration"]
    Action
            从 accept、reject、drop、mark 中取一个

详细的富规则说明见: https://manned.org/firewalld.richlanguage.5

富规则示例

# 查看
firewall-cmd --zone=public --list-rich-rules
# 禁ping
firewall-cmd --permanent --zone=public --add-rich-rule="rule protocol value='icmp' drop"
firewall-cmd --permanent --zone=public --remove-rich-rule='rule protocol value="icmp" drop'
# 内网互访, 允许192.168.150.*的所有IP访问
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.10.0/24" accept'
firewall-cmd --permanent --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.10.0/24" accept'

参考

Firewalld防火墙
https://www.cnblogs.com/chenmiao531759321/p/12019122.html

linux防火墙的使用(firewalld)
https://www.cnblogs.com/LiuChang-blog/p/12324201.html

man手册
https://manned.org/firewalld.1
https://manned.org/firewall-cmd.1
富规则说明:
https://manned.org/firewalld.richlanguage.5

Linux命令
man firewalld
man firewalld.richlanguage