Linux---使用命令行接口配置firewalld的管理

Firewalld 概述

动态防火墙后台程序 firewalld 提供了一个 动态管理的防火墙, 用以支持网络 “ zones” , 以分配对一个网络及其相关链接和界面一定程度的信任。它具备对 IP v4 和 IP v6 防火墙设置的支持。它支持以太网桥 , 并有分离运行时间和永久性配置选择。它还具备一个通向服务或者应用程序以直接增加防火墙规则
的接口系统提供了图像化的配置工具 firewall-config 、 system-config-firewall, 提供命令行客户端 firewall-cmd, 用于配置 firewalld 永久性或非永久性运行时间的改变 : 它依次用iptables 工具与执行数据包筛选的内核中的 Netfilter 通信。

Firewalld 域

这里写图片描述

一、使用命令行接口配置防火墙

firewalld-cmd --state      ##查看火墙当前状态
firewalld-cmd --get-active-zone ##查看火墙默认生效(已激活)的域
firewalld-cmd --get-default-zone ##显示默认区域
firewalld-cmd --get-zones   ##列出所有域
firewalld-cmd --zone=public --list-all ##列出pubulic域所有的策略
firewalld-cmd --get-services   ##查看火墙管控的所有服务
firewalld-cmd --list-all-zones  ##列出所有的域
firewalld-cmd --set-default-zone=dmz ##设置默认域为dmz
firewall-cmd --add-source=172.25.254.68 --zone=trusted    ##将172.25.254.68加入trusted域
firewall-cmd --list-all --zone=trusted ##列出所有域为trusted的信息
firewall-cmd --list-interfaces   ##列出所有接口
firewall-cmd --get-zone-of-interface=eth0  ##显示eth0的域
firewall-cmd --change-interface=eth0 --zone=trusted  ##修改eth0的域为trusted
firewall-cmd --remove-interface=eth0 --zone=trusted  ##移除eth0的域
firewall-cmd --permanent --add-source=172.25.254.68  ##将该ip永久加入
firewall-cmd --add-port=8080/tcp --zone=public  ##将8080端口加入public域
firewall-cmd --permanebt --remove-service=ssh
firewall-cmd --reload    ##对移除之后的访问有影响
firewall-cmd --complete-reload  ##对移除之后和之前的访问都有影响

配置安装:

[root@client ~]# yum install firewalld firewalld-config ##安装火墙
[root@client ~]# systemctl stop iptables  ##关闭iptables
[root@client ~]# systemctl disable iptables ##开机自动启动服务
[root@client ~]# systemctl start firewalld  ##开启火墙
[root@client ~]# systemctl enable firewalld ##开机自动开启
[root@client ~]# yum search iptables  ##寻找iptables安装包
[root@client ~]# yum install iptables-services.x86_64

这里写图片描述

接口配置:

[root@client ~]# firewall-cmd --state  ##查看火墙状态  
[root@client ~]# firewall-cmd --get-active-zones ##列出当前被系统使用的域
[root@client ~]# firewall-cmd --get-default-zone  ##查看火墙默认生效的域
[root@client ~]# firewall-cmd --get-zones  ##查看默认的规则

这里写图片描述

[root@client ~]# firewall-cmd --zone=public --list-all ##查看public域的信息
[root@client ~]# firewall-cmd --get-services  ##查看可以再火墙中用名称表示的服务名称
[root@client ~]# firewall-cmd --list-all-zones  ##查看所有的域

这里写图片描述
这里写图片描述

1.设置默认域

[root@client Desktop]# yum install httpd -y
[root@client Desktop]# systemctl start httpd
[root@client Desktop]# echo westos > /var/www/html/index.html

在网页浏览172.25.254.128 显示连接不上
这里写图片描述

[root@client Desktop]# firewall-cmd --set-default-zone=trusted
success
[root@client Desktop]# systemctl restart firewalld
[root@client Desktop]# firewall-cmd --add-source=172.25.254.28 --zone=trusted
success

再次网页浏览172.25.254.128,更改过域后就可以看到在里面写进的内容了
这里写图片描述

2.服务器网卡管控

在真机上:

[root@foundation68 ~]# virt-manager

在客户端添加两块网卡:
这里写图片描述
在客户端:
添加两个网卡的IP
这里写图片描述
这里写图片描述

[root@client Desktop]# nm-connection-editor ##添加两个IP
[root@client ~]# systemctl restart network  ##重启网络
[root@client ~]# ifconfig  ##查看IP

这里写图片描述

[root@client ~]# firewall-cmd --set-default-zone=public ##设置默认域为public
[root@client ~]# firewall-cmd --get-zone-of-interface=eth1 ##显示eth1的域
[root@client ~]# firewall-cmd --get-zone-of-interface=eth0 ##显示eth0的域

这里写图片描述

[root@client ~]# firewall-cmd --change-interface=eth0 --zone=trusted ##修改eth0的域为trusted
[root@client ~]# firewall-cmd --get-zone-of-interface=eth0
[root@client ~]# firewall-cmd --get-zone-of-interface=eth1
[root@client ~]# firewall-cmd --remove-interface=eth0 --zone=trusted  ##移除eth0的域

这里写图片描述

3.更改域信息

[root@client ~]# firewall-cmd --list-all
[root@client ~]# systemctl restart firewalld

这里写图片描述

[root@client ~]# firewall-cmd --add-source=172.25.254.68
[root@client ~]# firewall-cmd --list-all

这里写图片描述

