Linux的防火墙: netfilter防火墙框架, iptables , CentOS的firewalld, Ubuntu的ufw

506 阅读53分钟

Linux的防火墙: netfilter防火墙框架, iptables , firewalld, ufw

netfilter 是Linux的防火墙框架 iptables 是Linux的防火墙的管理工具,基于netfilter firewalld 是Redhat,CentOS等对 ptables 的包装 ufw 是 Debian,Ubuntu等对 iptables 的包装

graph TB
A("  netfilter( Linux内核的防火墙框架 )  " )

B("iptables(Linux) "  )--操作-->A

C("firewalld(CentOS) ")--操作-->B
 D("ufw(Ubuntu)  ")--操作-->B

@[TOC]

netfilter

netfilter官网

netfilter是Linux 2.4内核引入的包过滤引擎。它作为一个通用的、抽象的框架,提供一整套的hook函数的管理机制,使得诸如数据包过滤、网络地址转换(NAT)和基于协议类型的连接跟踪成为了可能。 netfilter的架构就是在整个网络流程的若干位置放置了一些检测点(HOOK),而在每个检测点上登记了一些处理函数进行处理。

现今许多市面上许多的IP分享器或无线网络路由器(Wireless router),多是嵌入式Linux平台,并利用Netfilter的数据包处理能力,提供NAT以及防火墙的功能。

在这里插入图片描述

Netfilter IP层的五个HOOK点 (五钩)

  1. NF_IP_PRE_ROUTING:刚刚进入网络层的数据包通过此点(刚刚进行完版本号,校验 和等检测), 目的地址转换在此点进行;
  2. NF_IP_LOCAL_IN:经路由查找后,送往本机的通过此检查点,INPUT包过滤在此点进行;
  3. NF_IP_FORWARD:要转发的包通过此检测点,FORWARD包过滤在此点进行;
  4. NF_IP_POST_ROUTING:所有马上便要通过网络设备出去的包通过此检测点,内置的源地址转换功能(包括地址伪装)在此点进行;
  5. NF_IP_LOCAL_OUT:本机进程发出的包通过此检测点,OUTPUT包过滤在此点进行。

@[TOC]

iptables

netfilter官网 netfilter官网 iptables 项目 Iptables 指南 --- Iptables Tutorial 1.2.2

iptables的四表五链

表(tables):提供特定的功能,iptables内置了4个表,即filter表、nat表、mangle表和raw表,分别用于实现包过滤,网络地址转换、包重构(修改)和数据跟踪处理。 链(chains):是数据包传播的路径,每一条链其实就是众多规则中的一个检查清单,每一条链中可以有一 条或数条规则。当一个数据包到达一个链时,iptables就会从链中第一条规则开始检查,看该数据包是否满足规则所定义的条件。如果满足,系统就会根据 该条规则所定义的方法处理该数据包;否则iptables将继续检查下一条规则,如果该数据包不符合链中任一条规则,iptables就会根据该链预先定 义的默认策略来处理数据包。

表链关系图 在这里插入图片描述

四表

filter表:过滤规则表,根据预定义的规则过滤符合条件的数据包 nat表:network address translation 地址转换规则表 mangle表:修改数据标记位规则表 raw表:关闭启用的连接跟踪机制,加快封包穿越防火墙速度

表的执行顺序 : mangle->nat->filter

五链

  1. PREROUTING : 路由之前的规则链
  2. INPUT : 数据包入口规则链
  3. FORWARD : 转发规则链
  4. OUTPUT : 数据包出口规则链
  5. POSTROUTING : 路由之前的规则链

五链对五钩

iptables的五链(chain)
  1. PREROUTING : 路由之前的规则链 , 可用于目标网络地址转换(DNAT)。
  2. INPUT : 数据包入口规则链
  3. FORWARD : 转发规则链
  4. OUTPUT : 数据包出口规则链
  5. POSTROUTING : 路由之后的规则链, 可用于源网络地址转换(SNAT)。
iptables的五链(chain)对应netfilter的五钩(hook)
  1. PREROUTING 对应 NF_INET_PRE_ROUTING
  2. INPUT 对应 NF_INET_LOCAL_IN
  3. FORWARD 对应 NF_INET_FORWARD
  4. OUTPUT 对应 NF_INET_LOCAL_OUT
  5. POSTROUTING 对应 NF_INET_POST_ROUTING
以用iptables开放3306端口的例子说明
##      在INPUT链插入一条规则,允许tcp协议的3306目标端口的包通行
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
### 或  在INPUT链插入一条规则,允许tcp协议的3306目标端口的包通行
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
## -I INPUT 和 -A INPUT 是 在INPUT链插入或追加规则
## -p 协议 , -p tcp tcp协议
## --dport 3306 (dest port)目标端口
## -j ACCEPT 解释 -j(jump target跳转目标,先当于链匹配后干什么,可接 ACCEPT,DROP,REJECT等) , ACCEPT:接包
  • -I INPUT-A INPUT 是 在INPUT链插入或追加规则 , -I等效--insert , -A等效--append
  • -p 接协议 , -p tcp tcp协议 , -p等效--protocol
  • --dport 3306 (dest port)目标端口
  • -j ACCEPT -j等效--jump (jump target)跳转目标,先当于链匹配后干什么,可接 ACCEPT,DROP,REJECT等) ACCEPT:接包回应 , DROP丢包不回应 , REJECT回应拒绝 , LOG在/var/log/messages文件中记录 日志信息,然后将数据包传递给下一条规则

iptables 语句的语法规则

iptables [-t table] command [match] [target/jump]

iptables [-t table] command [match] [target/jump]

除了 command 部分是必须的, 其它部分是可选的 在这里插入图片描述 对上图parameter部分更详细的说明 在这里插入图片描述 ! 感叹号是取反排除的意思, 可选

语法中 COMMAND 部分的选项

语法 " iptables [-t table] command [match] [target/jump] " 中, 除了 command 部分是必须的

CommandExampleExplanation
-A

--append
iptables -A INPUT ...This command appends the rule to the end of the chain. The rule will in other words always be put last in the rule-set and hence be checked last, unless you append more rules later on.
-D

--delete
iptables -D INPUT --dport 80 -j DROP, iptables -D INPUT 1This command deletes a rule in a chain. This could be done in two ways; either by entering the whole rule to match (as in the first example), or by specifying the rule number that you want to match. If you use the first method, your entry must match the entry in the chain exactly. If you use the second method, you must match the number of the rule you want to delete. The rules are numbered from the top of each chain, starting with number 1.
-R

--replace
iptables -R INPUT 1 -s 192.168.0.1 -j DROPThis command replaces the old entry at the specified line. It works in the same way as the --delete command, but instead of totally deleting the entry, it will replace it with a new entry. The main use for this might be while you're experimenting with iptables.
-I

--insert
iptables -I INPUT 1 --dport 80 -j ACCEPTInsert a rule somewhere in a chain. The rule is inserted as the actual number that we specify. In other words, the above example would be inserted as rule 1 in the INPUT chain, and hence from now on it would be the very first rule in the chain.
-L

--list
iptables -L INPUTThis command lists all the entries in the specified chain. In the above case, we would list all the entries in the INPUT chain. It's also legal to not specify any chain at all. In the last case, the command would list all the chains in the specified table (To specify a table, see the Tables section). The exact output is affected by other options sent to the parser, for example the -n and -v options, etc.
-F

--flush
iptables -F INPUTThis command flushes all rules from the specified chain and is equivalent to deleting each rule one by one, but is quite a bit faster. The command can be used without options, and will then delete all rules in all chains within the specified table.
-Z

--zero
iptables -Z INPUTThis command tells the program to zero all counters in a specific chain, or in all chains. If you have used the -v option with the -L command, you have probably seen the packet counter at the beginning of each field. To zero this packet counter, use the -Z option. This option works the same as -L, except that -Z won't list the rules. If -L and -Z is used together (which is legal), the chains will first be listed, and then the packet counters are zeroed.
-N

--new-chain
iptables -N allowedThis command tells the kernel to create a new chain of the specified name in the specified table. In the above example we create a chain called allowed. Note that there must not already be a chain or target of the same name.
-X

--delete-chain
iptables -X allowedThis command deletes the specified chain from the table. For this command to work, there must be no rules that refer to the chain that is to be deleted. In other words, you would have to replace or delete all rules referring to the chain before actually deleting the chain. If this command is used without any options, all chains but those built in to the specified table will be deleted.
-P

--policy
iptables -P INPUT DROPThis command tells the kernel to set a specified default target, or policy, on a chain. All packets that don't match any rule will then be forced to use the policy of the chain. Legal targets are DROP and ACCEPT (There might be more, mail me if so).
-E

