admin管理员组文章数量:1642348
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
版权声明:本文标题:firewalld防火墙的配置管理 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1729334964a1196860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论