[root@client ~]# firewall-cmd --remove-source=172.25.254.68  ##移除IP
[root@client ~]# firewall-cmd --permanent --add-source=172.25.254.123   ##将该ip永久加入
[root@client ~]# firewall-cmd --reload  ##生效
[root@client ~]# firewall-cmd --list-all

这里写图片描述

[root@client ~]# cd /etc/firewalld
[root@client firewalld]# ls
[root@client firewalld]# cd zones
[root@client zones]# ls
[root@client zones]# vim public.xml
  添加:  <service name="http"/>
[root@client zones]# systemctl restart firewalld
[root@client zones]# firewall-cmd --list-all

这里写图片描述
在文件里添加http服务
这里写图片描述
可以看到services里面有http服务
这里写图片描述

4.改变端口

[root@client ~]# vim /usr/lib/firewalld/services/http.xml
  <port protocol="tcp" port="8080"/>
[root@client ~]# firewall-cmd --add-port=8080/tcp --zone=public  ##将8080端口加入public域
[root@client ~]# firewall-cmd --list-all

更改端口:
这里写图片描述
这里写图片描述

5.ssh服务的移除

[root@client ~]# firewall-cmd --remove-service=ssh
success
[root@client ~]# firewall-cmd --list-all

这里写图片描述
在真机上连接虚拟机:
这里写图片描述
但是不会中断已经连接上的虚拟机的界面

[root@client ~]# firewall-cmd --reload  
success
[root@client ~]# firewall-cmd --list-all

用firewall-cmd –remove-service=ssh移除ssh服务后再次刷新,ssh服务会恢复到原来的services里
这里写图片描述

[root@client ~]# firewall-cmd --complete-reload  ##对移除之后和之前的访问都有影响

再次看真机上已经连接的虚拟机就不能做任何动作了,并且真机也连接不到虚拟机。已经完全中断ssh服务。
reload刷新之后不会中断连接,complete-reload刷新之后会中断连接


DirectRules

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

[root@localhost Desktop]# systemctl start firewalld
[root@localhost Desktop]# yum install httpd
[root@localhost Desktop]# systemctl start httpd
[root@localhost Desktop]# firewall-cmd --list-all   

这里写图片描述
在真机和server虚拟机上网页浏览:172.25.254.128
这里写图片描述
这里写图片描述
此时是unable to connect,是因为httpd默认域为public.

[root@localhost Desktop]# firewall-cmd --permanent --add-service=http ##添加服务
[root@localhost Desktop]# firewall-cmd --reload  ##重新加载
[root@localhost Desktop]# firewall-cmd --list-all

这里写图片描述
在真机和server虚拟机上网页浏览:172.25.254.128
这里写图片描述
这里写图片描述

[root@localhost Desktop]# firewall-cmd --permanent --remove-service=http ##删除服务
[root@localhost Desktop]# firewall-cmd --reload  ##重新加载策略
[root@localhost Desktop]# iptables -nL  ##查看策略
[root@localhost Desktop]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.28 -j ACCEPT 
##添加规则(filter 本地数据限制;-s 目的地址;-p 协议;--direct 端口;-j 行为;/REJECT拒绝/ACCEPT同意/DROP丢弃)
[root@localhost Desktop]# firewall-cmd --direct --get-all-rules  ##列出规则

这里写图片描述
在真机上网页浏览:172.25.254.128
这里写图片描述
在server虚拟机网页浏览:172.25.254.128
这里写图片描述


RichRules

通过该地址,可以用比直接接口方式更易理解的方式建立复杂防火墙的规则。此外。还能永久保留保留设置,这种语法使用关键词值。

端口转发:

[root@localhost Desktop]# firewall-cmd --list-all
[root@localhost Desktop]# firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.68 ##设定端口转发
[root@localhost Desktop]# firewall-cmd --permanent --add-masquerade ##开启地址伪装
[root@localhost Desktop]# firewall-cmd --reload  ##重新加载策略
[root@localhost Desktop]# firewall-cmd --list-all

这里写图片描述
这里写图片描述
在真机上连接虚拟机IP

[kiosk@foundation68 Desktop]$ ssh root@172.25.254.128

这里写图片描述
连接IP出现上图乱码时

[kiosk@foundation68 Desktop]$ rm -fr ~/.ssh/known_hosts
[kiosk@foundation68 Desktop]$ ssh root@172.25.254.128

这里写图片描述
连接128主机时,IP为128主机成功将连接请求转到了68主机上
以上实验操作为了实验方便我将设定端口转发IP设定为了我自己的真机IP,如果大家有需要可以设置为其他主机IP。

伪装地址:

在desktop虚拟机上:(作为路由)
添加网卡,设置两个IP在不同网段
这里写图片描述

[root@localhost Desktop]# firewall-cmd --permanent --remove-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.20
[root@localhost Desktop]# firewall-cmd --reload
[root@localhost Desktop]# sysctl -a | grep ip_forward

这里写图片描述
(如果net.ipv4.ip_forward = 0时
vim /etc/sysctl.conf 加入:net.ipv4.ip_forward = 1
再用sysctl -p 刷新执行 )

在server虚拟机上:
这里写图片描述

在desktop虚拟机上:

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

这里写图片描述

在server虚拟机上:
这里写图片描述
可以ping的通说明伪装地址成功
“0”网段的server主机可以成功连接“254”网段的“172.25.254.68”主机。

猜你喜欢

转载自blog.csdn.net/argued_d/article/details/80600858
今日推荐