firewalld防火墙的配置管理

Firewalld概述:

firewalld是一种提供了支持网络/防火墙区域(zone)定义网络链接以及接口安全等级的动态防火墙管理工具

1.启用firewalld

##安装firewalld
[root@localhost Desktop]# yum install -y firewalld firewall-config 
##开启firewalld
[root@localhost Desktop]# systemctl start firewalld
##开机自启
[root@localhost Desktop]# systemctl enable firewalld

2.管理firewalld

(1)图形的方式

以添加http为例:

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

在这里插入图片描述

[root@localhost Desktop]# firewall-config 

在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述
(2)命令的方式

1.火墙信息的查看

##查看火墙的状态
[root@localhost Desktop]# firewall-cmd --state
running
##查看火墙当前生效(正在使用)的域
[root@localhost Desktop]# firewall-cmd --get-active-zones 
public
  interfaces: eth0 eth1
##查看火墙的默认域
[root@localhost Desktop]# firewall-cmd --get-default-zone 
public
##查看所有可用的域
[root@localhost Desktop]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

在这里插入图片描述

##查看public域的信息
[root@localhost Desktop]# firewall-cmd --zone=public --list-all 	
##查看block域的信息  
[root@localhost Desktop]# firewall-cmd --zone=block --list-all 

在这里插入图片描述

##查看所有域的信息
[root@localhost Desktop]# firewall-cmd --list-all-zones 

在这里插入图片描述

##查看可控制的服务
[root@localhost Desktop]# firewall-cmd --get-services 

在这里插入图片描述
2.火墙对apache的访问控制

@1.默认所有主机均不能访问apache

##安装apache
[root@localhost Desktop]# yum install -y httpd
##打开apache
[root@localhost Desktop]# systemctl start httpd
##查看火墙的状态
[root@localhost Desktop]# systemctl status firewalld
##查看默认域信息
[root@localhost Desktop]# firewall-cmd --list-all

测试:

在这里插入图片描述

@2.允许所有主机访问apache

##永久添加http服务到火墙策略中
[root@localhost Desktop]# firewall-cmd --permanent --add-service=http
success
[root@localhost Desktop]# firewall-cmd --list-all
##重新加载火墙
[root@localhost Desktop]# firewall-cmd --reload 
success
[root@localhost Desktop]# firewall-cmd --list-all

测试:
在这里插入图片描述
还原:

##永久移除http服务
[root@localhost Desktop]# firewall-cmd --permanent --remove-service=http
success
##重新加载
[root@localhost Desktop]# firewall-cmd --reload 
success
[root@localhost Desktop]# firewall-cmd --list-all

@3.仅允许26主机能访问所有服务(包括apache)

##永久添加26主机到trusted(信任)域中
[root@localhost Desktop]# firewall-cmd --permanent --zone=trusted --add-source=172.25.254.26
success
##重新加载
[root@localhost Desktop]# firewall-cmd --reload 
success

测试:

在26主机中
在这里插入图片描述
在226主机中
在这里插入图片描述
还原:(注意:以怎样的方式添加的就以怎样的方式删除)

[root@localhost Desktop]# firewall-cmd --permanent --zone=trusted --remove-source=172.25.254.26
success
[root@localhost Desktop]# firewall-cmd --reload 
success

3.block和drop的区别

二者都表示拒绝,但drop只拒绝不回应,而block不仅拒绝并且会回应 。一般企业使用block域来有回应的拒绝,
因为这样将不会再接收到被拒绝方发送来的任何数据

注意:block 域 不能防御,只能降低影响

@1.默认同网段的主机可以进行通信

[root@foundation26 ~]# ping -w 3 172.25.254.126

在这里插入图片描述
@2.block域对通信的影响

##将26主机添加到block域中
[root@localhost Desktop]# firewall-cmd --zone=block --add-source=172.25.254.26

在这里插入图片描述
测试:

[root@foundation26 ~]# ping -w 3 172.25.254.126

