firewalld规则配置

firewalld规则配置

一、概念

  1. 动态防火墙

    启动新规则时,不会像iptables一样,先清空规则,再启动所有规则,如此会对现在程序有影响,哪怕只是一条规则。而firewalld 规则变更不需要对整个防火墙规则重载,可直接添加新规则

  2. iptables与firewalld的关系

    firewalld底层使用iptables作为防火墙规则管理入口。
    firewalld内核模块还是netfilter,只是firewalld修改了daemon和service
    添加规则还是通过iptables管理的,可以通过iptables查看规则
    firewalld没有链的概念

  3. firewalld配置

    储存在/usr/lib/fierwalld/和/etc/firewalld/目录中的XML文件中

    /usr/lib/fierwalld/services/ 模块目录,里面有每个服务对应的模板,配置默认端口
    /etc/firewalld/ 配置目录

  4. firewalld区域

    firewalld将网卡对应到不同的区域(zone),zone默认共有9个:block,dmz,drop,external,home,internal,public,trusted,work.
    区域相当于iptables的表 drop区域:配置的规则为丢弃的规则
    public区域:公共规则

二、开放端口

  1. 配置文件添加

    添加配置

    [root@localhost ~]# vi /etc/firewalld/zones/public.xml
    <?xml version="1.0" encoding="utf-8"?>
    <zone>
      <short>Public</short>
      <description>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.</d
    escription>
      <service name="ssh"/>
      <service name="dhcpv6-client"/>
      <port protocol="tcp" port="80"/>
      <port protocol="tcp" port="22"/>
      <port protocol="tcp" port="3306"/>
      <port protocol="tcp" port="8080"/>
      <port protocol="tcp" port="2222"/>
      <masquerade/>
      <forward-port to-addr="192.168.1.109" to-port="8080" protocol="tcp" port="80"/>
    </zone>
    

    加载规则

    firewall-cmd --reload
    
  2. 命令行添加

    列出所支持的zone和查看当前的默认zone

    firewall-cmd --get-zones
    

    默认使用区域

    firewall-cmd --get-default-zone
    

    查看防火墙规则

    firewall-cmd --list-all
    

    查看规则状态

    firewall-cmd --state
    

    加载规则

    firewall-cmd --reload
    

    开放3306端口

    firewall-cmd --add-port=3306/tcp --permanent
    firewall-cmd --reload
    

    开放网段

    firewall-cmd --permanent --add-source=192.168.0.0/22
    

    移除规则

     firewall-cmd --permanent --remove-source=192.168.0.0/22
    

    开放服务

    firewall-cmd --permanent --add-service=http
    

    移除服务

    firewall-cmd --permanent --remove-service=http
    

    添加服务

    #cp ssh.xml tomcat.xm 添加模块
    vim tomcat.xml
    #systemctl restart firewalld.service
    #firewall-cmd --add-service=tomcat --permanent
    

    开启12222端口

    #firewall-cmd -add-port=12222/tcp --permanent
    #cat /etc/firewalld/zones/public.xml
    

    删除规则

    #firewall-cmd --remove-port=12222/tcp --permanent
    

    允许192.168.0.142访问80端口

    #firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.142" port protocol="tcp" port="80" accept"
    family  对哪个协议
    source address	源地址
    accept	允许
    drop	拒绝
    

    拒绝192.168.0.142访问80端口

    #firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.142" port protocol="tcp" port="80" drop"
    

    注:同一规则允许及拒绝时,效果为拒绝,不会跟iptables一样,没有先后顺序优先匹配,为全文匹配,拒绝大于允许

三、端口转发

开启转发功能

[root@localhost subsys]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
#sysctl -p
或者
echo 1 >/proc/sys/net/ipv4/ip_forward

将访问192.168.1.123(本机)主机8080端口的请求转发至80端口

#firewall-cmd --permanent --zone=public --add-forward-port=port=8080:proto=tcp:toport=80:toaddr=192.168.1.123

将访问192.168.1.123(本机)主机8080端口的请求转发至192.168.1.111的80端口

#firewall-cmd --permanent --zone=public --add-forward-port=port=8080:proto=tcp:toaddr=192.168.1.111:toport=80

将本机的80端口转发至192.168.1.109 8080端口

#firewall-cmd --permanent --zone=public --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.109

允许IP 192.168.1.142访问本机器的6379端口

#firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.142" port protocol="tcp" port="6379" accept"

拒绝IP 192.168.1.142访问本机的22号端口

#firewall-cmd --permanent --add-rech-rule="rule family="ipv4" source address="192.168.1.142" port protocol="tcp" port="80" drop"

注:转发时,要开启伪装,伪装就是SNAT

允许IP伪装

[root@localhost ~]#  firewall-cmd --permanent --zone=public --add-masquerade
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --query-masquerade                         
yes
posted @ 2023-05-10 10:09  Teddy_boy  阅读(176)  评论(0编辑  收藏  举报