• centos7_firewall


    CentOS7开始引入firewall服务,支持IPV4和IPV6并支持网桥。 使用firewall-cmd(command)和firewall-config(GUI)管理。

    上图显示出firewall与iptables的关联关系。 

    IPTABLES和Firewalld区别

    • iptables 默认是允许所有,而firewalld默认是禁止所有
    • firewalld可以动态修改单条规则,而不需要像iptables那样,在修改了规则后必须得全部刷新才可以生效;

    安装firewall

    [root@localhost ~]# yum install firewalld -y
    #安装firewalld service
    [root@localhost ~]# yum install firewall-config
    # 安装图形界面

    [root@localhost ~]# systemctl start firewalld
    [root@localhost ~]# systemctl enable firewalld
    Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
    Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service

    firewalld中常用的区域名称及策略规则

    区域(zone) 默认规则策略
    trusted 允许所有的数据包
    home 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、mdns、ipp-client、amba-client与dhcpv6-client服务相关,则允许流量
    internal 等同于home区域
    work 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、ipp-client与dhcpv6-client服务相关,则允许流量
    public 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、dhcpv6-client服务相关,则允许流量
    external 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量
    dmz 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量
    block 拒绝流入的流量,除非与流出的流量相关
    drop 拒绝流入的流量,除非与流出的流量相关

    firewalld中的过滤规则

    规则 描述
    source 根据源地址过滤
    interface 根据网卡过滤
    service 根据服务名过滤
    port 根据端口过滤
    icmp-block icmp 报文过滤,按照 icmp 类型配置
    masquerade ip 地址伪装
    forward-port 端口转发
    rule 自定义规则

    其中,过滤规则的优先级别遵循如下顺序:1.source 2.interface 3firewalld.conf

     firewall-cmd命令中使用的参数以及作用

    参数 作用
    --get-default-zone 查询默认的区域名称
    --set-default-zone=<区域名称> 设置默认的区域,使其永久生效
    --get-zones 显示可用的区域
    --get-services 显示预先定义的服务
    --get-active-zones 显示当前正在使用的区域与网卡名称
    --add-source= 将源自此IP或子网的流量导向指定的区域
    --remove-source= 不再将源自此IP或子网的流量导向某个指定区域
    --add-interface=<网卡名称> 将源自该网卡的所有流量都导向某个指定区域
    --change-interface=<网卡名称> 将某个网卡与区域进行关联
    --list-all 显示当前区域的网卡配置参数、资源、端口以及服务等信息
    --list-all-zones 显示所有区域的网卡配置参数、资源、端口以及服务等信息
    --add-service=<服务名> 设置默认区域允许该服务的流量
    --add-port=<端口号/协议> 设置默认区域允许该端口的流量
    --remove-service=<服务名> 设置默认区域不再允许该服务的流量
    --remove-port=<端口号/协议> 设置默认区域不再允许该端口的流量
    --reload 让“永久生效”的配置规则立即生效,并覆盖当前的配置规则
    --panic-on 开启应急状况模式
    --panic-off 关闭应急状况模式

    命令

    1. 查看规则

    [root@localhost ~]# firewall-cmd --state
    running
    # 查看运行状态
    [root@localhost ~]# firewall-cmd --get-active-zones
    public
      interfaces: ens33
    #查看已被激活的 Zone 信息
    [root@localhost ~]# firewall-cmd --list-all-zones
    #查看所有zone
    [root@localhost ~]# firewall-cmd --get-default-zone
    public
    #查看firewalld服务当前所使用的区域
    [root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
    public
    #查看指定接口的 Zone 信息
    [root@localhost ~]# firewall-cmd --zone=public --list-interfaces
    ens33
    #查看指定级别的接口
    [root@localhost ~]# firewall-cmd --zone=public --list-all
    public (active)
      target: default
      icmp-block-inversion: no
      interfaces: ens33
      sources: 
      services: dhcpv6-client ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    #查看指定级别的所有信息,譬如 public
    [root@localhost ~]# firewall-cmd --get-service
    #查看所有级别被允许的信息
    [root@localhost ~]# firewall-cmd --get-service --permanent
    #查看重启后所有 Zones 级别中被允许的服务,即永久放行的服务
    [root@localhost ~]# firewall-cmd --version
    0.6.3
    查看版本
    [root@localhost ~]# firewall-cmd --help
    #查看帮助
    [root@localhost ~]# firewall-cmd --zone=dmz --list-ports
    # 列出 dmz 级别的被允许的进入端口
    [root@localhost ~]# firewall-cmd --zone=public --query-service=ssh
    yes
    [root@localhost ~]# firewall-cmd --zone=public --query-service=https
    no
    # 查询public区域是否允许请求SSH和HTTPS协议的流量:

    2.管理规则

    # firewall-cmd --panic-on           # 丢弃
    # firewall-cmd --panic-off          # 取消丢弃
    # firewall-cmd --query-panic        # 查看丢弃状态
    # firewall-cmd --reload             # 更新规则,不重启服务
    # firewall-cmd --complete-reload    # 更新规则,重启服务

    2.1 管理端口

    列出 dmz 级别的被允许的进入端口

    [root@localhost ~]# firewall-cmd --zone=dmz --list-ports
    列出 dmz 级别的被允许的进入端口
    [root@localhost ~]# firewall-cmd --zone=public --add-interface=eth0 --permanent
    success
    添加某接口至某信任等级,譬如添加 eth0 至 public,永久修改
    [root@localhost ~]# firewall-cmd --set-default-zone=public
    Warning: ZONE_ALREADY_SET: public
    success
    [root@localhost ~]# firewall-cmd --zone=dmz --add-port=8080/tcp
    success
    允许 tcp 端口 8080 至 dmz 级别
    [root@localhost ~]# firewall-cmd --zone=public --add-port=5060-5059/udp --permanent
    success
    允许某范围的 udp 端口至 public 级别,并永久生效

    2.2 网卡接口

    列出 public zone 所有网卡
    [root@localhost ~]# firewall-cmd --zone=public --list-interfaces
    ens33
    将 eth0 添加至 public zone,永久
    [root@localhost ~]# firewall-cmd --zone=public --permanent --add-interface=eth0
    success
    eth0 存在与 public zone,将该网卡添加至 work zone,并将之从 public zone 中删除
    [root@localhost ~]# firewall-cmd --zone=work --permanent --change-interface=eth0
    success
    删除 public zone 中的 eth0,永久
    [root@localhost ~]# firewall-cmd --zone=public --permanent --remove-interface=eth0
    Warning: NOT_ENABLED: eth0
    success

    2.3 管理服务

    添加 smtp 服务至 work zone
    [root@localhost ~]# firewall-cmd --zone=work --add-service=smtp
    success
    移除 work zone 中的 smtp 服务
    [root@localhost ~]# firewall-cmd --zone=work --remove-service=smtp
    success
    启用区域中的一种服务
    firewall-cmd [--zone=<zone>] --add-service=<service> [--timeout=<seconds>]
    例: 使区域中的ipp-client服务生效60秒:
    [root@localhost ~]# firewall-cmd --zone=home --add-service=ipp-client --timeout=60
    success
    例: 启用默认区域中的http服务:
    [root@localhost ~]# firewall-cmd --add-service=http
    success
    禁用区域中的某种服务
    firewall-cmd [--zone=<zone>] --remove-service=<service>
    例: 禁止home区域中的http服务:
    [root@localhost ~]# firewall-cmd --zone=home --remove-service=http
    NABLED: 'http' not in 'home'
    success
    查询区域中是否启用了特定服务
    firewall-cmd [--zone=<zone>] --query-service=<service>
    如果服务启用,将返回1,否则返回0。没有输出信息。
    启用区域端口和协议组合
    firewall-cmd [--zone=<zone>] --add-port=<port>[-<port>]/<protocol> [--timeout=<seconds>]
    此举将启用端口和协议的组合。端口可以是一个单独的端口 <port> 或者是一个端口范围 <port>-<port> 。协议可以是 tcp 或 udp
    禁用端口和协议组合
    firewall-cmd [--zone=<zone>] --remove-port=<port>[-<port>]/<protocol>
    查询区域中是否启用了端口和协议组合
    firewall-cmd [--zone=<zone>] --query-port=<port>[-<port>]/<protocol>
    如果启用,此命令将有返回值。没有输出信息。
    例: 阻塞区域的响应应答报文:
    [root@localhost ~]# firewall-cmd --zone=public --add-icmp-block=echo-reply
    success
    获取永久选项所支持的服务
    [root@localhost ~]# firewall-cmd --permanent --get-services
    获取永久选项所支持的ICMP类型列表
    [root@localhost ~]# firewall-cmd --permanent --get-icmptypes

    2.4 配置external zone中的IP地址伪装

    查看

    [root@localhost ~]# firewall-cmd --zone=external --query-masquerade
    yes
    打开伪装
    [root@localhost ~]# firewall-cmd --zone=external --add-masquerade
    Warning: ALREADY_ENABLED: masquerade already enabled in 'external'
    success
    关闭伪装
    [root@localhost ~]# firewall-cmd --zone=external --remove-masquerade
    success

    2.5 配置 public zone 的端口转发

    在区域中启用端口转发或映射
    firewall-cmd [--zone=<zone>] --add-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    端口可以映射到另一台主机的同一端口,也可以是同一主机或另一主机的不同端口。端口号可以是一个单独的端口 <port> 或者是端口范围 <port>-<port> 。协议可以为 tcp 或udp 。目标端口可以是端口号 <port> 或者是端口范围 <port>-<port> 。目标地址可以是 IPv4 地址。受内核限制,端口转发功能仅可用于IPv4。
    禁止区域的端口转发或者端口映射
    firewall-cmd [--zone=<zone>] --remove-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    查询区域的端口转发或者端口映射
    firewall-cmd [--zone=<zone>] --query-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }

    要打开端口转发,则需要先打开伪装

    [root@localhost ~]# firewall-cmd --zone=public --add-masquerade
    success
    然后转发 tcp 22 端口至 3753
    [root@localhost ~]# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=3753
    success
    转发 22 端口数据至另一个 ip 的相同端口上
    [root@localhost ~]# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toaddr=192.168.1.100
    success
    转发 22 端口数据至另一 ip 的 2055 端口上

    [root@localhost ~]# firewall-cmd --zone=public --add-forward-port=port=22:proto=tcp:toport=2055:toaddr=192.168.1.100
    success

    2.6 配置 public zone 的 icmp

    查看所有支持的 icmp 类型
     firewall-cmd --get-icmptypes destination-unreachable echo-reply echo-request parameter-problem redirect router-advertisement router-solicitation source-quench time-exceeded
    查看
    [root@localhost ~]#  firewall-cmd --zone=public --list-icmp-blocks
    echo-reply
    添加 echo-request 屏蔽
    firewall-cmd --zone=public --add-icmp-block=echo-request [--timeout=seconds]
    移除 echo-reply 屏蔽
    # firewall-cmd --zone=public --remove-icmp-block=echo-reply
    2.7 IP封禁
    [root@localhost ~]# firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='222.222.222.222' reject"
    success
    当然,我们仍然可以通过 ipset 来封禁 ip
    封禁 ip
    [root@localhost ~]# firewall-cmd --permanent --zone=public --new-ipset=blacklist --type=hash:ip
    success
    [root@localhost ~]# firewall-cmd --permanent --zone=public --ipset=blacklist --add-entry=222.222.222.222
    success
    封禁网段
    [root@localhost ~]# firewall-cmd --permanent --zone=public --new-ipset=blacklist --type=hash:net
    Error: NAME_CONFLICT: new_ipset(): 'blacklist'
    [root@localhost ~]# firewall-cmd --permanent --zone=public --ipset=blacklist --add-entry=222.222.222.0/24
    success
    导入 ipset 的 blacklist 规则
    [root@localhost ~]# firewall-cmd --permanent --zone=public --new-ipset-from-file=/path/blacklist.xml
    如果已经存 blacklist,则需要先删除
    [root@localhost ~]# firewall-cmd --get-ipsets
    
    [root@localhost ~]# firewall-cmd --permanent --zone=public --delete-ipset=blacklist
    success
    然后封禁 blacklist
    [root@localhost ~]# firewall-cmd --permanent --zone=public --add-rich-rule='rule source ipset=blacklist drop'
    success
    重新载入以生效
    [root@localhost ~]# firewall-cmd --reload
    success
    查看 blacklist
    [root@localhost ~]# firewall-cmd --ipset=blacklist --get-entries

    2.8 zone

    获取支持的永久区域
    [root@localhost ~]# firewall-cmd --permanent --get-zones
    block dmz drop external home internal public trusted work
    启用区域中的服务
    firewall-cmd --permanent [--zone=<zone>] --add-service=<service>
    此举将永久启用区域中的服务。如果未指定区域,将使用默认区域。
    禁用区域中的一种服务
    firewall-cmd --permanent [--zone=<zone>] --remove-service=<service>
    查询区域中的服务是否启用
    firewall-cmd --permanent [--zone=<zone>] --query-service=<service>
    如果服务启用,此命令将有返回值。此命令没有输出信息。
    例: 永久启用 home 区域中的 ipp-client 服务
    [root@localhost ~]# firewall-cmd --permanent --zone=home --add-service=ipp-client
    success
    永久启用区域中的一个端口-协议组合
    firewall-cmd --permanent [--zone=<zone>] --add-port=<port>[-<port>]/<protocol>
    查询区域中的端口-协议组合是否永久启用
    firewall-cmd --permanent [--zone=<zone>] --query-port=<port>[-<port>]/<protocol>
    如果服务启用,此命令将有返回值。此命令没有输出信息。
    例: 永久启用 home 区域中的 https (tcp 443) 端口
    [root@localhost ~]# firewall-cmd --permanent --zone=home --add-port=443/tcp
    success
    永久启用区域中的伪装
    firewall-cmd --permanent [--zone=<zone>] --add-masquerade
    此举启用区域的伪装功能。私有网络的地址将被隐藏并映射到一个公有IP。这是地址转换的一种形式,常用于路由。由于内核的限制,伪装功能仅可用于IPv4。
    永久禁用区域中的伪装
    firewall-cmd --permanent [--zone=<zone>] --remove-masquerade
    查询区域中的伪装的永久状态
    firewall-cmd --permanent [--zone=<zone>] --query-masquerade
    如果服务启用,此命令将有返回值。此命令没有输出信息。
    永久启用区域中的ICMP阻塞
    firewall-cmd --permanent [--zone=<zone>] --add-icmp-block=<icmptype>
    此举将启用选中的 Internet 控制报文协议 (ICMP) 报文进行阻塞。 ICMP 报文可以是请求信息或者创建的应答报文或错误应答报文。
    永久禁用区域中的ICMP阻塞
    firewall-cmd --permanent [--zone=<zone>] --remove-icmp-block=<icmptype>
    查询区域中的ICMP永久状态
    firewall-cmd --permanent [--zone=<zone>] --query-icmp-block=<icmptype>
    如果服务启用,此命令将有返回值。此命令没有输出信息。
    例: 阻塞公共区域中的响应应答报文:
    [root@localhost ~]# firewall-cmd --permanent --zone=public --add-icmp-block=echo-reply
    success
    在区域中永久启用端口转发或映射
    firewall-cmd --permanent [--zone=<zone>] --add-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }

    端口可以映射到另一台主机的同一端口,也可以是同一主机或另一主机的不同端口。端口号可以是一个单独的端口 <port> 或者是端口范围 <port>-<port> 。协议可以为 tcp 或udp 。目标端口可以是端口号 <port> 或者是端口范围 <port>-<port> 。目标地址可以是 IPv4 地址。受内核限制,端口转发功能仅可用于IPv4。

    永久禁止区域的端口转发或者端口映射
    firewall-cmd --permanent [--zone=<zone>] --remove-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    查询区域的端口转发或者端口映射状态
    firewall-cmd --permanent [--zone=<zone>] --query-forward-port=port=<port>[-<port>]:proto=<protocol> { :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> }
    如果服务启用,此命令将有返回值。此命令没有输出信息。
    例: 将 home 区域的 ssh 服务转发到 127.0.0.2
    [root@localhost ~]# firewall-cmd --permanent --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2
    success

    IPTABLES

    iptables中常用的参数以及作用

    参数 作用
    -P 设置默认策略
    -F 清空规则链
    -L 查看规则链
    -A 在规则链的末尾加入新规则
    -I num 在规则链的头部加入新规则
    -D num 删除某一条规则
    -s 匹配来源地址IP/MASK,加叹号“!”表示除这个IP外
    -d 匹配目标地址
    -i 网卡名称 匹配从这块网卡流入的数据
    -o 网卡名称 匹配从这块网卡流出的数据
    -p 匹配协议,如TCP、UDP、ICMP
    --dport num 匹配目标端口号
    --sport num 匹配来源端口号

    -L查看已有的防火墙规则链:

    [root@localhost ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
    ACCEPT     all  --  anywhere             anywhere            
    INPUT_direct  all  --  anywhere             anywhere            
    INPUT_ZONES_SOURCE  all  --  anywhere             anywhere            
    INPUT_ZONES  all  --  anywhere             anywhere            
    DROP       all  --  anywhere             anywhere             ctstate INVALID
    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
    ACCEPT     all  --  anywhere             anywhere            
    FORWARD_direct  all  --  anywhere             anywhere            
    FORWARD_IN_ZONES_SOURCE  all  --  anywhere             anywhere            
    FORWARD_IN_ZONES  all  --  anywhere             anywhere            
    FORWARD_OUT_ZONES_SOURCE  all  --  anywhere             anywhere            
    FORWARD_OUT_ZONES  all  --  anywhere             anywhere            
    DROP       all  --  anywhere             anywhere             ctstate INVALID
    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    OUTPUT_direct  all  --  anywhere             anywhere            
    
    Chain FORWARD_IN_ZONES (1 references)
    target     prot opt source               destination         
    FWDI_public  all  --  anywhere             anywhere            [goto] 
    FWDI_work  all  --  anywhere             anywhere            [goto] 
    FWDI_public  all  --  anywhere             anywhere            [goto] 
    
    Chain FORWARD_IN_ZONES_SOURCE (1 references)
    target     prot opt source               destination         
    
    Chain FORWARD_OUT_ZONES (1 references)
    target     prot opt source               destination         
    FWDO_public  all  --  anywhere             anywhere            [goto] 
    FWDO_work  all  --  anywhere             anywhere            [goto] 
    FWDO_public  all  --  anywhere             anywhere            [goto] 
    
    Chain FORWARD_OUT_ZONES_SOURCE (1 references)
    target     prot opt source               destination         
    
    Chain FORWARD_direct (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_public (2 references)
    target     prot opt source               destination         
    FWDI_public_log  all  --  anywhere             anywhere            
    FWDI_public_deny  all  --  anywhere             anywhere            
    FWDI_public_allow  all  --  anywhere             anywhere            
    ACCEPT     icmp --  anywhere             anywhere            
    
    Chain FWDI_public_allow (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_public_deny (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_public_log (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_work (1 references)
    target     prot opt source               destination         
    FWDI_work_log  all  --  anywhere             anywhere            
    FWDI_work_deny  all  --  anywhere             anywhere            
    FWDI_work_allow  all  --  anywhere             anywhere            
    ACCEPT     icmp --  anywhere             anywhere            
    
    Chain FWDI_work_allow (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_work_deny (1 references)
    target     prot opt source               destination         
    
    Chain FWDI_work_log (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_public (2 references)
    target     prot opt source               destination         
    FWDO_public_log  all  --  anywhere             anywhere            
    FWDO_public_deny  all  --  anywhere             anywhere            
    FWDO_public_allow  all  --  anywhere             anywhere            
    
    Chain FWDO_public_allow (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_public_deny (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_public_log (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_work (1 references)
    target     prot opt source               destination         
    FWDO_work_log  all  --  anywhere             anywhere            
    FWDO_work_deny  all  --  anywhere             anywhere            
    FWDO_work_allow  all  --  anywhere             anywhere            
    
    Chain FWDO_work_allow (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_work_deny (1 references)
    target     prot opt source               destination         
    
    Chain FWDO_work_log (1 references)
    target     prot opt source               destination         
    
    Chain INPUT_ZONES (1 references)
    target     prot opt source               destination         
    IN_public  all  --  anywhere             anywhere            [goto] 
    IN_work    all  --  anywhere             anywhere            [goto] 
    IN_public  all  --  anywhere             anywhere            [goto] 
    
    Chain INPUT_ZONES_SOURCE (1 references)
    target     prot opt source               destination         
    
    Chain INPUT_direct (1 references)
    target     prot opt source               destination         
    
    Chain IN_public (2 references)
    target     prot opt source               destination         
    IN_public_log  all  --  anywhere             anywhere            
    IN_public_deny  all  --  anywhere             anywhere            
    IN_public_allow  all  --  anywhere             anywhere            
    ACCEPT     icmp --  anywhere             anywhere            
    
    Chain IN_public_allow (1 references)
    target     prot opt source               destination         
    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh ctstate NEW
    
    Chain IN_public_deny (1 references)
    target     prot opt source               destination         
    REJECT     all  --  222.222.222.222      anywhere             reject-with icmp-port-unreachable
    
    Chain IN_public_log (1 references)
    target     prot opt source               destination         
    
    Chain IN_work (1 references)
    target     prot opt source               destination         
    IN_work_log  all  --  anywhere             anywhere            
    IN_work_deny  all  --  anywhere             anywhere            
    IN_work_allow  all  --  anywhere             anywhere            
    ACCEPT     icmp --  anywhere             anywhere            
    
    Chain IN_work_allow (1 references)
    target     prot opt source               destination         
    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh ctstate NEW
    
    Chain IN_work_deny (1 references)
    target     prot opt source               destination         
    
    Chain IN_work_log (1 references)
    target     prot opt source               destination         
    
    Chain OUTPUT_direct (1 references)
    target     prot opt source               destination         

    -F参数清空已有的防火墙规则链:

    [root@localhost ~]# iptables -F
    [root@localhost ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain FORWARD_IN_ZONES (0 references)
    target     prot opt source               destination         
    
    Chain FORWARD_IN_ZONES_SOURCE (0 references)
    target     prot opt source               destination         
    
    Chain FORWARD_OUT_ZONES (0 references)
    target     prot opt source               destination         
    
    Chain FORWARD_OUT_ZONES_SOURCE (0 references)
    target     prot opt source               destination         
    
    Chain FORWARD_direct (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_public (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_public_allow (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_public_deny (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_public_log (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_work (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_work_allow (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_work_deny (0 references)
    target     prot opt source               destination         
    
    Chain FWDI_work_log (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_public (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_public_allow (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_public_deny (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_public_log (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_work (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_work_allow (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_work_deny (0 references)
    target     prot opt source               destination         
    
    Chain FWDO_work_log (0 references)
    target     prot opt source               destination         
    
    Chain INPUT_ZONES (0 references)
    target     prot opt source               destination         
    
    Chain INPUT_ZONES_SOURCE (0 references)
    target     prot opt source               destination         
    
    Chain INPUT_direct (0 references)
    target     prot opt source               destination         
    
    Chain IN_public (0 references)
    target     prot opt source               destination         
    
    Chain IN_public_allow (0 references)
    target     prot opt source               destination         
    
    Chain IN_public_deny (0 references)
    target     prot opt source               destination         
    
    Chain IN_public_log (0 references)
    target     prot opt source               destination         
    
    Chain IN_work (0 references)
    target     prot opt source               destination         
    
    Chain IN_work_allow (0 references)
    target     prot opt source               destination         
    
    Chain IN_work_deny (0 references)
    target     prot opt source               destination         
    
    Chain IN_work_log (0 references)
    target     prot opt source               destination         
    
    Chain OUTPUT_direct (0 references)
    target     prot opt source               destination   

    把INPUT规则链的默认策略设置为拒绝:

    [root@localhost ~]#  iptables -P INPUT DROP
    [root@localhost ~]# iptables -L

    当把INPUT链设置为默认拒绝后,就要往里面写入允许策略了,否则所有流入的数据包都会被默认拒绝掉,需要留意规则链的默认策略拒绝动作只能是DROP,而不能是REJECT。

    向INPUT链中添加允许ICMP流量进入的策略规则:

    在日常运维工作中,经常会使用ping命令来检查对方主机是否在线,而向防火墙的INPUT规则链中添加一条允许ICMP流量进入的策略规则就默认允许了这种ping命令检测行为。

    [root@localhost ~]# iptables -I INPUT -p icmp -j ACCEPT
    [root@localhost ~]# ping -c 4 www.baidu.com
    PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data.
    64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=1 ttl=52 time=24.6 ms
    64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=2 ttl=52 time=19.9 ms
    64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=3 ttl=52 time=19.5 ms
    64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=4 ttl=52 time=20.1 ms
    
    --- www.a.shifen.com ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3006ms
    rtt min/avg/max/mdev = 19.566/21.091/24.603/2.047 ms

    删除INPUT规则链中刚刚加入的那条策略(允许ICMP流量),并把默认策略设置为允许:

    [root@localhost ~]# iptables -D INPUT 1
    [root@localhost ~]# iptables -P INPUT ACCEPT
    [root@localhost ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination   

    将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,拒绝来自其他所有主机的流量

    [root@localhost ~]# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
    [root@localhost ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT
    [root@localhost ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    ACCEPT     tcp  --  192.168.1.0/24       anywhere             tcp dpt:ssh
    REJECT     tcp  --  anywhere             anywhere             tcp dpt:ssh reject-with icmp-port-unreachable

    防火墙策略规则是按照从上到下的顺序匹配的,因此一定要把允许动作放到拒绝动作前面,否则所有的流量就将被拒绝掉,从而导致任何主机都无法访问我们的服务。

    向INPUT规则链中添加拒绝所有人访问本机12345端口的策略规则

    复制代码
    [root@linuxprobe ~]# iptables -I INPUT -p tcp --dport 12345 -j REJECT
    [root@linuxprobe ~]# iptables -I INPUT -p udp --dport 12345 -j REJECT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
    复制代码

    向INPUT规则链中添加拒绝192.168.10.5主机访问本机80端口(Web服务)的策略规则

    复制代码
    [root@linuxprobe ~]# iptables -I INPUT -p tcp -s 192.168.10.5 --dport 80 -j REJECT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    REJECT tcp -- 192.168.10.5 anywhere tcp dpt:http reject-with icmp-port-unreachable
    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
    复制代码

    向INPUT规则链中添加拒绝所有主机访问本机10001024端口的策略规则

    [root@linuxprobe ~]# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
    [root@linuxprobe ~]# iptables -A INPUT -p udp --dport 1000:1024 -j REJECT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    REJECT tcp -- 192.168.10.5 anywhere tcp dpt:http reject-with icmp-port-unreachable
    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpts:cadlock2:1024 reject-with icmp-port-unreachable
    REJECT udp -- anywhere anywhere udp dpts:cadlock2:1024 reject-with icmp-port-unreachable
    ………………省略部分输出信息………………

    有关iptables命令的知识讲解到此就结束了,大家是不是意犹未尽?考虑到Linux防火墙的发展趋势,大家只要能把上面的实例吸收消化,就可以完全搞定日常的iptables配置工作了。但是请特别注意,使用iptables命令配置的防火墙规则默认会在系统下一次重启时失效,如果想让配置的防火墙策略永久生效,还要执行保存命令:

    [root@linuxprobe ~]# service iptables save
    iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
  • 相关阅读:
    uva-679 Dropping Balls UVA
    并查集模板
    最大子段和模板
    uva 10048 Audiophobia UVA
    2018/11/2 周五集训队第三次比赛补题题解
    单源最短路径算法小总结
    DP背包问题小总结
    SpringMVC的请求-获得请求参数-获得请求头信息
    SpringMVC的请求-获得请求参数-自定义类型转换器
    SpringMVC的请求-获得请求参数-Restful风格的参数的获取
  • 原文地址:https://www.cnblogs.com/liujunjun/p/12184685.html
Copyright © 2020-2023  润新知