在这里插入图片描述
@3.drop域对通信的影响

##重新添加域时,必须先移除之前的域,因为不能同时存在于两个域中
[root@localhost Desktop]# firewall-cmd --zone=drop --add-source=172.25.254.26
Error: ZONE_CONFLICT
##先将26主机从block域中移除
[root@localhost Desktop]# firewall-cmd --zone=block --remove-source=172.25.254.26
success
##再将26主机添加drop域中
[root@localhost Desktop]# firewall-cmd --zone=drop --add-source=172.25.254.26
success

在这里插入图片描述
测试:

[root@foundation26 ~]# ping -w 3 172.25.254.126

在这里插入图片描述
还原:

##移除
[root@localhost Desktop]# firewall-cmd --zone=drop --remove-source=172.25.254.26 
success

4.网络接口的使用

##查看系统中生效的网络接口
[root@localhost Desktop]#  firewall-cmd --list-interfaces
eth0 eth1
##查看eth0接口所在的域
[root@localhost Desktop]#  firewall-cmd --get-zone-of-interface=eth0
public
##查看eth1接口所在的域
[root@localhost Desktop]#  firewall-cmd --get-zone-of-interface=eth1
public

在这里插入图片描述

##更改eth1接口所在域为trusted
[root@localhost Desktop]#  firewall-cmd --change-interface=eth1 --zone=trusted 
success
##查看eth1接口所在的域
[root@localhost Desktop]#  firewall-cmd --get-zone-of-interface=eth1
trusted

在这里插入图片描述

##从trusted域中移除eth1接口
[root@localhost Desktop]#  firewall-cmd --remove-interface=eth1 --zone=trusted 
success
[root@localhost Desktop]#  firewall-cmd --get-zone-of-interface=eth1
no zone

在这里插入图片描述
还原:

[root@localhost Desktop]#  firewall-cmd --add-interface=eth1 --zone=public 
success

5.网络端口的使用

##查看火墙可用的端口
[root@localhost services]# firewall-cmd --list-ports 
##永久性添加端口
[root@localhost services]# firewall-cmd --permanent --add-port=8080/tcp
success
##重新加载
[root@localhost services]# firewall-cmd --reload
success
##可查看刚添加的端口
[root@localhost services]# firewall-cmd --list-ports 
8080/tcp

在这里插入图片描述
还原:

##移除端口
[root@localhost services]# firewall-cmd --permanent --remove-port=8080/tcp
success
##重新加载
[root@localhost services]# firewall-cmd --reload
success
[root@localhost services]# firewall-cmd --list-ports 

在这里插入图片描述
6.reload与complete-reload的区别

--reload             ##只刷新策略,不会将正在连接的断开
--complete-reload    ##刷新策略,并且会中断正在连接的终端(完全中断)

3.firewalld文件管理

(1)添加ip源的两种方式

临时性添加:

##临时添加ip源
[root@localhost Desktop]# firewall-cmd --add-source=172.25.254.226
success
##可查看到新添加的ip源
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述

##重启火墙
[root@localhost Desktop]# systemctl restart firewalld
##发现ip源消失
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述
永久性添加:

##永久性添加ip源; --permanent表示永久性的
[root@localhost Desktop]# firewall-cmd --permanent --add-source=172.25.254.226
success
##重新加载
[root@localhost Desktop]# firewall-cmd --reload 
success
##可查看到新添加的ip源
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述

##重启火墙
[root@localhost Desktop]# systemctl restart firewalld
##发现此时ip源仍然存在
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述
永久添加方式,实质上是通过自动写入/etc/firewalld/zones/public.xml 文件中实现的

###可查看到刚添加的ip源,xml代表可扩展性标记语言
[root@localhost Desktop]# vim /etc/firewalld/zones/public.xml永久添加,是通过自动写入/etc/firewalld/zones/public.xml 中实现的

在这里插入图片描述
还原:

##移除ip源
[root@localhost services]# firewall-cmd --permanent --remove-source=172.25.254.226
success
[root@localhost services]# firewall-cmd --reload
success
[root@localhost services]# firewall-cmd --list-all

在这里插入图片描述
(2)以文件的方式添加apache服务(永久性)

[root@localhost Desktop]# vim /etc/firewalld/zones/public.xml
#########################
8   <service name="http"/>   ##添加http服务

在这里插入图片描述

##重启火墙
[root@localhost Desktop]# systemctl restart firewalld
##可查看到http服务成功添加到火墙策略中
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述
添加apache服务实际上是在/usr/lib/firewalld/services/http.xml 文件中添加的80端口

##可查看到80端口
[root@localhost services]# vim /usr/lib/firewalld/services/http.xml 

在这里插入图片描述
4.firewalld的访问限制

(1)仅允许26主机仅可以访问apache服务

实验环境:

[root@localhost Desktop]# rpm -q httpd
httpd-2.4.6-40.el7.x86_64
[root@localhost Desktop]# systemctl status httpd

在这里插入图片描述

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

在这里插入图片描述
实验:

##仅允许26主机仅可以访问apache;80为apache的端口
[root@localhost Desktop]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.26 -j ACCEPT
success

具体参数的含义:
 --direct          #direct规则
 filter            #filter表
INPUT 1            #第一条链
-p tcp             #tcp协议
--dport 80         #目的地端口为80
-s                 #设定被允许访问的ip
-j                 #要执行的动作,ACCEPT表示接受,DROP表示丢弃,不回显,REJECT表示拒绝,回显

##查看所有的direct规则
[root@localhost Desktop]# firewall-cmd --direct --get-all-rules 
ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.26 -jACCEPT

在这里插入图片描述
测试:

在26主机中:
在这里插入图片描述
在其他主机中:
在这里插入图片描述
(2).仅允许26主机可以ssh连接本机

@默认所有人都可以ssh连接本机,因为火墙策略中添加了ssh服务
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述

##1.删除ssh服务,此时所有人均无法ssh连接本机
[root@localhost Desktop]# firewall-cmd --remove-service=ssh --permanent 
success
[root@localhost Desktop]# firewall-cmd --reload 
success
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述

##发现此时所有主机都无法来连接
[root@localhost Desktop]# ssh root@172.25.254.126
ssh: connect to host 172.25.254.126 port 22: No route to host

在这里插入图片描述

[root@foundation26 ~]# ssh root@172.25.254.126
ssh: connect to host 172.25.254.126 port 22: No route to host

在这里插入图片描述

##2.仅允许26主机可以ssh连接本机;22为sshd服务的端口
[root@localhost Desktop]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j ACCEPT
success
##查看所有的direct规则
[root@localhost Desktop]# firewall-cmd --direct --get-all-rules 
ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.26 -j ACCEPT
ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j ACCEPT

在这里插入图片描述
测试:

在26主机中:

[root@foundation26~]# ssh root@172.25.254.126
root@172.25.254.126's password: 
Last login: Tue Dec 11 20:24:40 2018 from 172.25.254.26

在这里插入图片描述
在其他主机中:

[root@localhost Desktop]# ssh root@172.25.254.126
ssh: connect to host 172.25.254.126 port 22: No route to host

在这里插入图片描述
还原:(删除策略)

[root@localhost Desktop]# firewall-cmd --direct --get-all-rules 
ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.26 -j ACCEPT
ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j ACCEPT
[root@localhost Desktop]# firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.26 -j ACCEPT
success
[root@localhost Desktop]# firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j ACCEPT
success
[root@localhost Desktop]# firewall-cmd --direct --get-all-rules 

在这里插入图片描述
(3).仅拒绝26主机ssh连接本机

##1.仅拒绝26主机ssh连接本机
[root@localhost Desktop]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j REJECT
success
##查看所有的direct规则
[root@localhost Desktop]# firewall-cmd --direct --get-all-rules ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j REJECT