--rename-chain
iptables -E allowed disallowedThe -E command tells iptables to change the first name of a chain, to the second name. In the example above we would, in other words, change the name of the chain from allowed to disallowed. Note that this will not affect the actual way the table will work. It is, in other words, just a cosmetic change to the table.
命令说明
-A
等效
--append
将规则rule到指定chain的最后
iptables -A INPUT …
-D
等效
--drop
将rule从执行chain中删除。两种使用方式
1. 指定完整的规则
2. 指定规则的序号:序号chain中从上到下依次递增,1开始
iptables -D INPUT --dport 80 -j DROP, iptables -D INPUT 1
-R
等效
--replace
替换指定位置的rule
iptables -R INPUT 1 -s 192.168.0.1 -j DROP
-I
等效
--insert
向chain中的指定位置插入rule
iptables -I INPUT 1 --dport` 80 -j ACCEPT
-L
等效
--list
列出指定chain中的所有rule
iptables -L INPUT
-F
等效
--flush
清空指定chain上的所有rule,和一条一条删除效果一样,只不过更快
iptables -F INPUT
-Z
等效
--zero
清空指定chain上的计数器。这些计数器用于计量包数和字节数
iptables -Z INPUT
-N
等效
--new-chain
创建一个新的chain
iptables -N allowed
-X
等效
--delete-chain
删除置顶chain,只有已经被清空的chain,即没有任何rule的chain才能被删除
内建的chain如INPUT等是无法被删除的
iptables -X allowed
-P
等效
--policy
为chain设置默认的target或policy,在该chain上,任何没有被rule匹配到的包,都会应用该默认规则。
只有两个合法的target:DROP 和 ACCEPT
iptables -P INPUT DROP
-E
等效
--rename-chain
重命名chain
iptables -E allowed disallowed

语法中 parameter/match 参数/匹配 部分的选项

www.frozentux.net/iptables-tu…

匹配项说明
-p --protocol指定协议 例如 -p tcp
- 协议只能是 /etc/protocols 文件中存在的,否则会报错
- 可以正向也可以反向。
iptables -A INPUT -p ! tcp 表示非tcp
-s --src --source来源IP,可以有多种形式。也可以用 ! 运算符反向指定
- 单个IP形式 192.168.0.0
- CIDR形式 192.168.0.0/24
- 子网掩码形式 192.168.0.0/255.255.255.0
-d --dst --destination同上,反向而已
-i --in-interface指定包的来源接口,如en0。
- 只能在INPUT FORWARD PREROUTING 三个chain使用
- 允许通配,+表示所有接口,en+表示en开头的所有接口
-o --out-interface同上,反向而已
-f --fragment匹配分段数据包的第二、第三。。。段
如果不指定,就只会匹配未分段的数据包或者分段数据包的第一段
TCP相关的匹配

有关 TCP 的匹配 , 加上-p tcp

匹配项说明例子
--sport, --source-port匹配来源端口iptables -A INPUT -p tcp --sport 22
--dport, --destination-port匹配目标端口iptables -A INPUT -p tcp --dport 22
--tcp-flags匹配tcp标记,如SYN/RST/ACK/FIN等,也可用ALL、NONEiptables -p tcp --tcp-flags SYN,FIN,ACK SYN
--syn遗留语法,和 --tcp-flags SYN,FIN,ACK SYN一个效果iptables -p tcp --syn
--tcp-option根据tcp选项匹配iptables -p tcp --tcp-option 16
Match--sport, --source-port
Kernel2.3, 2.4, 2.5 and 2.6
Exampleiptables -A INPUT -p tcp --sport 22
ExplanationThe --source-port match is used to match packets based on their source port. Without it, we imply all source ports. This match can either take a service name or a port number. If you specify a service name, the service name must be in the /etc/services file, since iptables uses this file in which to find. If you specify the port by its number, the rule will load slightly faster, since iptables don't have to check up the service name. However, the match might be a little bit harder to read than if you use the service name. If you are writing a rule-set consisting of a 200 rules or more, you should definitely use port numbers, since the difference is really noticeable. (On a slow box, this could make as much as 10 seconds' difference, if you have configured a large rule-set containing 1000 rules or so). You can also use the --source-port match to match any range of ports, --source-port 22:80 for example. This example would match all source ports between 22 and 80. If you omit specifying the first port, port 0 is assumed (is implicit). --source-port :80 would then match port 0 through 80. And if the last port specification is omitted, port 65535 is assumed. If you were to write --source-port 22:, you would have specified a match for all ports from port 22 through port 65535. If you invert the port range, iptables automatically reverses your inversion. If you write --source-port 80:22, it is simply interpreted as --source-port 22:80. You can also invert a match by adding a ! sign. For example, --source-port ! 22 means that you want to match all ports but port 22. The inversion could also be used together with a port range and would then look like --source-port ! 22:80, which in turn would mean that you want to match all ports but ports 22 through 80. Note that this match does not handle multiple separated ports and port ranges. For more information about those, look at the multiport match extension.
--source-port 匹配用于根据数据包的源端口匹配数据包。没有它,我们暗示所有源端口。此匹配可以采用服务名称或端口号。如果指定服务名,则服务名必须在 /etc/services 文件中,因为 iptables 使用该文件在其中查找。如果您通过端口号指定端口,则规则加载速度会稍快一些,因为 iptables 不必检查服务名称。但是,与使用服务名称相比,匹配项可能更难阅读。如果您正在编写包含 200 条或更多规则的规则集,则绝对应该使用端口号,因为差异非常明显。 (如果您配置了一个包含 1000 条左右规则的大型规则集,那么在慢速机器上,这可能会产生多达 10 秒的差异)。您还可以使用 --source-port 匹配来匹配任何范围的端口,例如 --source-port 22:80。此示例将匹配 22 到 80 之间的所有源端口。如果您省略指定第一个端口,则假定端口 0(隐含)。 --source-port :80 然后将匹配端口 0 到 80。如果省略最后一个端口规范,则假定端口 65535。如果你写--source-port 22:,你会为从端口22到端口65535的所有端口指定一个匹配。如果你反转端口范围,iptables会自动反转你的反转。如果你写--source-port 80:22,它会简单地解释为--source-port 22:80。您还可以通过添加一个来反转匹配!符号。例如, --source-port ! 22 表示您要匹配除端口 22 之外的所有端口。反转也可以与端口范围一起使用,然后看起来像 --source-port ! 22:80,这反过来意味着您要匹配除端口 22 到 80 之外的所有端口。请注意,此匹配不处理多个单独的端口和端口范围。有关这些的更多信息,请查看多端口匹配扩展。
Match--dport, --destination-port
Kernel2.3, 2.4, 2.5 and 2.6
Exampleiptables -A INPUT -p tcp --dport 22
ExplanationThis match is used to match TCP packets, according to their destination port. It uses exactly the same syntax as the --source-port match. It understands port and port range specifications, as well as inversions. It also reverses high and low ports in port range specifications, as above. The match will also assume values of 0 and 65535 if the high or low port is left out in a port range specification. In other words, exactly the same as the --source-port syntax. Note that this match does not handle multiple separated ports and port ranges. For more information about those, look at the multiport match extension.
此匹配用于根据目标端口匹配 TCP 数据包。 它使用与 --source-port 匹配完全相同的语法。 它了解端口和端口范围规范以及倒置。 如上所述,它还反转端口范围规范中的高端口和低端口。 如果端口范围规范中忽略了高端口或低端口,则匹配还将采用 0 和 65535 的值。 换句话说,与 --source-port 语法完全相同。 请注意,此匹配不处理多个单独的端口和端口范围。 有关这些的更多信息,请查看多端口匹配扩展。
Match--tcp-flags
Kernel2.3, 2.4, 2.5 and 2.6
Exampleiptables -p tcp --tcp-flags SYN,FIN,ACK SYN
ExplanationThis match is used to match on the TCP flags in a packet. First of all, the match takes a list of flags to compare (a mask) and secondly it takes list of flags that should be set to 1, or turned on. Both lists should be comma-delimited. The match knows about the SYN, ACK, FIN, RST, URG, PSH flags, and it also recognizes the words ALL and NONE. ALL and NONE is pretty much self describing: ALL means to use all flags and NONE means to use no flags for the option. --tcp-flags ALL NONE would in other words mean to check all of the TCP flags and match if none of the flags are set. This option can also be inverted with the ! sign. For example, if we specify ! SYN,FIN,ACK SYN, we would get a match that would match packets that had the ACK and FIN bits set, but not the SYN bit. Also note that the comma delimitation should not include spaces. You can see the correct syntax in the example above.
Match--syn
Kernel2.3, 2.4, 2.5 and 2.6
Exampleiptables -p tcp --syn
ExplanationThe --syn match is more or less an old relic from the ipchains days and is still there for backward compatibility and for and to make transition one to the other easier. It is used to match packets if they have the SYN bit set and the ACK and RST bits unset. This command would in other words be exactly the same as the --tcp-flags SYN,RST,ACK SYN match. Such packets are mainly used to request new TCP connections from a server. If you block these packets, you should have effectively blocked all incoming connection attempts. However, you will not have blocked the outgoing connections, which a lot of exploits today use (for example, hacking a legitimate service and then installing a program or suchlike that enables initiating an existing connection to your host, instead of opening up a new port on it). This match can also be inverted with the ! sign in this, ! --syn, way. This would match all packets with the RST or the ACK bits set, in other words packets in an already established connection.
Match--tcp-option
Kernel2.3, 2.4, 2.5 and 2.6
Exampleiptables -p tcp --tcp-option 16
ExplanationThis match is used to match packets depending on their TCP options. A TCP Option is a specific part of the header. This part consists of 3 different fields. The first one is 8 bits long and tells us which Options are used in this stream, the second one is also 8 bits long and tells us how long the options field is. The reason for this length field is that TCP options are, well, optional. To be compliant with the standards, we do not need to implement all options, but instead we can just look at what kind of option it is, and if we do not support it, we just look at the length field and can then jump over this data. This match is used to match different TCP options depending on their decimal values. It may also be inverted with the ! flag, so that the match matches all TCP options but the option given to the match. For a complete list of all options, take a closer look at the Internet Engineering Task Force who maintains a list of all the standard numbers used on the Internet.

语法中的 -j 部分

www.frozentux.net/iptables-tu…

-j 等效 --jump (jump target) 目标跳转

指定规则的目标;也就是说,如果包匹配应当做什么。目标可以是用户自定义链(不是这条规则所在的),某个会立即决定包的命运的专用内建目标,或者一个扩展(参见下面的EXTENSIONS)。如果规则的这个选项被忽略,那么匹配的过程不会对包产生影响,不过规则的计数器会增加。

-j后接的target
jump target
说明
ACCEPT允许数据包通过。
DROP直接丢弃数据包,不给任何回应信息,客户端只能干等,直到超时。
REJECT明确拒绝数据包,会给发送端回复一个响应,明确告知数据包被拒绝。
REDIRECT重定向,在本机做端口映射。
LOG在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则,
也就是说除了记录以外不对数据包做任何其他操作,仍然让下一条规则去匹配。
SNAT源地址转换,解决内网用户用同一个公网地址上网的问题。
MASQUERADE是SNAT的一种特殊形式,适用于动态的、临时会变的ip上。
DNAT目标地址转换。

ACCEPT , CLASSIFY , CLUSTERIP , CONNMARK , CONNSECMARK , DNAT , DROP , DSCP , ECN , LOG , MARK , MASQUERADE , MIRROR , NETMAP , NFQUEUE , NOTRACK , QUEUE , REDIRECT , REJECT , RETURN , SAME , SECMARK , SNAT , TCPMSS , TOS , TTL , ULOG

iptables 命令行用例

列出iptables的链规则 -L 或 --list , (查看开放的端口)

列出 iptables 的所有链的规则
### 列出 iptables 的所有链的规则, 用数字显示地址和端口
iptables -L -n 
#####  n和L写在一起的话, n必须在L前面
iptables -nL
###  列出 iptables 的所有链的规则
iptables -L
iptables --list
###  更详细地列出 iptables 的所有链的规则
iptables -L -v
###  更更详细地列出 iptables 的所有链的规则
iptables -L -vv
###  更更更详细地列出 iptables 的所有链的规则
iptables --list -vvv
列出 iptables 的 INPUT 链 的规则
###  列出 iptables 的 INPUT 链的规则
iptables -L INPUT
### 列出 iptables 的 INPUT 链的规则, 用数字显示地址和端口
iptables -nL INPUT
iptables -n -L INPUT
#####  -L必须接链, -n可以写在-L之前或链后, 不能写成 iptables -Ln INPUT 或 iptables -L -n INPUT , 可以写成 iptables -L INPUT -n
iptables -L INPUT -n
###  更详细列出 iptables 的 INPUT 链的规则
iptables -L INPUT -v
iptables -L INPUT -v -n
iptables -L INPUT -nv
iptables -L INPUT -vn
###  更更详细列出 iptables 的 INPUT 链的规则
iptables -L INPUT -vvv
iptables -L INPUT -vvv -n
iptables -L INPUT -v -n -v -v
iptables -L INPUT -nvvvvvvv
iptables -L INPUT -vvnvv

开放关闭端口

开放关闭端口, 添加允许所有ip的 22 , 80 , 443 , 1433 , 3306 , 8080 端口的数据包通行的规则

向INPUT链插入端口规则 , 开放 22 , 80 , 443 , 1433 , 3306 , 8080 端口 , 用-I,还可以指定从第几行插入

### 向INPUT链插入端口规则 , 开放 22 , 80 , 443  , 1433 , 3306 , 8080 端口
sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 1433 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
### 向INPUT链第五行前插入端口规则 , 开放 22 , 80 , 443  , 1433 , 3306 , 8080 端口 
sudo iptables -I INPUT 5 -p tcp --dport 22 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 443 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 1433 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 3306 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 8080 -j ACCEPT

向INPUT链追加端口规则 , 开放 22 , 80 , 443 , 1433 , 3306 , 8080 端口 , 用 -A

### 向INPUT链追加端口规则 , 开放 22 , 80 , 443  , 1433 , 3306 , 8080 端口
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 1433 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

删除INPUT链的规则---删除 "22 , 80 , 443 , 1433 , 3306 , 8080 端口通行" 这些条规则 上面用-I-A两次添加了相同的规则,分别插入在INPUT链开头,和追加在INPUT链末尾 , 下面的删除代码也要执行两次才会删除这些规则

###  删除INPUT链的规则---删除 "22 , 80 , 443  , 1433 , 3306 , 8080 端口通行"  这些条规则
sudo iptables -D INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 1433 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 3306 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 8080 -j ACCEPT
开放关闭不连续端口 , 使用-m multiport使得--dpart--spart后的端口可以用逗号,分隔
sudo iptables -I INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
sudo iptables -A INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
sudo iptables -D INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
开放关闭连续端口, 用冒号:分隔 , 开始端口最小1:末尾端口最大65535

插入1到65535端口, 追加1到65535端口, 删除两遍

sudo iptables -I INPUT -p tcp --dport 1:65535 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 1:65535 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
开放关闭连续和不连续的端口

开放22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口

### 开放 22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口
sudo iptables -I INPUT -p tcp -m multiport --dport 22,80,443,666:888,1433,3306:3399,8000:8999 -j ACCEPT
上面的方法一起用

在INPUT链的第五行前插入一条规则如下 允许来自192.168.0.100的22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口 的数据包通行 然后删除这条规则

###  查看INPUT链的规则
iptables -nL INPUT
###  在INPUT链的第五行前插入一条规则如下
###  允许来自192.168.0.100的22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口 的数据包通行
sudo iptables -I INPUT -p tcp -s 192.168.0.100 -m multiport --dport 22,80,443,666:888,1433,3306:3399,8000:8999 -j ACCEPT
###  查看INPUT链的规则
iptables -nL INPUT
###  删除上面插入的规则
sudo iptables -D INPUT -p tcp -s 192.168.0.100 -m multiport --dport 22,80,443,666:888,1433,3306:3399,8000:8999 -j ACCEPT

开放关闭源IP的端口

在上面的命令中加 -s ip地址 就能指定ip

对指定IP开放关闭 3306 端口 , 用-s--source接IP地址

对192.168.0.100 开放关闭 3306 端口 , 用-s--source接IP地址

sudo iptables -I INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
iptables -nL INPUT

如果重复执行添加(插入或追加),就会出现重复的规则,删除也要删除相同次数才能删完, 示例
sudo iptables -I INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -I INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -I INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -I INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT

清除所有规则 -F--flush

删除所有链所有规则iptables -F
###  查看所有链所有规则
iptables -L
###  清除所有链所有规则
iptables -F
iptables -flush
###  查看所有链所有规则
iptables -L
删除 INPUT 链所有规则iptables --flush INPUT
###  查看 INPUT 链所有规则
iptables -L INPUT
###  清除 INPUT 链所有规则
iptables --F INPUT
iptables --flush INPUT
###  查看 INPUT 链所有规则
iptables -L INPUT







filewalld (Redhat,CentOS,Fedora等)

filewalld官网

  • firewalld是对iptables的封装,使防火墙管理起来更容易更清晰, 本质上是 firewald操作iptables操作netfilter, 底层始终是netfilter
  • firewalld使用zone(区域)概念,相当于Windows的专用网络公用网络等, 其实质是一套套规则, 方便切换
  • firewalld的每个zone指定了target, 就是iptables的target
  • firewalld的zone指定了端口port,和服务service 可以通过指定端口放行数据包,也可以使用service指定的端口放行数据包 zone可以直接管理端口, 也可以通过service间接管理端口
graph LR
A[<h3>zone</h3>firewalld的区域<br/>zone] -- zone直接包含IP协议端口--> D
A --> C(<h3>service</h3><br/>zone包含service<br/>service包含IP协议端口)
 D{管理IP协议端口}
C --> D

firewalld对比iptables

  • iptables使用chains链管理规则 , firewalld使用zones区域和services服务管理规则, zone区域类似Windows的专用网络公用网络等
  • iptables默认都放行,需添加限制规则 ; firewalld默认都限制,需要添加放行规则
  • iptables的规则时静态的,每修改一条都要全部清除,重新读取配置表,全部刷新,已有连接会断开 firewalld的规则是动态的, 修改后firewall-cmd --reload不会断开已有连接
  • iptables 没有守护进程,并不能算是真正意义上的服务, 而 firewalld 有守护进程
  • iptables 通过控制端口来控制服务,而 firewalld 则是通过控制协议来控制端口

启用禁用启动停止重启firewalld

CentOS7的firewalld默认是启用的 使用systemctl命令时, firewalld等效firewalld.service

启用firewalld服务, 开机启动firewalld服务

###  启用firewalld服务, 开机启动firewalld服务
sudo systemctl enable firewalld
sudo systemctl enable firewalld.service
###  查看是否已启用firewalld服务,是enable启用还是disable禁用? 有可能启用但未启动,也可能禁用但当前启动
systemctl is-enabled firewalld.service

禁用firewalld服务, 系统启动时不开启firewalld服务

###  禁用firewalld服务, 系统启动时不开启firewalld服务
sudo systemctl disable firewalld
sudo systemctl disable firewalld.service
###  查看firewalld服务是enable启用还是disable禁用? 有可能启用但未启动,也可能禁用但当前启动
systemctl is-enabled firewalld.service

启动firewalld服务

###  启动firewalld服务
sudo systemctl start firewalld
sudo systemctl start firewalld.service

停止firewalld服务

###   停止firewalld服务
sudo systemctl stop firewalld
sudo systemctl stop firewalld.service

重启firewalld服务, 重新加载firewalld, 使修改立即生效

##  使配置或修改立即生效
###   重启firewalld服务, 会断开已有连接, 可以在服务停止状态执行
sudo systemctl restart firewalld
sudo systemctl restart firewalld.service
###   重新加载firewalld, 不会断开已有连接, 不能在firewalld服务处于停止状态的时候执行
sudo firewall-cmd --reload
firewall-cmd --reload 对比 systemctl restart firewalld
  • firewall-cmd --reload 是使修改的配置生效, 不会断开连接
  • systemctl restart firewalld 是重启firewalld服务, 新配置会生效, 但会断开已有连接

比如: 远程控制台已建立连接, 此时本地关闭22端口,--delete-service ssh服务,

  • 执行firewall-cmd --reload并不会断开连接, 但远程退出后就无法登录了
  • 执行 systemctl restart firewalld 则远程连接立即断开

查看firewalld服务状态, 服务是否运行

sudo systemctl status firewalld
sudo systemctl status firewalld.service
sudo firewall-cmd --state

systemctl扩展知识

查看已启用的服务列表:systemctl list-unit-files|grep enabled 查看启动失败的服务:systemctl --failed




firewall的两个配置文件夹,预定义和自定义的

  • 预定义文件夹 : /usr/lib/firewalld/
    cd /usr/lib/firewalld/
    ls /usr/lib/firewalld/
    
  • 自定义文件夹 : /etc/firewalld/
    cd /etc/firewalld
    ls /etc/firewalld
    



在这里插入图片描述

在控制台操作 firewalld ,
firewall-cmd 或者用 firewall-offline-cmd

控制台操作 firewalld , 用 firewall-cmdfirewall-offline-cmd命令, 而不是 firewalld offline是脱机的,离线的意思.

  • firewall-cmd只能在firewalld服务运行时使用,
  • firewall-offline-cmd 在firewalld运行和停止时都能够使用
  • 如果本地机没有图形界面, 又没开放ssh端口, 可以先在本地机关闭firewalld服务, 远程机连上后用firewall-offline-cmd操作,方便复制粘贴配置,配置好后再开启firewalld服务

两种开头大部分用法相同, 但也有一些区别,比如

  • 运行时可以使用firewall-cmd --reload 命令, 但没有 firewall-offline-cmd --reload 命令
  • firewall-offline-cmd 可以在非运行状态执行, 很多命令本身就是永久生效的, 不能再加--permanent
  • firewall-offline-cmd 不能使用 --get-active-zone获取活动zone, 但能查看默认zone, --get-default-zone

man firewall-cmdman firewall-offline-cmd可查看说明手册 用firewall-cmd --helpfirewall-offline-cmd --help 可以查看帮助

firewall-cmd --help | grep port 查看port相关的帮助内容 firewall-cmd --help | grep service 查看service相关的帮助内容 firewall-cmd --help | grep zone 查看zone相关的帮助内容 firewall-cmd --help | grep interface 查看interface相关的帮助内容


permanent

--permanent 选项, 使设置永久生效

firewall-cmd命令语句中的一些设置, 加--permanent 才永久生效, 不加--permanent的话

  • 立即生效的命令,不加--permanent的话,firewall-cmd --reload后会失效
  • firewall-cmd --reload后才生效的命令, 不加--permanent的话,在重启系统 或systemctl restart firewalld 后就失效

有些设置本身就永久生效, 不用加,也不能加--permanent firewall-offline-cmd开头的命令,一般都不能加--permanent , 本身就是永久的


firewalld的区域(zone)概念

firewalld官方文档---Zone

firewalld的zones类似Windonws的专用网络公用网络等, 相当于规则模板, 有利于管理的方便

firewalld自带的zones , 预定义的zones

来自官方文档 https://firewalld.org/documentation/zone/predefined-zones.html

Predefined Zones 预定义区域 These are the zones provided by firewalld sorted according to the default trust level of the zones from untrusted to trusted: 下面这些是 firewalld 提供的预定义区域,根据其默认的信任级别从不信任到信任排序, 按信任度从低到高排列

  • drop 信任度最低

Any incoming network packets are dropped, there is no reply. Only outgoing network connections are possible. 任何传入的网络数据包都被丢弃,没有回复。 只有传出网络连接是可能的。 只能发送,不能接收

  • block

Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated within this system are possible. 任何传入的网络连接都会被拒绝,并使用 IPv4 的 icmp-host-prohibited 消息和 IPv6 的 icmp6-adm-prohibited 消息。 只有在该系统内发起的网络连接是可能的。 被拒绝的连接会收到一个icmp,返回目标主机不可达。

  • public

For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted. 用于公共区域。 您不相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。 这也是firewalld默认的区域,默认不开放22以外的任何端口,需手动添加

  • external

For use on external networks with masquerading enabled especially for routers. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted. 用于启用伪装的外部网络,尤其适用于路由器。 您不相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。

  • dmz

For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted. 对于您的非军事区内的计算机可以公开访问,但对您的内部网络的访问权限有限。 仅接受选定的传入连接。

  • work

For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.

  • home

For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted. 用于工作区域。 您主要相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。

  • internal

For use on internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted. 用于内部网络。 您主要相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。

  • trusted 信任度最高

All network connections are accepted. 接受所有网络连接。相当于关闭防火墙

  • firewalld使用zone(区域)概念,相当于Windows的专用网络公用网络等, 其实质是一套套规则, 方便切换
  • firewalld的每个zone指定了target, 就是iptables的target
  • firewalld的zone指定了端口port,和服务service 可以通过指定端口放行数据包,也可以使用service指定的端口放行数据包 zone可以直接管理端口, 也可以通过service间接管理端口
graph LR
A[<big><big><b>zone</b></big></big><br/>firewalld的区域<br/>zone] -- zone直接包含IP协议端口--> D
A --> C(<big>service</big><br/>zone包含service<br/>service包含IP协议端口)
 D{管理IP协议端口}
C --> D

firewalld可以指定一个default zone, 还可以给每个网卡接口interface指定一个zone, 当网卡没有指定zone时,默认zone对该网卡生效 可以 用firewall-cmd --get-default-zone查看默认zone , 用firewall-offline-cmd --get-default-zone查看默认zone , 用firewall-cmd --get-active-zone查看网卡接口zone 不能用firewall-offline-cmd --get-active-zone

  • 刚安装的CentOS7 的默认的zone和主网卡的zone 都是public 在这里插入图片描述
  • 刚安装的Fedora 36 WorkStation版 的默认的zone和主网卡的zone 都是FedoraWorkstation 说明zone也并不局限于上面那几个 在这里插入图片描述
  • ens33是Vmware的默认主网卡名称, enp0s3是VirtualBox默认的主网卡名称
查看 active活动zone 和 default默认zone
查看当前活动zone

查看当前活动zone只能用firewall-cmd开头 , 不能firewall-offline-cmd开头

sudo firewall-cmd --get-active-zone   ###  查看当前活动zone
查看默认zone

查看默认zone既可以用firewall-cmd开头 , 也能firewall-offline-cmd开头

sudo firewall-cmd --get-default-zone   ###  查看默认zone
sudo firewall-offline-cmd --get-default-zone   ###  查看默认zone
查看有哪些zone可选

firewalld用xml文件保存zone模板,

  • 可以到/usr/lib/firewalld/zones目录查看预定义的zone模板 或自定义的zone模板目录 /etc/firewalld/zones查看自定义的模板,自定义模板的目录一开始是空文件夹,如果对区域进行了修改,就会在自定义文件夹 /etc/firewalld/zones出现同名的xml,预定义文件夹/usr/lib/firewalld/zones的xml不会变化. 新增的自定义名称的zone也会出现在 /etc/firewalld/zones
cd /usr/lib/firewalld/zones     ### 预定义的zone模板的目录
ls /usr/lib/firewalld/zones 
cd /etc/firewalld/zones       ### 自定义的zone模板的目录 , 该目录模板优先级高于预定义目录的模板
ls /usr/lib/firewalld/zones 

cat /usr/lib/firewalld/zones/public.xml
cat /etc/firewalld/zones/public.xml   ###  修改public才会出现

  • 也可以用命令查看有哪些zone --get-zones 列出预定义和自定义的所有zone的名称 --list-all-zones 列出预定义和自定义的所有zone的详情
列出已定义的zone的名称 --get-zones , 只显示名称

列出已定义的zone --get-zones , 包括预定义和自定义的zone , 只显示名称

sudo firewall-cmd --get-zones   ###  列出所有区域的名称
sudo firewall-offline-cmd --get-zones   ###  列出所有区域的名称

例如

  • CentOS 7 最小安装版
    sudo firewall-cmd --get-zones
    block dmz drop external home internal public trusted work
    
    CentOS 7 默认使用了firewalld预定义的zone中的public
  • Fedora 36 WorkStation版
    sudo firewall-cmd --get-zones
    FedoraServer FedoraWorkstation block dmz drop external home internal libvirt nm-shared public trusted work
    
    Fedora 36 WorkStation版 默认并没有用firewalld预定义的zone , 而是自定义了一个新zone 名为 FedoraWorkstation
列出所有zone的配置详情(添加的和启用的规则) --list-all-zones
sudo firewall-cmd --list-all-zones   ###  列出所有区域的配置详情
sudo firewall-offline-cmd --list-all-zones   ###  列出所有区域的配置详情
列出默认区域 default zone 的配置详情(添加的和启用的规则) --list-all
sudo firewall-cmd --list-all   ###   列出默认区域的配置详情
sudo firewall-offline-cmd --list-all   ###   列出默认区域的配置详情
查看指定区域的配置详情

查看指定区域详情可以用--list-all --zone= 或者 --info-zone= 查看public区域的配置,添加的和启用的规则

sudo firewall-cmd --list-all --zone=public   ###  查看public区域的配置详情
sudo firewall-offline-cmd --list-all --zone=public   ###  查看public区域的配置详情
###   查看各预定义区域的配置,添加的和启用的规则
sudo firewall-cmd --zone=drop --list-all
sudo firewall-cmd --info-zone=drop
sudo firewall-offline-cmd --zone=drop --list-all
sudo firewall-offline-cmd --info-zone=drop

sudo firewall-cmd --zone=block --list-all
sudo firewall-cmd --info-zone=block
sudo firewall-offline-cmd --zone=block --list-all
sudo firewall-offline-cmd --info-zone=block

sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --info-zone=public
sudo firewall-offline-cmd --zone=public --list-all
sudo firewall-offline-cmd --info-zone=public

sudo firewall-cmd --zone=external --list-all
sudo firewall-cmd --info-zone=external
sudo firewall-offline-cmd --zone=external --list-all
sudo firewall-offline-cmd --info-zone=external

sudo firewall-cmd --zone=dmz  --list-all
sudo firewall-cmd --info-zone=dmz
sudo firewall-offline-cmd --zone=dmz  --list-all
sudo firewall-offline-cmd --info-zone=dmz

sudo firewall-cmd --zone=work --list-all
sudo firewall-cmd --info-zone=work
sudo firewall-offline-cmd --zone=work --list-all
sudo firewall-offline-cmd --info-zone=work

sudo firewall-cmd --zone=home --list-all
sudo firewall-cmd --info-zone=home
sudo firewall-offline-cmd --zone=home --list-all
sudo firewall-offline-cmd --info-zone=home

sudo firewall-cmd --zone=internal --list-all
sudo firewall-cmd --info-zone=internal
sudo firewall-offline-cmd --zone=internal --list-all
sudo firewall-offline-cmd --info-zone=internal

sudo firewall-cmd --zone=trusted --list-all
sudo firewall-cmd --info-zone=trusted
sudo firewall-offline-cmd --zone=trusted --list-all
sudo firewall-offline-cmd --info-zone=trusted

###   如果没有 --zone 参数, 就显示默认zone 的规则
sudo firewall-cmd --list-all   ###   列出默认区域的配置详情
sudo firewall-offline-cmd --list-all   ###   列出默认区域的配置详情

firewalld的默认zone, 默认区域的概念

当一个网卡接口(例如lo)没有指定zone的时候, 就会使用默认zone的规则 可以用--remove-interface=选项来移除zone的interfaces对应的网卡, 使默认zone生效

查看默认zone是哪个区域
sudo firewall-cmd --get-default-zone   ### 查看默认zone是哪个区域
设置默认区域 --set-default-zone=

设置默认区域,带 --set-default-zone=的语句不用加--permanent就能永久生效, 也不能加--permanent

##  设置默认区域, 默认区域在没有区域时起作用
##  设置默认区域,带 `--set-default-zone=`的语句不用加`--permanent`就能永久生效, 也不能加`--permanent`
###  设置默认区域为drop 
sudo firewall-cmd --set-default-zone=drop
sudo firewall-offline-cmd --set-default-zone=drop
###  设置默认区域为block
sudo firewall-cmd --set-default-zone=block
sudo firewall-offline-cmd --set-default-zone=block
###  设置默认区域为public
sudo firewall-cmd --set-default-zone=public
sudo firewall-offline-cmd --set-default-zone=public
###  设置默认区域为external
sudo firewall-cmd --set-default-zone=external
sudo firewall-offline-cmd --set-default-zone=external
###  设置默认区域为dmz
sudo firewall-cmd --set-default-zone=dmz
sudo firewall-offline-cmd --set-default-zone=dmz
###  设置默认区域为work
sudo firewall-cmd --set-default-zone=work
sudo firewall-offline-cmd --set-default-zone=work
###  设置默认区域为home
sudo firewall-cmd --set-default-zone=home
sudo firewall-offline-cmd --set-default-zone=home
###  设置默认区域为internal
sudo firewall-cmd --set-default-zone=internal
sudo firewall-offline-cmd --set-default-zone=internal
###  设置默认区域为trusted
sudo firewall-cmd --set-default-zone=trusted
sudo firewall-offline-cmd --set-default-zone=trusted
查看默认区域的配置,添加的和启用的规则
###   查看默认区域的配置,添加的和启用的规则
sudo firewall-cmd --list-all
sudo firewall-offline-cmd --list-all
查看默认zone放行了哪些端口
###  查看默认zone启用了哪些服务
sudo firewall-cmd --list-ports
sudo firewall-offline-cmd --list-ports
查看默认zone启用了哪些服务
###  查看默认zone启用了哪些服务
sudo firewall-cmd --list-services
sudo firewall-offline-cmd --list-services

查看当前活动的zone区域,以及对应的网卡
###   查看当前活动的zone区域,以及对应的网卡
sudo firewall-cmd --get-active-zones
查看指定网卡的zone
###  查看lo网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=lo
sudo firewall-offline-cmd --get-zone-of-interface=lo
###  查看ens33网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=ens33
sudo firewall-offline-cmd --get-zone-of-interface=ens33
###  查看enp0s3网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=enp0s3
sudo firewall-offline-cmd --get-zone-of-interface=enp0s3

移除指定网卡的zone

sudo firewall-cmd --permanent --remove-interface=网卡接口名

###  移除ens33网卡接口的zone
sudo firewall-cmd --permanent --remove-interface=ens33
sudo firewall-offline-cmd --remove-interface=ens33
###  查看ens33网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=ens33
sudo firewall-offline-cmd --get-zone-of-interface=ens33
###  移除enp0s3网卡接口的zone
sudo firewall-cmd --permanent --remove-interface=enp0s3
sudo firewall-offline-cmd --remove-interface=enp0s3
###  查看enp0s3网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=enp0s3
sudo firewall-offline-cmd --get-zone-of-interface=enp0s3
给指定网卡添加zone , 要没有zone才能添加

添加网络接口用--add-interface= , 网络接口必须没有zone 有zone的话用修改 --change-interface=

### 先移除ens33网卡接口的zone
sudo firewall-cmd --permanent --remove-interface=ens33
###  给ens33网卡接口添加pulic区域 , 要没有zone才能添加 , 加 --permanent 选项才能长久生效
sudo firewall-cmd --permanent --zone=public --add-interface=ens33
sudo firewall-offline-cmd --zone=public --add-interface=ens33
修改指定网卡的zone

修改网络接口用--change-interface= 网络接口必须有zone,才能修改, 没有zone的话,用添加--add-interface= 修改ens33网卡接口的zone为public

###  修改ens33网卡接口的zone为public , 加 --permanent 选项才能长久生效
sudo firewall-cmd --permanent --zone=public --change-interface=ens33
###  查看ens33网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=ens33

修改ens33网卡接口的zone为internal

###  修改ens33网卡接口的zone为internal , 加 --permanent 选项才能长久生效
sudo firewall-cmd --permanent --zone=internal --change-interface=ens33
###  查看ens33网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=ens33

新增自定zone 删除zone

新增zone和删除zone的语法对比

  • 新增zone : --new-zone=
  • 删除zone : --delete-zone=
  • 新增zone : firewall-cmd --permanent --new-zone=自定义新zone名
  • 删除zone : firewall-cmd --permanent --delete-zone=Zone名称
创建自定义的新区域

创建新区域语法firewall-cmd --new-zone=自定义zone名称 --permanent 创建新区域后要firewall-cmd --reload重新加载,或重启服务才起效

###  创建自定义的新zone , 取名NewCustomZone001 
sudo firewall-cmd --new-zone=NewCustomZone001 --permanent
###  创建新区域后要重新加载或重启服务才起效 , 重新加载firewalld, 不能在firewalld服务处于停止状态的时候执行
sudo firewall-cmd --reload
###  列出所有区域的名称
sudo firewall-cmd --get-zones
###  创建自定义的新zone , 取名NewCustomZone001 
sudo firewall-cmd --new-zone=NewCustomZone001 --permanent
###  列出所有区域的名称
sudo firewall-cmd --get-zones
###  列出所有区域的配置
sudo firewall-cmd --list-all-zones
###  创建新区域后要重新加载或重启服务才起效 , 重新加载firewalld, 不能在firewalld服务处于停止状态的时候执行
sudo firewall-cmd --reload
###  列出所有区域的名称
sudo firewall-cmd --get-zones
###  列出所有区域的配置
sudo firewall-cmd --list-all-zones
删除zone --delete-zone=
###  按名称删除zone
sudo firewall-cmd --permanent --delete-zone=Zone名称

创建删除zone测试

## 创建zone,删除zone 的测试
###  创建自定义的新zone , 取名NewCustomZone001 
sudo firewall-cmd --new-zone=NewCustomZone001 --permanent
###  列出所有区域的名称
sudo firewall-cmd --get-zones
###  创建新区域后要重新加载或重启服务才起效 , 重新加载firewalld, 不能在firewalld服务处于停止状态的时候执行
sudo firewall-cmd --reload
###  列出所有区域的名称
sudo firewall-cmd --get-zones
###  按名称删除zone
sudo firewall-cmd --delete-zone=NewCustomZone001  --permanent
###  列出所有区域的名称
sudo firewall-cmd --get-zones
###  删除区域后要重新加载或重启服务才起效 , 重新加载firewalld, 不能在firewalld服务处于停止状态的时候执行
sudo firewall-cmd --reload
###  列出所有区域的名称
sudo firewall-cmd --get-zones


firewalld的端口操作

firewalld可以

  • 直接在zone中添加端口 --zone= --add-port=
  • 先给zone添加service服务 --zone= --add-service=, 再给service添加端口, --service= --add-port=
###  开发zone的端口
sudo firewall-cmd --permanent --zone=myzone --add-port=80/tcp
sudo firewall-cmd --permanent --zone=myzone --add-port=3306-3399/tcp
sudo firewall-offline-cmd --zone=myzone --add-port=80/tcp
sudo firewall-offline-cmd --zone=myzone --add-port=3306-3399/tcp
###  开发service的端口
sudo firewall-cmd --permanent --service=myservice --add-port=80/tcp
sudo firewall-cmd --permanent --service=myservice --add-port=3306-3399/tcp
sudo firewall-offline-cmd --service=myservice --add-port=80/tcp
sudo firewall-offline-cmd --service=myservice --add-port=3306-3399/tcp
查看firewalld开启的端口
sudo firewall-cmd --list-ports  ###  列出 默认zone 开放的端口
sudo firewall-cmd --list-ports --zone=public  ### 列出 名为public的zone 开放的端口
sudo firewall-offline-cmd --list-ports  ###  列出 默认zone 开放的端口
sudo firewall-offline-cmd --list-ports --zone=public  ### 列出 名为public的zone 开放的端口
sudo firewall-offline-cmd --list-ports --service=mysql  ### 列出 名为mysql的service 开放的端口

注意,即便没有22端口,也不代表22端口没有放行 发现firewalld默认的public区没有开启任何端口,但是却允许ssh, 原因是public区开启了ssh服务 ssh配置模板文件之/usr/lib/firewalld/services/ssh.xml 👇

[root@Vmw-C7-2207-min z]# cat /usr/lib/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
</service>
firewalld 开启关闭端口 , 可以指定zone, 或者指定service

firewalld官方文档---开启端口或服务---Open a Port or Service

  • 加上--permanent则永久生效, 不加则重启失效
  • 加上--zone= 则在指定的区域生效 , 不加则在默认区域default-zone生效 , 等号可以换成空格
  • 加上 --service= 则在指定的服务生效
一个例子, 开放关闭public区域的tcp协议的80端口
###  开放public区域的80端口, 永久生效
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
###  关闭public区域的80端口, 永久生效
sudo firewall-cmd --zone=public --remove-port=80/tcp --permanent
###  开放public区域的80端口, 永久生效
sudo firewall-offline-cmd --zone=public --add-port=80/tcp
###  关闭public区域的80端口, 永久生效
sudo firewall-offline-cmd --zone=public --remove-port=80/tcp
sudo firewall-cmd --reload
  • 添加端口命令:firewall-cmd --zone=public --add-port=80/tcp --permanent 移除端口命令:firewall-cmd --zone=public --remove-port=80/tcp --permanent
  • 说明:开启别的端口只需要将参数中的80修改为所要开放的端口
  • 参数:
    • --zone=public 表示作用域
    • --add-port=80/tcp 表示添加的端口及协议
    • --remove-port=80/tcp 表示移除的端口及协议
    • --permanent 表示永久生效,没有此参数则重启后失效
开放默认区域 default-zone 的端口 , 不加--zone

如果不用--zone指定区域, 就是默认区域

sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=88/udp
sudo firewall-cmd --permanent --add-port 443/tcp
sudo firewall-cmd --permanent --add-port 444/udp

sudo firewall-offline-cmd --add-port=80/tcp
sudo firewall-offline-cmd --add-port=88/udp
sudo firewall-offline-cmd --add-port 443/tcp
sudo firewall-offline-cmd --add-port 444/udp
firewalld开放一些常用端口
sudo firewall-cmd --permanent --add-port=22/tcp
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --permanent --add-port=1443/tcp
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --permanent --add-port=22/tcp --zone=public
sudo firewall-cmd --permanent --add-port=80/tcp --zone=public
sudo firewall-cmd --permanent --add-port=443/tcp --zone=public
sudo firewall-cmd --permanent --add-port=1443/tcp --zone=public
sudo firewall-cmd --permanent --add-port=3306/tcp --zone=public

sudo firewall-offline-cmd --add-port=22/tcp
sudo firewall-offline-cmd --add-port=80/tcp
sudo firewall-offline-cmd --add-port=443/tcp
sudo firewall-offline-cmd --add-port=1443/tcp
sudo firewall-offline-cmd --add-port=3306/tcp
sudo firewall-offline-cmd --add-port=22/tcp --zone=public
sudo firewall-offline-cmd --add-port=80/tcp --zone=public
sudo firewall-offline-cmd --add-port=443/tcp --zone=public
sudo firewall-offline-cmd --add-port=1443/tcp --zone=public
sudo firewall-offline-cmd --add-port=3306/tcp --zone=public


firewalld的service服务

firewalld本身是一项服务, firewalld下还有service服务方便端口配置, 此服务非彼服务 firewalld预定义了一些其它应用或服务的网络通行规则,称为service, 如ssh,http,https , 这些service服务规则以xml的格式保存, 预定义的服务.xml,默认文件夹是 /usr/lib/firewalld/services/

cd /usr/lib/firewalld/services/
ls /usr/lib/firewalld/services/
sudo cat -n /usr/lib/firewalld/services/ssh.xml
sudo cat -n /usr/lib/firewalld/services/ftp.xml
sudo cat -n /usr/lib/firewalld/services/http.xml
sudo cat -n /usr/lib/firewalld/services/https.xml
sudo cat -n /usr/lib/firewalld/services/mssql.xml
sudo cat -n /usr/lib/firewalld/services/mysql.xml
sudo cat -n /usr/lib/firewalld/services/git.xml
sudo cat -n /usr/lib/firewalld/services/svn.xml

下面的结果来自CentOS7 2207 在这里插入图片描述

ls /usr/lib/firewalld/services/
amanda-client.xml        dhcp.xml                 https.xml         ldap.xml               nmea-0183.xml             puppetmaster.xml            smtp.xml                 upnp-client.xml
amanda-k5-client.xml     distcc.xml               http.xml          libvirt-tls.xml        nrpe.xml                  quassel.xml                 snmptrap.xml             vdsm.xml
amqps.xml                dns.xml                  imaps.xml         libvirt.xml            ntp.xml                   radius.xml                  snmp.xml                 vnc-server.xml
amqp.xml                 docker-registry.xml      imap.xml          lightning-network.xml  nut.xml                   redis.xml                   spideroak-lansync.xml    wbem-https.xml
apcupsd.xml              docker-swarm.xml         ipp-client.xml    llmnr.xml              openvpn.xml               RH-Satellite-6-capsule.xml  squid.xml                wbem-http.xml
audit.xml                dropbox-lansync.xml      ipp.xml           managesieve.xml        ovirt-imageio.xml         RH-Satellite-6.xml          ssh.xml                  wsmans.xml
bacula-client.xml        elasticsearch.xml        ipsec.xml         matrix.xml             ovirt-storageconsole.xml  rpc-bind.xml                steam-streaming.xml      wsman.xml
bacula.xml               etcd-client.xml          ircs.xml          mdns.xml               ovirt-vmconsole.xml       rsh.xml                     svdrp.xml                xdmcp.xml
bgp.xml                  etcd-server.xml          irc.xml           minidlna.xml           plex.xml                  rsyncd.xml                  svn.xml                  xmpp-bosh.xml
bitcoin-rpc.xml          finger.xml               iscsi-target.xml  mongodb.xml            pmcd.xml                  rtsp.xml                    syncthing-gui.xml        xmpp-client.xml
bitcoin-testnet-rpc.xml  freeipa-ldaps.xml        isns.xml          mosh.xml               pmproxy.xml               salt-master.xml             syncthing.xml            xmpp-local.xml
bitcoin-testnet.xml      freeipa-ldap.xml         jenkins.xml       mountd.xml             pmwebapis.xml             samba-client.xml            synergy.xml              xmpp-server.xml
bitcoin.xml              freeipa-replication.xml  kadmin.xml        mqtt-tls.xml           pmwebapi.xml              samba-dc.xml                syslog-tls.xml           zabbix-agent.xml
ceph-mon.xml             freeipa-trust.xml        kerberos.xml      mqtt.xml               pop3s.xml                 samba.xml                   syslog.xml               zabbix-server.xml
ceph.xml                 ftp.xml                  kibana.xml        mssql.xml              pop3.xml                  sane.xml                    telnet.xml
cfengine.xml             ganglia-client.xml       klogin.xml        ms-wbt.xml             postgresql.xml            sips.xml                    tftp-client.xml
condor-collector.xml     ganglia-master.xml       kpasswd.xml       murmur.xml             privoxy.xml               sip.xml                     tftp.xml
ctdb.xml                 git.xml                  kprop.xml         mysql.xml              proxy-dhcp.xml            slp.xml                     tinc.xml
dhcpv6-client.xml        gre.xml                  kshell.xml        nfs3.xml               ptp.xml                   smtp-submission.xml         tor-socks.xml
dhcpv6.xml               high-availability.xml    ldaps.xml         nfs.xml                pulseaudio.xml            smtps.xml                   transmission-client.xml

自定义的service.xml在/etc/firewalld/services目录, 一开始为空文件夹

cd /etc/firewalld/services
ls /etc/firewalld/services
firewalld查看哪些服务配置可用
###  查看还有哪些服务可以打开
sudo firewall-cmd --get-services
###  查看还有哪些服务可以打开 老版本可用,新版本不行
sudo firewall-cmd --get-service
firewalld查看默认zone启用了哪些服务
###  查看默认zone启用了哪些服务
sudo firewall-cmd --list-services
firewalld添加已存在的服务, --add-service= , 允许服务对应的数据包通行

添加服务用 --add-service= 服务名 ,

  • 加上 --permanent 才会永久生效 ,
  • --zone=区域名指定将服务添加到哪个区域, 如果不指定--zone=区域,则服务添加到默认的区域 default-zone 指向的区域
sudo firewall-cmd --permanent --zone=dmz --add-service=http
sudo firewall-cmd --permanent --zone=dmz --add-service=https
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=mssql
sudo firewall-cmd --permanent --add-service=mysql
firewalld自定义新service, 用--new-service=

firewalld官方文档 ---添加一个自定义的服务---https://firewalld.org/documentation/howto/add-a-service.html

添加新的空的服务service

sudo firewall-cmd --permanent --new-service=myservice  ### 创建一个新service
或
sudo firewall-offline-cmd --new-service=myservice  ### 创建一个新service
firewall-cmd 命令添加新service, 要加--permanent

firewall-cmd 命令添加一个新的空的服务

sudo firewall-cmd --permanent --new-service=myservice  ### 创建一个新service

此时, 在/etc/firewalld/services目录 , 就多出一个 myservice.xml 文件 用firewall-cmd 命令配置这个服务

firewall-cmd --permanent --service=myservice --set-description=description
firewall-cmd --permanent --service=myservice --set-short=description
firewall-cmd --permanent --service=myservice --add-port=portid[-portid]/protocol
firewall-cmd --permanent --service=myservice --add-protocol=protocol
firewall-cmd --permanent --service=myservice --add-source-port=portid[-portid]/protocol
firewall-cmd --permanent --service=myservice --add-module=module
firewall-cmd --permanent --service=myservice --set-destination=ipv:address[/mask]
firewall-cmd --permanent --service=myservice --set-description=这个服务的描述
firewall-cmd --permanent --service=myservice --set-short=这个服务的简短描述
firewall-cmd --permanent --service=myservice --add-port=1025-65535/udp  ### 连续端口
firewall-cmd --permanent --service=myservice --add-port=30000-65535/tcp  ### 连续端口
firewall-cmd --permanent --service=myservice --add-port=22/tcp  ### 单个端口
firewall-cmd --permanent --service=myservice --add-port=80/tcp
firewall-cmd --permanent --service=myservice --add-port=443/tcp
firewall-cmd --permanent --service=myservice --add-port=1433-1499/tcp
firewall-cmd --permanent --service=myservice --add-port=3306-3399/tcp
firewall-cmd --permanent --service=myservice --add-protocol=protocol
firewall-cmd --permanent --service=myservice --add-source-port=portid[-portid]/protocol
firewall-cmd --permanent --service=myservice --add-module=module
firewall-cmd --permanent --service=myservice --set-destination=ipv4:192.168.168.168/24
firewall-cmd --permanent --service=myservice --set-destination=ipv6:192.168.168.168/24
firewall-offline-cmd 命令添加新service , 不能加--permanent

firewall-offline-cmd 命令添加一个新的空的服务

firewall-offline-cmd --new-service=myservice

firewall-offline-cmd 命令配置这个服务

firewall-offline-cmd --service=myservice --set-description=description
firewall-offline-cmd --service=myservice --set-short=description
firewall-offline-cmd --service=myservice --add-port=portid[-portid]/protocol
firewall-offline-cmd --service=myservice --add-protocol=protocol
firewall-offline-cmd --service=myservice --add-source-port=portid[-portid]/protocol
firewall-offline-cmd --service=myservice --add-module=module
firewall-offline-cmd --service=myservice --set-destination=ipv:address[/mask]
给public区域添加mysql服务, 并将mysql服务通行的端口3306扩充到3399
sudo firewall-cmd --permanent --zone=public --add-service=mysql;
sudo firewall-cmd --permanent --service=mysql --add-port=3306-3399/tcp;    ## 添加服务的端口

sudo firewall-cmd --permanent --service=mysql --remove-port=3306-3399/tcp; ## 删除服务的端口


--delete-service--remove-service

来自帮助文档的说明

  --delete-service=<service>
                       Delete an existing service [P only]
  --remove-service=<service>
                       Remove a service from a zone [P] [Z]
  • --delete-service 是删除一个存在的service
  • --remove-service 是从一个zone中移除service



查看 firewalld 信息的命令汇总

systemctl status firewalld  ###  firewalld服务状态
firewall-cmd --state   ###  firewalld是否启动
firewall-cmd --version   ###  firewalld的版本
sudo firewall-offline-cmd --get-default-zone      ###   查看默认zone是哪个区
sudo firewall-cmd --get-default-zone      ###   查看默认zone是哪个区
sudo firewall-cmd --get-active-zone       ###   查看当前活动的区域
sudo firewall-cmd --list-ports                  ###  查看默认zone的端口
sudo firewall-cmd --list-ports --zone=public    ###  查看public zone的端口


firewalld的 紧急模式

开启紧急模式,拒绝所有数据包,强制关闭所有网络连接
sudo firewall-cmd --panic-on
关闭紧急模式
sudo firewall-cmd --panic-off
查看是否开启了紧急模式
sudo firewall-cmd --query-panic     ###  查看是否开启了紧急模式

例子

sudo firewall-cmd --query-panic     ###  查看是否开启了紧急模式
sudo firewall-cmd --panic-on   ###  开启紧急模式
sudo firewall-cmd --query-panic     ###  查看是否开启了紧急模式
sudo firewall-cmd --panic-off  ###  关闭紧急模式
sudo firewall-cmd --query-panic     ###  查看是否开启了紧急模式

[root@Vmw-C7-2207-min ~]# firewall-cmd --query-panic
firewall-cmd --query-panic
sudo firewall-cmd --panic-off
firewall-cmd --query-panic
no
[root@Vmw-C7-2207-min ~]# sudo firewall-cmd --panic-on
success
[root@Vmw-C7-2207-min ~]# firewall-cmd --query-panic
yes
[root@Vmw-C7-2207-min ~]# sudo firewall-cmd --panic-off
success
[root@Vmw-C7-2207-min ~]# firewall-cmd --query-panic
no




Debug firewalld

查错命令 firewalld --nofork --debug

sudo firewalld --nofork --debug

Firewalld官方文档---查错---Debug firewalld




firewall-cmd的帮助文件

service相关的
firewall-cmd --help | grep "service"
  --get-services       Print predefined services [P]
  --new-service=<service>
                       Add a new service [P only]
  --new-service-from-file=<filename> [--name=<service>]
                       Add a new service from file with optional name [P only]
  --delete-service=<service>
                       Delete an existing service [P only]
  --load-service-defaults=<service>
  --info-service=<service>
                       Print information about a service
  --path-service=<service>
                       Print file path of a service [P only]
  --service=<service> --set-description=<description>
                       Set new description to service [P only]
  --service=<service> --get-description
                       Print description for service [P only]
  --service=<service> --set-short=<description>
                       Set new short description to service [P only]
  --service=<service> --get-short
                       Print short description for service [P only]
  --service=<service> --add-port=<portid>[-<portid>]/<protocol>
                       Add a new port to service [P only]
  --service=<service> --remove-port=<portid>[-<portid>]/<protocol>
                       Remove a port from service [P only]
  --service=<service> --query-port=<portid>[-<portid>]/<protocol>
                       Return whether the port has been added for service [P only]
  --service=<service> --get-ports
                       List ports of service [P only]
  --service=<service> --add-protocol=<protocol>
                       Add a new protocol to service [P only]
  --service=<service> --remove-protocol=<protocol>
                       Remove a protocol from service [P only]
  --service=<service> --query-protocol=<protocol>
                       Return whether the protocol has been added for service [P only]
  --service=<service> --get-protocols
                       List protocols of service [P only]
  --service=<service> --add-source-port=<portid>[-<portid>]/<protocol>
                       Add a new source port to service [P only]
  --service=<service> --remove-source-port=<portid>[-<portid>]/<protocol>
                       Remove a source port from service [P only]
  --service=<service> --query-source-port=<portid>[-<portid>]/<protocol>
                       Return whether the source port has been added for service [P only]
  --service=<service> --get-source-ports
                       List source ports of service [P only]
  --service=<service> --add-module=<module>
                       Add a new module to service [P only]
  --service=<service> --remove-module=<module>
                       Remove a module from service [P only]
  --service=<service> --query-module=<module>
                       Return whether the module has been added for service [P only]
  --service=<service> --get-modules
                       List modules of service [P only]
  --service=<service> --set-destination=<ipv>:<address>[/<mask>]
                       Set destination for ipv to address in service [P only]
  --service=<service> --remove-destination=<ipv>
                       Disable destination for ipv i service [P only]
  --service=<service> --query-destination=<ipv>:<address>[/<mask>]
                       Return whether destination ipv is set for service [P only]
  --service=<service> --get-destinations
                       List destinations in service [P only]
  --list-services      List services added for a zone [P] [Z]
  --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]

--set相关的
firewall-cmd --help | grep "\-\-set"
  --set-log-denied=<value>
  --set-default-zone=<zone>
  --ipset=<ipset> --set-description=<description>
  --ipset=<ipset> --set-short=<description>
  --icmptype=<icmptype> --set-description=<description>
  --icmptype=<icmptype> --set-short=<description>
  --service=<service> --set-description=<description>
  --service=<service> --set-short=<description>
  --service=<service> --set-destination=<ipv>:<address>[/<mask>]
  --set-description=<description>
  --set-target=<target>
  --set-short=<description>
  --set-priority=<priority>
  --helper=<helper> --set-description=<description>
  --helper=<helper> --set-short=<description>
  --helper=<helper> --set-module=<module>
  --helper=<helper> --set-family={ipv4|ipv6|}
--get相关的
firewall-cmd --help | grep "\-\-get"
  --get-log-denied     Print the log denied value
  --get-automatic-helpers
  --get-default-zone   Print default zone for connections and interfaces
  --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>
  --get-zone-of-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  --get-target         Get the zone target [P only] [Z]
  --get-ipset-types    Print the supported ipset types
  --get-ipsets         Print predefined ipsets
  --ipset=<ipset> --get-description
  --ipset=<ipset> --get-short
  --ipset=<ipset> --get-entries
  --icmptype=<icmptype> --get-description
  --icmptype=<icmptype> --get-short
  --icmptype=<icmptype> --get-destinations
  --service=<service> --get-description
  --service=<service> --get-short
  --service=<service> --get-ports
  --service=<service> --get-protocols
  --service=<service> --get-source-ports
  --service=<service> --get-modules
  --service=<service> --get-destinations
  --get-description    Print description for zone [P only] [Z]
  --get-short          Print short description for zone [P only] [Z]
  --get-helpers         Print predefined helpers
  --helper=<helper> --get-description
  --helper=<helper> --get-short
  --helper=<helper> --get-ports
  --helper=<helper> --get-module
  --helper=<helper> --get-family
  --get-all-chains
  --get-chains {ipv4|ipv6|eb} <table>
  --get-all-rules
  --get-rules {ipv4|ipv6|eb} <table> <chain>
  --get-all-passthroughs
  --get-passthroughs {ipv4|ipv6|eb} <arg>...
--list相关的
firewall-cmd --help | grep "\-\-list"
  --list-all-zones     List everything added for or enabled in all zones [P]
  --list-all-policies  List everything added for or enabled in all policies
  --list-all           List everything added for or enabled [P] [Z] [O]
  --list-services      List services added [P] [Z]
  --list-ports         List ports added [P] [Z] [O]
  --list-protocols     List protocols added [P] [Z] [O]
  --list-source-ports  List source ports added [P] [Z] [O]
  --list-icmp-blocks   List Internet ICMP type blocks added [P] [Z] [O]
  --list-forward-ports List IPv4 forward ports added [P] [Z] [O]
  --list-rich-rules    List rich language rules added [P] [Z] [O]
  --list-ingress-zones
  --list-egress-zones
  --list-interfaces    List interfaces that are bound to a zone [P] [Z]
  --list-sources       List sources that are bound to a zone [P] [Z]
  --list-lockdown-whitelist-commands
  --list-lockdown-whitelist-contexts
  --list-lockdown-whitelist-uids
  --list-lockdown-whitelist-users
--new相关的
firewall-cmd --help | grep "\-\-new"
  --new-zone=<zone>    Add a new zone [P only]
  --new-zone-from-file=<filename> [--name=<zone>]
  --new-policy=<policy>
  --new-policy-from-file=<filename> [--name=<policy>]
  --new-ipset=<ipset> --type=<ipset type> [--option=<key>[=<value>]]..
  --new-ipset-from-file=<filename> [--name=<ipset>]
  --new-icmptype=<icmptype>
  --new-icmptype-from-file=<filename> [--name=<icmptype>]
  --new-service=<service>
  --new-service-from-file=<filename> [--name=<service>]
  --new-helper=<helper> --module=<module> [--family=<family>]
  --new-helper-from-file=<filename> [--name=<helper>]
--add相关的
firewall-cmd --help | grep "\-\-add"
  --ipset=<ipset> --add-entry=<entry>
  --ipset=<ipset> --add-entries-from-file=<entry>
  --icmptype=<icmptype> --add-destination=<ipv>
  --service=<service> --add-port=<portid>[-<portid>]/<protocol>
  --service=<service> --add-protocol=<protocol>
  --service=<service> --add-source-port=<portid>[-<portid>]/<protocol>
  --service=<service> --add-module=<module>
  --add-service=<service>
  --add-port=<portid>[-<portid>]/<protocol>
  --add-protocol=<protocol>
  --add-source-port=<portid>[-<portid>]/<protocol>
  --add-icmp-block=<icmptype>
  --add-icmp-block-inversion
  --add-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
  --add-masquerade     Enable IPv4 masquerade for a zone [P] [Z] [T]
  --add-rich-rule=<rule>
  --add-interface=<interface>
  --add-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  --helper=<helper> --add-port=<portid>[-<portid>]/<protocol>
  --add-chain {ipv4|ipv6|eb} <table> <chain>
  --add-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
  --add-passthrough {ipv4|ipv6|eb} <arg>...
  --add-lockdown-whitelist-command=<command>
  --add-lockdown-whitelist-context=<context>
  --add-lockdown-whitelist-uid=<uid>
  --add-lockdown-whitelist-user=<user>
--change相关的
firewall-cmd --help | grep "\-\-change"
  --change-interface=<interface>
  --change-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
--delete相关的
 firewall-cmd --help | grep "\-\-delete"
  --delete-zone=<zone> Delete an existing zone [P only]
  --delete-ipset=<ipset>
  --delete-icmptype=<icmptype>
  --delete-service=<service>
  --delete-helper=<helper>
--remove相关的
firewall-cmd --help | grep "\-\-remove"
  --ipset=<ipset> --remove-entry=<entry>
  --ipset=<ipset> --remove-entries-from-file=<entry>
  --icmptype=<icmptype> --remove-destination=<ipv>
  --service=<service> --remove-port=<portid>[-<portid>]/<protocol>
  --service=<service> --remove-protocol=<protocol>
  --service=<service> --remove-source-port=<portid>[-<portid>]/<protocol>
  --service=<service> --remove-helper=<helper>
  --service=<service> --remove-destination=<ipv>
  --service=<service> --remove-include=<service>
  --remove-service=<service>
  --remove-port=<portid>[-<portid>]/<protocol>
  --remove-protocol=<protocol>
  --remove-source-port=<portid>[-<portid>]/<protocol>
  --remove-icmp-block=<icmptype>
  --remove-forward-port=port=<portid>[-<portid>]:proto=<protocol>[:toport=<portid>[-<portid>]][:toaddr=<address>[/<mask>]]
  --remove-masquerade  Disable IPv4 masquerade [P] [Z] [O]
  --remove-rich-rule=<rule>
  --remove-icmp-block-inversion
  --remove-forward     Disable forwarding of packets between interfaces and
  --remove-ingress-zone=<zone>
  --remove-egress-zone=<zone>
  --remove-interface=<interface>
  --remove-source=<source>[/<mask>]|<MAC>|ipset:<ipset>
  --helper=<helper> --remove-port=<portid>[-<portid>]/<protocol>
  --remove-chain {ipv4|ipv6|eb} <table> <chain>
  --remove-rule {ipv4|ipv6|eb} <table> <chain> <priority> <arg>...
  --remove-rules {ipv4|ipv6|eb} <table> <chain>
  --remove-passthrough {ipv4|ipv6|eb} <arg>...
  --remove-lockdown-whitelist-command=<command>
  --remove-lockdown-whitelist-context=<context>
  --remove-lockdown-whitelist-uid=<uid>
  --remove-lockdown-whitelist-user=<user>

--info相关的

firewall-cmd --help | grep "\-\-info"
  --info-zone=<zone>   Print information about a zone
  --info-policy=<policy>
  --info-ipset=<ipset> Print information about an ipset
  --info-icmptype=<icmptype>
  --info-service=<service>
  --info-helper=<helper> Print information about an helper

@[TOC]







ufw (Debian,Ubuntu等)

ufw启用防火墙

sudo ufw enable

ufw禁用防火墙

sudo ufw disable

ufw禁用防火墙后, ufw服务还是活动的, 只是在退出状态 在这里插入图片描述 所以不能用 systemctl start ufw 启动防火墙

ufw查看防火墙状态

sudo ufw status

ufw打开端口22

sudo ufw allow 22

ufw禁用端口22

sudo ufw deny 22

外来访问默认值: 允许/拒绝

默认允许外来访问

sudo ufw default allow

默认拒绝外来访问

sudo ufw default deny







附加扩展知识

systemctl 的一些扩展用法

查看已启用的操作系统服务列表:systemctl list-unit-files|grep enabled

sudo systemctl list-unit-files|grep enabled   ###  查看已启用的操作系统服务列表

查看启动失败的操作系统服务列表:systemctl --failed

sudo systemctl --failed   ###  查看启动失败的操作系统服务列表

netstat 的一些用法

netstat

  • -a所有类State , -l监听类State
  • -n 不进行域名解析, 不显示域名,显示为IP 数字
  • -p 显示 PID 和 Program name

netstat -anp列出所有类 netstat -lnp列出监听类

netstat 查看监听的端口, 加 -l, 例如netstat -lnp

netstat -lnp  ###  查看监听的端口

netstat 查看监听的TCP端口, 加-lt, 例如netstat -lnpt

netstat -lnpt  ###  查看监听的TCP端口

netstat 查看监听的UDP端口, 加-lu , 例如netstat -lnpu

netstat -lnpu  ###  查看监听的UDP端口