firewalld和iptables的關系
firewalld自身並不具備防火牆的功能,而是和iptables一樣需要通過內核的netfilter來實現,也就是說firewalld和iptables一樣,他們的作用都是用於維護規則,而真正使用規則干活的是內核的netfilter,只不過firewalld和iptables的結構以及使用方法不一樣罷了。
firewalld的配置模式
firewalld的配置文件以xml格式為主(主配置文件firewalld.conf例外),他們有兩個存儲位置
1、/etc/firewalld/ 用戶配置文件
2、/usr/lib/firewalld/ 系統配置文件,預置文件
我們知道每個zone就是一套規則集,但是有那么多zone,對於一個具體的請求來說應該使用哪個zone(哪套規則)來處理呢?這個問題至關重要,如果這點不弄明白其他的都是空中樓閣,即使規則設置的再好,不知道怎樣用、在哪里用也不行。
對於一個接受到的請求具體使用哪個zone,firewalld是通過三種方法來判斷的:
1、source,也就是源地址 優先級最高
2、interface,接收請求的網卡 優先級第二
3、firewalld.conf中配置的默認zone 優先級最低
這三個的優先級按順序依次降低,也就是說如果按照source可以找到就不會再按interface去查找,如果前兩個都找不到才會使用第三個,也就是學生在前面給大家講過的在firewalld.conf中配置的默認zone。
安裝firewalld,運行、停止、禁用firewalld
root執行 # yum install firewalld
啟動:# systemctl start firewalld
查看狀態:# systemctl status firewalld 或者 firewall-cmd --state
停止:# systemctl disable firewalld
禁用:# systemctl stop firewalld
配置firewalld
查看版本:$ firewall-cmd --version
查看幫助:$ firewall-cmd --help
查看設置:
顯示狀態:$ firewall-cmd --state
查看區域信息: $ firewall-cmd --get-active-zones
查看指定接口所屬區域:$ firewall-cmd --get-zone-of-interface=eth0
拒絕所有包:# firewall-cmd --panic-on
取消拒絕狀態:# firewall-cmd --panic-off
查看是否拒絕:$ firewall-cmd --query-panic
更新防火牆規則:# firewall-cmd --reload
# firewall-cmd --complete-reload
兩者的區別就是第一個無需斷開連接,就是firewalld特性之一動態添加規則,第二個需要斷開連接,類似重啟服務
將接口添加到區域,默認接口都在public
# firewall-cmd --zone=public --add-interface=eth0
永久生效再加上 --permanent 然后reload防火牆
設置默認接口區域
# firewall-cmd --set-default-zone=public
立即生效無需重啟
打開端口(貌似這個才最常用)
查看所有打開的端口:
# firewall-cmd --zone=dmz --list-ports
加入一個端口到區域:
# firewall-cmd --zone=dmz --add-port=8080/tcp
若要永久生效方法同上
打開一個服務,類似於將端口可視化,服務需要在配置文件中添加,/etc/firewalld 目錄下有services文件夾,這個不詳細說了,詳情參考文檔
# firewall-cmd --zone=work --add-service=smtp
移除服務
# firewall-cmd --zone=work --remove-service=smtp
測試:[root@iotApp&iagri-44 ~]# echo "hello felix" |nc -l 6666
遠程主機:telnet iotApp&iagri-44 6666 返回:
Connected to iotApp&iagri-44
Escape character is '^]'.
hello felix
Connection closed by foreign host.
The format or structure of the rich rule commands is as follows:
rule [family="rule family"]
[ source [NOT] [address="address"] [mac="mac-address"] [ipset="ipset"] ] [ destination [NOT] address="address" ] [ element ] [ log [prefix="prefix text"] [level="log level"] [limit value="rate/duration"] ] [ audit ] [ action ]
Elements:
The element can be only one of the following element types: service, port, protocol, masquerade, icmp-block, forward-port, and source-port.
service name=service_name
port port=number_or_range protocol=protocol
protocol value=protocol_name_or_ID
icmp-block name=icmptype_name
forward-port port=number_or_range protocol=protocol /to-port=number_or_range to-addr=address
source-port port=number_or_range protocol=protocol
log [prefix=prefix text] [level=log level] limit value=rate/duration
action:
accept | reject [type=reject type] | drop | mark set="mark[/mask]"
Using the Rich Rule Log Command Example 3:
rule family="ipv4" source address="192.168.0.0/24" service name="tftp" log prefix="tftp" level="info" limit value="1/m" accept
Using the Rich Rule Log Command Example 4 :
rule family="ipv6" source address="1:2:3:4:6::" service name="radius" log prefix="dns" level="info" limit value="3/m" reject
rule family="ipv6" service name="radius" accept
[root@iotApp&iagri-44 ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=0.0.0.0/16 accept' --permanent
[root@iotApp&iagri-44 ~]# firewall-cmd --help
Usage: firewall-cmd [OPTIONS...]
General Options
-h, --help Prints a short help text and exists
-V, --version Print the version string of firewalld
-q, --quiet Do not print status messages
Status Options
--state Return and print firewalld state
--reload Reload firewall and keep state information
--complete-reload Reload firewall and loose state information
--runtime-to-permanent
Create permanent from runtime configuration
Permanent Options
--permanent Set an option permanently
Usable for options maked with [P]
Zone Options
--get-default-zone Print default zone for connections and interfaces
--set-default-zone=<zone>
Set default zone
--get-active-zones Print currently active zones
--get-zones Print predefined zones [P]
--get-services Print predefined services [P]
--get-icmptypes Print predefined icmptypes [P]
--get-zone-of-interface=<interface>
Print name of the zone the interface is bound to [P]
--get-zone-of-source=<source>[/<mask>]
Print name of the zone the source[/mask] is bound to [P]
--list-all-zones List everything added for or enabled in all zones [P]
--new-zone=<zone> Add a new zone [P only]
--delete-zone=<zone> Delete an existing zone [P only]
--zone=<zone> Use this zone to set or query options, else default zone
Usable for options maked with [Z]
--get-target Get the zone target [P] [Z]
--set-target=<target>
Set the zone target [P] [Z]
IcmpType Options
--new-icmptype=<icmptype>
Add a new icmptype [P only]
--delete-icmptype=<icmptype>
Delete and existing icmptype [P only]
Service Options
--new-service=<service>
Add a new service [P only]
--delete-service=<service>
Delete and existing service [P only]
Options to Adapt and Query Zones
--list-all List everything added for or enabled in a zone [P] [Z]
--list-services List services added for a zone [P] [Z]
--timeout=<timeval> Enable an option for timeval time, where timeval is
a number followed by one of letters 's' or 'm' or 'h'
Usable for options maked with [T]
--add-service=<service>
Add a service for a zone [P] [Z] [T]
--remove-service=<service>
Remove a service from a zone [P] [Z]
--query-service=<service>
Return whether service has been added for a zone [P] [Z]
--list-ports List ports added for a zone [P] [Z]
--add-port=<portid>[-<portid>]/<protocol>
Add the port for a zone [P] [Z] [T]
--remove-port=<portid>[-<portid>]/<protocol>
Remove the port from a zone [P] [Z]
--query-port=<portid>[-<portid>]/<protocol>
Return whether the port has been added for zone [P] [Z]
--list-icmp-blocks List Internet ICMP type blocks added for a zone [P] [Z]
--add-icmp-block=<icmptype>
Add an ICMP block for a zone [P] [Z] [T]
--remove-icmp-block=<icmptype>
Remove the ICMP block from a zone [P] [Z]
--query-icmp-block=<icmptype>
Return whether an ICMP block has been added for a zone
[P] [Z]
--list-forward-ports List IPv4 forward ports added for a zone [P] [Z]
--add-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
Add the IPv4 forward port for a zone [P] [Z] [T]
--remove-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
Remove the IPv4 forward port from a zone [P] [Z]
--query-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
Return whether the IPv4 forward port has been added for
a zone [P] [Z]
--add-masquerade Enable IPv4 masquerade for a zone [P] [Z] [T]
--remove-masquerade Disable IPv4 masquerade for a zone [P] [Z]
--query-masquerade Return whether IPv4 masquerading has been enabled for a
zone [P] [Z]
--list-rich-rules List rich language rules added for a zone [P] [Z]
--add-rich-rule=<rule>
Add rich language rule 'rule' for a zone [P] [Z] [T]
--remove-rich-rule=<rule>
Remove rich language rule 'rule' from a zone [P] [Z]
--query-rich-rule=<rule>
Return whether a rich language rule 'rule' has been
added for a zone [P] [Z]
Options to Handle Bindings of Interfaces
--list-interfaces List interfaces that are bound to a zone [P] [Z]
--add-interface=<interface>
Bind the <interface> to a zone [P] [Z]
--change-interface=<interface>
Change zone the <interface> is bound to [Z]
--query-interface=<interface>
Query whether <interface> is bound to a zone [P] [Z]
--remove-interface=<interface>
Remove binding of <interface> from a zone [P] [Z]
Options to Handle Bindings of Sources
--list-sources List sources that are bound to a zone [P] [Z]
--add-source=<source>[/<mask>]
Bind <source>[/<mask>] to a zone [P] [Z]
--change-source=<source>[/<mask>]
Change zone the <source>[/<mask>] is bound to [Z]
--query-source=<source>[/<mask>]
Query whether <source>[/<mask>] is bound to a zone
[P] [Z]
--remove-source=<source>[/<mask>]
Remove binding of <source>[/<mask>] from a zone [P] [Z]
Direct Options
--direct First option for all direct options
--get-all-chains
Get all chains [P]
--get-chains {ipv4|ipv6|eb} <table>
Get all chains added to the table [P]
--add-chain {ipv4|ipv6|eb} <table> <chain>
Add a new chain to the table [P]
--remove-chain {ipv4|ipv6|eb} <table> <chain>
Remove the chain from the table [P]
--query-chain {ipv4|ipv6|eb} <table> <chain>
Return whether the chain has been added to the table [P]
--get-all-rules
Get all rules [P]
--get-rules {ipv4|ipv6|eb} <table> <chain>
Get all rules added to chain in table [P]
--add-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
Add rule to chain in table [P]
--remove-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
Remove rule with priority from chain in table [P]
--remove-rules {ipv4|ipv6|eb} <table> <chain>
Remove rules from chain in table [P]
--query-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
Return whether a rule with priority has been added to
chain in table [P]
--passthrough {ipv4|ipv6|eb} <arg>...
Pass a command through (untracked by firewalld)
--get-all-passthroughs
Get all tracked passthrough rules [P]
--get-passthroughs {ipv4|ipv6|eb} <arg>...
Get tracked passthrough rules [P]
--add-passthrough {ipv4|ipv6|eb} <arg>...
Add a new tracked passthrough rule [P]
--remove-passthrough {ipv4|ipv6|eb} <arg>...
Remove a tracked passthrough rule [P]
--query-passthrough {ipv4|ipv6|eb} <arg>...
Return whether the tracked passthrough rule has been
added [P]
Lockdown Options
--lockdown-on Enable lockdown.
--lockdown-off Disable lockdown.
--query-lockdown Query whether lockdown is enabled
Lockdown Whitelist Options
--list-lockdown-whitelist-commands
List all command lines that are on the whitelist [P]
--add-lockdown-whitelist-command=<command>
Add the command to the whitelist [P]
--remove-lockdown-whitelist-command=<command>
Remove the command from the whitelist [P]
--query-lockdown-whitelist-command=<command>
Query whether the command is on the whitelist [P]
--list-lockdown-whitelist-contexts
List all contexts that are on the whitelist [P]
--add-lockdown-whitelist-context=<context>
Add the context context to the whitelist [P]
--remove-lockdown-whitelist-context=<context>
Remove the context from the whitelist [P]
--query-lockdown-whitelist-context=<context>
Query whether the context is on the whitelist [P]
--list-lockdown-whitelist-uids
List all user ids that are on the whitelist [P]
--add-lockdown-whitelist-uid=<uid>
Add the user id uid to the whitelist [P]
--remove-lockdown-whitelist-uid=<uid>
Remove the user id uid from the whitelist [P]
--query-lockdown-whitelist-uid=<uid>
Query whether the user id uid is on the whitelist [P]
--list-lockdown-whitelist-users
List all user names that are on the whitelist [P]
--add-lockdown-whitelist-user=<user>
Add the user name user to the whitelist [P]
--remove-lockdown-whitelist-user=<user>
Remove the user name user from the whitelist [P]
--query-lockdown-whitelist-user=<user>
Query whether the user name user is on the whitelist [P]
Panic Options
--panic-on Enable panic mode
--panic-off Disable panic mode
--query-panic Query whether panic mode is enabled