在这里插入图片描述

##查看策略
[root@localhost Desktop]# firewall-cmd --list-all
##2.添加ssh服务,允许所有主机ssh连接	
[root@localhost Desktop]# firewall-cmd --add-service=ssh 
success
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述
测试:

在26主机中:

[root@foundation26 ~]# ssh root@172.25.254.126
ssh: connect to host 172.25.254.126 port 22: Connection refused

在这里插入图片描述
在其他主机中:

[root@localhost Desktop]# ssh root@172.25.254.126
root@172.25.254.126's password: 
Last login: Sun Dec  9 11:04:24 2018 from 172.25.254.26

在这里插入图片描述
还原:(删除策略)

[root@localhost Desktop]# firewall-cmd --direct --get-all-rules
ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j REJECT
[root@localhost Desktop]# firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.26 -j REJECT
success

在这里插入图片描述
5.端口转发与在地址伪装

(1).端口转发

##由于ssh服务是之前临时添加的,重新加载后便会失效。所以先永久性添加ssh服务
[root@localhost Desktop]# firewall-cmd --add-service=ssh --permanent 
success
[root@localhost Desktop]# firewall-cmd --reload 
success
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述
默认ssh连接到哪台主机,便显示的是哪台主机的ip

[root@foundation26 ~]# ssh root@172.25.254.126
[root@localhost ~]# ifconfig eth0

在这里插入图片描述
实验:

##1.开启路由功能
[root@localhost Desktop]# firewall-cmd --add-masquerade --permanent 
success
[root@localhost Desktop]# firewall-cmd --reload 
success
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述

##2.端口转发;将本机的22端口转到226主机的22端口上(注意:226必须是可以ping通的ip)
[root@localhost Desktop]# firewall-cmd --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.226
success

具体参数含义:
port=22      ##表示22端口(ssh的端口)   
proto=tcp    ##表示tcp协议    
toport       ##表示将22端口转向22端口   
toaddr       ##表示转向ip

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

在这里插入图片描述
测试:

[root@foundation34 ~]# ssh root@172.25.254.126
[root@localhost ~]# ifconfig eth0

在这里插入图片描述
还原:(删除端口转发策略)

[root@localhost Desktop]# firewall-cmd --remove-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.226
success
[root@localhost Desktop]# firewall-cmd --list-all

在这里插入图片描述

(2)地址伪装

配置服务端:

1.配置双网卡ip

@1.再添加一个网卡
在这里插入图片描述
@2.给新添加的网卡配置ip

[root@localhost ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
###################
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=none
IPADDR=1.1.1.126
PREFIX=24

在这里插入图片描述

[root@localhost ~]# systemctl restart network
[root@localhost ~]# ip addr show 

在这里插入图片描述

2.查看到路由功能已经开启

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

在这里插入图片描述
配置客户端:

1.修改主机名

[root@localhost Desktop]# hostnamectl set-hostname client
[root@localhost Desktop]# hostname
client

2.配置ip和网关

[root@client Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
#########################
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=1.1.1.226
PREFIX=24
GATEWAY=1.1.1.126    ##网关(路由器的内网)

在这里插入图片描述

[root@client Desktop]# systemctl restart network
##查看ip
[root@client Desktop]# ifconfig eth0

在这里插入图片描述

##查看网关
[root@client Desktop]# route -n

在这里插入图片描述

##检测是否能ping通网关
[root@client Desktop]# ping 1.1.1.126

在这里插入图片描述
3.测试

##此时便可以ping通不在同一个网段的ip
[root@client Desktop]# ping 172.25.254.26

在这里插入图片描述

##注意:连接的主机最好不要是双网卡主机,这样便于观察现象
[root@client Desktop]# ssh root@172.25.254.26
##发现地址伪装了;原本是1.1.1网段的伪装成了172.25.254网段
[root@foundation26 ~]# w -i

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值