Contents
  1. 1. IPtable
    1. 1.1. iptables传输数据包的过程:
    2. 1.2. 4个表: 优先顺序Raw——mangle——nat——filter
    3. 1.3. 5个链:链(chains)是数据包传播的路径,每一条链是众多规则中的一个检查清单,每一条链中可以有一条或数条规则:
    4. 1.4. 原则
    5. 1.5. 命令
      1. 1.5.1. rule-specification
      2. 1.5.2. -m常用模块
      3. 1.5.3. 备份还原
      4. 1.5.4. 限制访问ip, 按顺序先允许个别,后拒绝所有
  2. 2. 数据节点
    1. 2.1. 主要成员节点 增加规则
    2. 2.2. 主节点(在主要成员节点基础上增加)
    3. 2.3. kafka
  3. 3. 端口转发
  4. 4. Firewalld
    1. 4.0.1. firewalld区域概念
    2. 4.0.2. 配置
  • 5. knockd
  • 目前Linux系统的防火墙类型主要有两种:分别是iptables和firewalld,他们不是真正的防火墙,是指用来定义防火墙规则功能的”防火墙管理工具/程序”,将定义好的规则交由内核中的netfilter即网络过滤器来读取,从而真正实现防火墙功能。

    在配置防火墙时,不建议两种配置方法结合使用(建议只使用其中的一种)

    • iptables-静态防火墙
      • 早期的Linux系统中默认使用的是iptables防火墙,配置文件在/etc/sysconfig/iptables,主要工作在网络层
      • 该防火墙使用链式规则,只可以过滤互联网的数据包,无法过滤从内网到内网的数据包
      • iptables只可以通过命令行进行配置
      • iptables默认是允许所有,需要通过拒绝去做限制
      • iptables在修改了规则之后必须得全部刷新才可以生效,还会丢失连接(无法守护进程)
    • firewalld-动态防火墙
      • 取代了之前的iptables防火墙,配置文件在/usr/lib/firewalld和/etc/fiewalld中,主要工作在网络层
      • 新增区域概念,不仅可以过滤互联网的数据包,也可以过滤内网的数据包
      • firewalld不仅可以通过命令行进行配置,也可以通过图形化界面配置
      • firewalld默认是拒绝所有,需要通过允许去放行
      • firewalld可以动态修改单条规则,动态管理规则集(允许更新规则而不破环现有会话和连接,可以守护进程)

    IPtable

    iptables传输数据包的过程:

    1. 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
    2. 如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。
    3. 如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右移动,经过FORWARD链,然后到达POSTROUTING链输出。

    4个表: 优先顺序Raw——mangle——nat——filter

    • filter表:包过滤:INPUT、FORWARD、OUTPUT
    • nat表:网络地址转换(IP、端口):PREROUTING、POSTROUTING、OUTPUT
    • mangle表:包重构(修改数据包的服务类型、TTL、并且可以配置路由实现QOS内核模块):PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
    • raw表:决定数据包是否被状态跟踪机制处理:OUTPUT、PREROUTING

    5个链:链(chains)是数据包传播的路径,每一条链是众多规则中的一个检查清单,每一条链中可以有一条或数条规则:

    • PREROUTING:nat,mangle,raw
    • FORWARD:mangle,filter
    • INPUT:mangle,filter
    • OUTPUT:nat,mangle,raw,filter
    • POSTROUTING:nat,mangle,raw

    原则

    INPUT严防,OUTPUT预防;
    细粒度在前,粗粒度在后;

    命令

    iptables
    https://zhuanlan.zhihu.com/p/501380475
    https://ipset.netfilter.org/iptables.man.html

    Each table contains built-in chains and user-defined chains. INPUT FORWARD OUTPUT
    Each chain is a list of rules
    Each rule specifies what to do with a packet that matches. ACCEPT, DROP or RETURN

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    --append  -A chain		Append to chain
    --check -C chain Check for the existence of a rule
    --delete -D chain Delete matching rule from chain
    --delete -D chain rulenum Delete rule rulenum (1 = first) from chain
    --insert -I chain [rulenum] Insert in chain as rulenum (default 1=first)
    --replace -R chain rulenum Replace rule rulenum (1 = first) in chain
    --list -L [chain [rulenum]] List the rules in a chain or all chains
    --list-rules -S [chain [rulenum]] Print the rules in a chain or all chains
    --flush -F [chain] Delete all rules in chain or all chains
    --zero -Z [chain [rulenum]] Zero counters in chain or all chains
    --new -N chain Create a new user-defined chain
    --delete-chain -X [chain] Delete a user-defined chain
    --policy -P chain target Change policy on chain to target(built-in only)
    --rename-chain -E old-chain new-chain Change chain name, (moving any references)

    rule-specification

    -p, –protocol protocol: tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or the special keyword “all”
    -s, –source address[/mask][,…] 写多个地址最后会拆分多条规则
    -d, –destination address[/mask][,…]
    -m, –match match
    -j, –jump target: ACCEPT DROP REJECT

    其中REJECT支持
    –reject-with [type] : icmp-net-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unreachable, icmp-net-prohibited, icmp-host-prohibited, or icmp-admin-prohibited, tcp-reset

    https://blog.csdn.net/sbdx/article/details/117002045
    对外用-j DROP,延长攻击时长
    对内用-j REJECT –reject-with tcp-reset好,节约时间,节约带宽。

    https://ipset.netfilter.org/iptables-extensions.man.html

    -m常用模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    -m comment 注释
    --comment "my local LAN"
    -m multiport 适用于tcp, udp, udplite, dccp and sctp. Up to 15 ports can be specified. A port range (port:port) counts as two ports.
    --ports port[,port|,port:port]...
    --source-ports,--sports port[,port|,port:port]...
    --destination-ports,--dports port[,port|,port:port]...
    -m iprange
    --src-range from[-to]
    --dst-range from[-to]
    -m tcp can be used if `--protocol tcp' is specified
    --source-port,--sport port[:port]
    --destination-port,--dport port[:port]

    备份还原

    1
    2
    3
    4
    5
    iptables -nvL INPUT --line-number
    iptables-save >/data/tmp/iptables.bak
    iptables-restore </data/tmp/iptables.bak

    可以导出后编辑规则,导入UTF8编码的文本
    • 带行号显示 iptables -nL INPUT –line-numbers -v
    • 配置命令 iptables -S OUTPUT
    • 删除 iptables -D INPUT 8 按序号删除风险高,容易错位,必须从下至上操作
      • iptables -A INPUT -s 192.168.1.5 -j DROP
      • iptables -D INPUT -s 192.168.1.5 -j DROP
    • 插入到指定位置前 iptables -I INPUT 125 -s <address> -j ACCEPT
    • 0流量规则 iptables -S INPUT -v |grep -E "c\ 0\ 0"
      • 删除 iptables -S INPUT -v |grep -E "c\ 0\ 0" |sed 's/-c\ 0\ 0//' | cut -d " " -f 2- | xargs -rL1 iptables -D
    • 排除连接的ip端口 tcpdump -c 100 -i bond1 -nntq -P in '( (not src net 10.27.48) and (not src net 10.17.14) and (!src port 9092) and (dst portrange 30000-49999 ) and (tcp) )'
      • 简化结果 head test|awk -F'[ :]' '{print $2, $4}'|sort -u
      • 获取端口|awk -F'.' '{print $NF}'|xargs -I{} lsof -i:{}
      • 获取入访IP|awk -F'.' '{print $1"."$2"."$3"."$4}' |sort -u

    批量删除规则:

    1. 检查确认 iptables -S OUTPUT -v | grep 192 | cut -d " " -f 2- | xargs -rL1 echo iptables -D
    2. 执行 iptables -S OUTPUT | grep 192 | cut -d " " -f 2- | xargs -rL1 iptables -D

    限制访问ip, 按顺序先允许个别,后拒绝所有

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    # 清除规则
    iptables -F

    # 本地回路(若只对需保护的端口 放通+drop 则不需要配置此项)
    iptables -I INPUT -i lo -j ACCEPT
    iptables -I OUTPUT -o lo -j ACCEPT

    # 允许主动出访连接的入访
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

    # 服务器初始开放端口(注意ssh)
    iptables -I INPUT -p icmp -j ACCEPT -m comment --comment "允许ping"
    iptables -I INPUT -p tcp --dport 22 -j ACCEPT
    iptables -I INPUT -p tcp --dport 10050 -j ACCEPT -m comment --comment "zabbix"

    # 跳板机/网页界面放通
    iptables -R INPUT 6 -p tcp -s 10.17.14.0/24 -m multiport --dport 9870,8088,18080,19888,12345,8090,9090,3000 -j ACCEPT -m comment --comment "web"

    # 集群网段放通1 (任意端口)
    iptables -A INPUT -m iprange --src-range 10.27.48.1-10.27.48.43 -j ACCEPT

    # 集群网段放通2 自定义链(需要限定部分端口)
    ## 创建
    iptables -t filter -N CLUSTER # filter表类型, 自定义链名称
    iptables -I CLUSTER -s 10.17.41.127 -j ACCEPT
    iptables -I CLUSTER -s 10.17.41.129 -j ACCEPT
    iptables -I CLUSTER -s 10.3.4.191 -j ACCEPT
    iptables -I CLUSTER -s 10.3.4.192 -j ACCEPT
    iptables -I CLUSTER -s 10.3.4.194 -j ACCEPT
    iptables -I CLUSTER -s 10.3.4.195 -j ACCEPT
    iptables -I CLUSTER -s 10.17.41.20 -j ACCEPT
    iptables -I CLUSTER -s 10.17.41.21 -j ACCEPT
    iptables -I CLUSTER -s 10.17.41.24 -j ACCEPT
    iptables -I CLUSTER -s 10.17.41.25 -j ACCEPT

    ## 指定哪些连接进入自定义链
    ## 替换规则
    iptables -R INPUT ? -p tcp -m multiport --dports 9000,50010,50020,8080 -j CLUSTER
    iptables -I INPUT ? -p tcp -m multiport --dports 9000,50010,50020,8080 -j DROP
    ## 改名
    iptables -E CLUSTER HDFS
    ## 删除
    iptables -X HDFS


    # 默认拒绝(危险,必须确保必要连接已允许)
    iptables -P INPUT DROP

    # 限制输出(注意ssh),可选
    iptables -A OUTPUT -p all -d 10.0.0.0/8 -j ACCEPT
    iptables -P OUTPUT DROP

    # 查看流量
    iptables -nvL --line-number

    数据节点

    1
    2
    3
    4
    5
    6
    d=`date +%Y%m%d`
    sudo iptables-save > iptable${d}
    vi iptable${d}

    sudo iptables-restore < iptable${d}
    sudo iptables -nL
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    :HDFS - [0:0]
    :WEBUI - [0:0]
    :PORT_22 - [0:0]

    -A INPUT -i lo -j ACCEPT
    -A INPUT -m iprange --src-range 10.27.48.1-10.27.48.49 -j ACCEPT
    -A INPUT -m iprange --src-range 10.27.5.3-10.27.5.7 -j ACCEPT
    -A INPUT -m iprange --src-range 10.3.60.121-10.3.60.130 -j ACCEPT
    -A INPUT -m iprange --src-range 10.27.3.46-10.27.3.83 -j ACCEPT

    -A INPUT -p tcp -m multiport --dports 50010 -j HDFS -m comment --comment "访问HDFS"
    -A HDFS -m iprange --src-range 10.3.16.118-10.3.16.126 -m comment --comment "中台萝岗海豚" -j ACCEPT
    -A HDFS -m iprange --src-range 10.19.85.1-10.19.85.8 -m comment --comment "中台移动云海豚" -j ACCEPT
    -A HDFS -s 10.17.41.125/32 -m comment --comment "海豚worker" -j ACCEPT
    -A HDFS -s 10.169.129.0/24 -m comment --comment "中台萝岗" -j ACCEPT
    -A HDFS -s 10.136.102.0/24 -m comment --comment "中台移动云" -j ACCEPT
    -A HDFS -j DROP

    -A INPUT -p tcp -m multiport --dports 50075,8042 -j WEBUI -m comment --comment "访问webUI"
    -A WEBUI -s 10.19.80.0/24 -m comment --comment "4A移动云广州" -j ACCEPT
    -A WEBUI -s 10.17.14.0/24 -m comment --comment "4A萝岗业务域" -j ACCEPT
    -A WEBUI -s 10.33.7.0/24 -m comment --comment "4A北方业务域" -j ACCEPT
    -A WEBUI -j DROP

    -A INPUT -p tcp -m multiport --dports 20:22 -j PORT_22 -m comment --comment "访问20-22端口"
    -A PORT_22 -m iprange --src-range 10.168.24.1-10.168.24.3 -m comment --comment "移动云交付" -j ACCEPT
    -A PORT_22 -s 10.50.6.0/24 -m comment --comment "4A移动云无锡" -j ACCEPT
    -A PORT_22 -s 10.35.26.0/24 -m comment --comment "4A移动云呼和浩特" -j ACCEPT
    -A PORT_22 -s 10.19.80.0/24 -m comment --comment "4A移动云广州" -j ACCEPT
    -A PORT_22 -s 10.17.14.0/24 -m comment --comment "4A萝岗业务域" -j ACCEPT
    -A PORT_22 -s 10.169.0.0/24 -m comment --comment "4A萝岗管理域" -j ACCEPT
    -A PORT_22 -s 10.33.7.0/24 -m comment --comment "4A北方业务域" -j ACCEPT
    -A PORT_22 -s 10.173.0.0/24 -m comment --comment "4A北方管理域" -j ACCEPT
    -A PORT_22 -j DROP

    -A INPUT -p tcp -m multiport --dports 50020,13562,8040 -m comment --comment "internal" -j DROP
    -A INPUT -p tcp -m tcp --dport 4040:4069 -m comment --comment "spark flink动态端口" -j DROP
    -A INPUT -p tcp -m tcp --dport 9999 -m comment --comment "移动云python漏洞" -j DROP
    -A INPUT -p tcp -m tcp --dport 9100 -m comment --comment metrics -j DROP
    -A INPUT -p tcp -m tcp --dport 40000 -m comment --comment devops_agent -j DROP

    COMMIT

    主要成员节点 增加规则

    48.4/5/6

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # zk
    -A INPUT -s 10.17.41.125/32 -p tcp -m multiport --dports 2181 -m comment --comment "zk 海豚worker" -j ACCEPT
    -A INPUT -p tcp -m multiport --dports 2181,7000,8381,2888,3888 -m comment --comment zookeeper -j DROP

    # hadoop JournalNode
    -A INPUT -p tcp -m tcp --dport 8480 -m comment --comment JournalNode -j DROP
    -A INPUT -p tcp -m tcp --dport 8485 -m comment --comment JournalNode -j DROP
    -A INPUT -p tcp -m tcp --dport 9083 -m comment --comment HiveMetaStore -j DROP

    # dolphins
    -A INPUT -p tcp -m tcp --dport 1234 -m comment --comment ds_worker -j DROP
    -A INPUT -p tcp -m tcp --dport 50051 -m comment --comment ds_logger -j DROP

    ## (optional)thrift
    -A INPUT -p tcp -m tcp --dport 10198:10199 -m comment --comment thrift -j DROP

    ## (optional)中台
    -A INPUT -s 10.136.102.0/24 -p tcp -m multiport --dports 4040:4069 -j ACCEPT

    主节点(在主要成员节点基础上增加)

    48.1/7

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # hadoop
    WEBUI + (ng转发后的)50170,8089,18088
    HDFS + 9000

    -A INPUT -p tcp -m tcp --dport 8088 -m comment --comment yarn -j DROP
    -A INPUT -p tcp -m tcp --dport 50070 -m comment --comment hdfs -j DROP
    -A INPUT -p tcp -m tcp --dport 8050 -m comment --comment NameNode -j DROP
    -A INPUT -p tcp -m tcp --dport 8019 -m comment --comment DFSZKFailoverController -j DROP
    -A INPUT -p tcp -m multiport --dports 8030:8033 -m comment --comment ResourceManager -j DROP


    # zeppelin/jupyter
    WEBUI + 8090,15588

    # dolphins
    WEBUI + 12345
    -A INPUT -p tcp -m tcp --dport 5678 -m comment --comment ds_master -j DROP


    # mysql
    :MYSQL - [0:0]

    -A INPUT -p tcp -m multiport --dports 3306 -j MYSQL -m comment --comment "访问mysql"
    -A MYSQL -s 10.17.41.125/32 -m comment --comment "海豚worker" -j ACCEPT
    -A MYSQL -s 10.153.97.137/32 -m comment --comment "数据资产安全管理" -j ACCEPT
    -A MYSQL -m iprange --src-range 10.19.85.1-10.19.85.6 -m comment --comment "中台元数据信息接入" -j ACCEPT
    -A MYSQL -j DROP

    kafka

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    :KAFKA - [0:0]

    -A INPUT -p tcp -m multiport --dports 9092 -j KAFKA -m comment --comment "访问KAFKA"
    -A KAFKA -m iprange --src-range 10.17.49.11-10.17.49.14 -m comment --comment kafka -j ACCEPT
    -A KAFKA -m iprange --src-range 10.17.49.170-10.17.49.172 -m comment --comment kafka -j ACCEPT
    -A KAFKA -m iprange --src-range 10.17.49.181-10.17.49.184 -m comment --comment "kafka生产" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.17.49.28-10.17.49.30 -m comment --comment "kafka测试" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.19.6.144-10.19.6.147 -m comment --comment "建设小组_移动云" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.3.4.114-10.3.4.115 -m comment --comment kafka -j ACCEPT
    -A KAFKA -m iprange --src-range 10.3.4.120-10.3.4.122 -m comment --comment kafka -j ACCEPT
    -A KAFKA -m iprange --src-range 10.3.4.49-10.3.4.55 -m comment --comment kafka -j ACCEPT
    -A KAFKA -m iprange --src-range 10.35.0.126-10.35.0.129 -m comment --comment "运营平台" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.35.0.135-10.35.0.136 -m comment --comment "运营平台" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.35.0.15-10.35.0.26 -m comment --comment kafka -j ACCEPT
    -A KAFKA -m iprange --src-range 10.35.0.210-10.35.0.215 -m comment --comment "灰度环境" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.35.0.214-10.35.0.215 -m comment --comment "kafka灰度" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.35.0.84-10.35.0.86 -m comment --comment "运营平台" -j ACCEPT
    -A KAFKA -m iprange --src-range 10.35.0.92-10.35.0.97 -m comment --comment "kafka灰度" -j ACCEPT
    -A KAFKA -s 10.136.102.0/24 -j ACCEPT
    -A KAFKA -s 10.17.41.125/32 -j ACCEPT
    -A KAFKA -s 10.3.4.118/32 -m comment --comment kafka -j ACCEPT
    -A KAFKA -s 10.3.4.56/32 -m comment --comment kafka -j ACCEPT
    -A KAFKA -s 10.35.0.0/24 -m comment --comment "运营平台网段" -j ACCEPT
    -A KAFKA -s 10.35.0.90/32 -m comment --comment "运营平台" -j ACCEPT
    -A KAFKA -s 10.35.0.97/32 -m comment --comment kafka -j ACCEPT
    -A KAFKA -s 10.35.24.0/24 -m comment --comment kafka -j ACCEPT
    -A KAFKA -j DROP

    端口转发

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    sysctl -w net.ipv4.ip_forward=1 
    # less /etc/sysctl.conf
    #net.ipv4.ip_forward=1

    sudo iptables -t nat -I PREROUTING -p tcp -s 10.3.(允许的ip)--dport 8848 -j DNAT --to-destination 36.xxxx:32222

    sudo iptables -t nat -I POSTROUTING -p tcp -d 36.xxxx --dport 32222 -j MASQUERADE

    #sudo iptables -I FORWARD -p tcp -d 36.xxxx --dport 32222 -j ACCEPT

    #iptables -A INPUT -m iprange --src-range 10.27.48.1-10.27.48.43 -j ACCEPT
    #iptables -A INPUT -p tcp -m multiport --dports 8848 -j DROP

    iptables -t nat -nL PREROUTING --line-number
    iptables -t nat -nL POSTROUTING --line-number
    iptables -t filter -nL FORWARD

    Firewalld

    frewalld是服务名称,firewall-cmd和firewall-config是配置工具名称

    firewall-cmd  基于命令行配置

    firewall-config基于图形化界面配置(这两个配置方式实时同步)

    runtime运行时: 修改规则马上生效,但是是临时生效 [不建议]
    permanent持久配置: 修改后需要reload重载才会生效 [强烈推荐]

    1
    2
    3
    4
    5
    6
    7
    #禁用旧版防火墙服务
    [root@firewalld ~]# systemctl mask iptables
    [root@firewalld ~]# systemctl mask ip6tables

    #启动firewalld防火墙, 并加入开机自启动服务
    [root@firewalld ~]# systemctl start firewalld
    [root@firewalld ~]# systemctl enable firewalld

    firewalld区域概念

    数据包到达防火墙匹配规则:
    绑定源地址的区域规则>网卡绑定的区域规则>默认区域的规则

    默认所有网卡都是public区域,可以根据需要将网卡设置为不同的区域

    zone 区域 规则
    Trust 信任区域 允许所有的数据包流入与流出
    Public 公共区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、dhcpv6-client服务相关,则允许流量
    External 外部区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量
    Home 家庭区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、mdns、ipp-client、samba-client与dhcpv6-client服务相关,则允许流量
    Internal 内部区域 同home区域
    Work 工作区域 拒绝流入的流量,除非与流出的流量数相关;而如果流量与ssh、ipp-client与dhcpv6-client服务相关,则允许流量
    Dmz 隔离区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量
    Block 限制区域 拒绝流入的流量,除非与流出的流量相关(有回应)
    Drop 丢弃区域 拒绝流入的流量,除非与流出的流量相关(无回应)
    zone配置文件:block.xml、dmz.xml、drop.xml、external.xml、 home.xml、internal.xml、public.xml、trusted.xml、work.xml,他们都保存在“/usr/lib/firewalld/zones/”目录下。

    cat /etc/services 保存的协议类型和端口号

    配置

    默认临时生效,–permanent 表示此配置加入到永久生效

    或者在配置结束后执行此命令 firewall-cmd --runtime-to-permanent将临时更改为永久

    永久配置完成后需要立即同步 firewall-cmd --reload 立即同步永久配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    #### 查看默认区域并进行更改

    firewall-cmd --get-zones 查询可用的区域
    firewall-cmd --get-default-zone 查询默认区域的名称
    firewall-cmd --get-active-zone 显示当前正在使用的区域与网卡名称
    firewall-cmd --set-default-zone=trusted 设置默认区域为trusted区域

    #### 将网卡/子网与区域绑定(允许/拒绝此子网通过)

    firewall-cmd --zone=drop --add-source=192.168.20.0/24 将此子网与drop区域绑定(拒绝从此子网发来的流量)
    firewall-cmd --zone=trusted --add-interface=ens160 将此网卡与trusted区域绑定(允许从此网卡发来的流量)

    --remove-source 删除子网与区域的绑定
    --change-source 更改子网与区域的绑定

    #### 配置区域允许/拒绝的协议/端口号

    firewall-cmd --list-all 显示当前区域的端口号、网卡、服务等信息
    --list-all-zones 显示所有区域的
    firewall-cmd --get-services 列举出来当前所有被允许的协议
    firewall-cmd --zone=public --add-service http 配置public区域允许通过http协议
    --remove-service ssh 拒绝通过ssh协议
    --add-port=123/tcp 允许通过tcp的123端口
    --remove-port=123/tcp 拒绝通过tcp的123端口


    #### 配置协议端口转换(端口映射)

    firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.10.1

    将192.168.10.1主机的tcp 22端口号转为888端口号(public区域接收ssh)

    --remove-forward-port 删除此端口映射

    #### 其它配置
    --panic-on  紧急模式,切断一切的网络连接(特殊情况去使用)
    --panic-off  恢复一切的网络连接

    #### 配置富规则rich(更复杂、更详细的防火墙策略配置)
    优先级最高(高于默认规则,两个并不冲突),能够根据源目地址、端口号来限制用户

    firewall-cmd --zone=public --list-rich-rule 显示public区域已经配置的富规则

    firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.100.1/24" service name="ssh" accept" 允许来自192.168.100.1的主机访问22端口

    --add-rich-rule 添加一个富规则
    --remove-rich-rule 删除一个富规则
    accept/reject 允许/拒绝访问

    常用

    1
    2
    3
    4
    5
    6
    7
    sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.17.14.0/24" accept"
    sudo firewall-cmd --reload
    sudo firewall-cmd --zone=trusted --list-rich-rule

    sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.x.x.x" accept"


    knockd

    端口敲门服务,即:knockd服务。该服务通过动态的添加iptables规则来隐藏系统开启的服务,使用自定义的一系列序列号来“敲门”,使系统开启需要访问的服务端口,才能对外访问。不使用时,再使用自定义的序列号来“关门”,将端口关闭,不对外监听