Linux_firewalld管理

一.Firewalld 概述

<1>动态防火墙后台程序 firewalld 提供了一个 动态管理的防火墙, 用以支持网络 “ zones” , 以分配对一个网络及其相关链接和界面一定程度的信任。它具备对 IP v4 和 IP v6 防火墙设置的支持。它支持以太网桥 , 并有分离运行时间和永久性配置选择。它还具备一个通向服务或者应用程序以直接增加防火墙规则的接口。

系统提供了图像化的配置工具 firewall-config 、 system-config-firewall, 提供命令行客户端 firewall-cmd, 用于配置 firewalld 永久性或非永久性运行时间的改变 : 它依次用iptables 工具与执行数据包筛选的内核中的 Netfilter 通信。

<2>firewalld 和 iptables service
firewalld 和 iptables service 之间最本质的不同是 :iptables service 在 /etc/sysconfig/iptables 中储存配置.

firewalld 将配置储存在 /usr/lib/firewalld/ 和/etc/firewalld/ 中的各种 XML 文件里.

二.启用firewalld

<1>防火墙相关命令

下载并开启服务关闭iptables

[root@client ~]# yum install firewall firewall-config -y

[root@client ~]# systemctl start firewalld

[root@client ~]# systemctl enable firewalld

[root@client ~]# systemctl stop iptable

[root@client ~]# systemctl disable iptable

查看火墙状态

[root@localhost ~]# firewall-cmd --state

查看火墙正在使用的域

[root@localhost ~]# firewall-cmd --get-active-zones

查看火墙的默认域

[root@localhost ~]# firewall-cmd --get-default-zone

查看火墙所有的域

[root@localhost ~]# firewall-cmd --get-zones

查看public域的信息

[root@localhost ~]# firewall-cmd --zone=public --list-all

查看不同通过火墙的服务

[root@localhost ~]# firewall-cmd --get-services

修改火墙的默认域

[root@localhost ~]# firewall-cmd --set-default-zone=trusted

[root@localhost ~]# firewall-cmd --get-default-zone

火墙的图形化配置工具

[root@localhost ~]# firewall-config

<2>修改端口

<3>对指定ip的控制


临时添加主机域,重启之后失效

永久添加主机域

从trusted域移除

<4>修改默认域

[root@client ~]# yum install httpd -y

[root@client ~]# systemctl start httpd

[root@client ~]# echo hello >/var/www/html/index.html

[root@client ~]# firewall-cmd --get-default-zone##查看默认域

public


[root@client ~]# firewall-cmd --set-default-zone=trusted##修改默认域为trust

success


<5>用文件的方式添加

[root@client ~]# cd /etc/firewalld/zones/

[root@client zones]# ls

public.xml  ROL.xml  trusted.xml

[root@client zones]# vim public.xml

<6>移出防火墙服务

临时移出

[root@client zones]# firewall-cmd --remove-service=ssh

[root@client zones]# firewall-cmd --reload

永久移出

[root@client zones]# firewall-cmd  --permanent --remove-service=ssh

[root@client zones]# firewall-cmd --reload

三.DirtectRules

通过 firewall-cmd 工具 , 可以使用 --direct 选项在运行时间里增加或者移除链。如果不熟悉 iptables , 使用直接接口非常危险 , 因为您可能无意间导致防火墙被入侵。直接端口模式适用于服务或者程序 , 以便在运行时间内增加特定的防火墙规则。直接端口模式添加的规则优先应用。

在desktop虚拟机上

[root@localhost ~]# yum install httpd -y

[root@localhost ~]# systemctl start firewalld

[root@localhost ~]# systemctl start httpd

[root@localhost ~]# firewall-cmd --list-all


在真机进行浏览:172.25.254.116


[root@localhost ~]# firewall-cmd --permanent --add-service=http

[root@localhost ~]# firewall-cmd --reload ##重新加载

[root@localhost ~]# firewall-cmd --list-all

在真机浏览:172.25.254.116

[root@localhost ~]# firewall-cmd --permanent --remove-service=http##删除服务

[root@localhost ~]# firewall-cmd --reload

[root@localhost ~]# iptables -nL##查看策略

[root@localhost ~]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.66 -j ACCEPT##除172.25.254.66以外,其它主机不能访问http服务

[root@localhost ~]# firewall-cmd --direct --get-all-rules ##查看特定规则

在真机进行测试

在server进行测试

四.RichRules

通过“ rich language” 语法 , 可以用比直接接口方式更易理解的方法建立复杂防火墙规则。此外 , 还能永久保留设置。这种

语言使用关键词值 , 是 iptables 工具的抽象表示。这种语言可以用来配置分区 , 也仍然支持现行的配置方式.

端口转发

[root@localhost ~]# firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.216##使用22端口(ssh服务)连接到本机的主机将其连接到172.25.254.216上

[root@localhost ~]# firewall-cmd --permanent --add-masquerade##打开伪装功能

[root@localhost ~]# firewall-cmd --reload

[root@localhost ~]# firewall-cmd --list-all

连接116主机,主机成功将连接请求转到了216主机上

伪装地址

在desktop虚拟机上:添加两个网卡,设置两个IP不同网段

[root@localhost ~]# vim /etc/sysctl.conf 

[root@localhost ~]# sysctl -p##刷新执行

[root@localhost ~]# sysctl -a | grep ip_forward

在server上

在desktop上

[root@localhost ~]# firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.254.116 masquerade'##设定伪装的地址策略

[root@localhost ~]# firewall-cmd --reload

[root@localhost ~]# firewall-cmd --list-all

在server上测试:可以ping通说明伪装地址成功


猜你喜欢

转载自blog.csdn.net/g_541243027/article/details/80613408
今日推荐