基于linux下的firewalld的管理

本文详细介绍了Firewalld防火墙的安装配置方法,包括服务管理、动态管理命令使用、实验演示及高级配置如地址伪装等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

•系统提供了三种配置方法:(1)图像化的配置工具 firewall-config ;(2) 命令行工具 firewall-cmd;(3)直接编辑xml文件;

首先,区分一下firewalld和iptables services

• firewalld 和 iptables service 之间最本质的不同是 :
• iptables service 在 /etc/sysconfig/iptables 中储存配置
• firewalld 将配置储存在 /usr/lib/firewalld/ 和
/etc/firewalld/ 中的各种 XML 文件里 .


下面我们介绍命令行怎么配置firewalld?

安装配置

//安装
yum install firewalld firewall-config
//服务管理
systemctl start firewalld  ##启动防火墙
systemctl stop firewalld   ##停止防火墙
systemctl disable firewalld  ##停用防火墙
systemctl status firewalld  ##查看当前防火墙的状态

firewalld的管理方式

firewall-cmd --state //查看状态
firewall-cmd --get-active-zone //查看当前生效的域
firewall-cmd --get-default-zone  //查看火墙默认生效的域
firewall-cmd --get-zones  //查看所有的域
firewall-cmd --zone=public --list-all //查看public的域的信息
                    block
firewall-cmd --get-services  //列出系统中用名称表示的服务
firewall-cmd --list-all-zones  //所有域的状态
firewall-cmd --list-all   //列出所有域的规则
firewall-cmd --set-default-zone=trusted  //修改默认的为trusted域
firewall-cmd --reload  //更新防火墙规则
firewall-cmd --complete-reload  
######它与--reload的区别就是第一个无需断开连接,第二个需要断开连接,类似重启服务
firewall-cmd -get-zone-of-interface=eth0  //查看eth0的域
firewall-cmd --change-interface=eth0 --zone=trusted  //改变eth0的域
firewall-cmd --remove-interface=eth0 --zone=trused   //移除
firewall-cmd --add-source=ip  //临时添加一个ip源
firewall-cmd --permanent --add-source=ip  //永久添加一个ip源
firewall-cmd --add-port=8080/tcp --zone=public  //添加一个端口8080

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

实验 A:

[root@client ~]# yum install httpd  //安装apppache
[root@client ~]# systemctl start httpd  //开启服务
[root@client ~]# echo westos > /var/www/html/index.html //在httpd服务的默认发布文件追加westos
[root@client ~]# firewall-cmd --set-default-zone=trusted
success

这里写图片描述
这里写图片描述
打开浏览器查询,输入172.25.254.174
这里写图片描述

实验 B:
在试验A 的基础上,完成下面操作:

[root@client ~]# firewall-cmd --set-default-zone=public
success
[root@client ~]# firewall-cmd --add-source=172.25.254.74 --zone=trusted
success
[root@client ~]# firewall-cmd --permanent --add-source=ip  //永久添加一个ip源

打开两个不同主机的浏览器查询(除了设置http的主机):ip为74的主机可以看到内容,其他的主机是看不到内容的

实验 C:
文件修改方式将http设置允许穿过火墙;

[root@client ~]# cd /etc/firewalld/zones
[root@client zones]# vim public.xml 
 <service name="http"/>
[root@client zones]# vim /usr/lib/firewalld/http.xml
 <port protocol="tcp" port="8080"/>   //修改端口为8080
[root@client zones]# systemctl restart firewalld
[root@client zones]# firewall-cmd --list-all  //设置允许http穿过火墙成功
[root@client ~]# firewall-cmd --add-port=8080/tcp --zone=public
[root@client ~]# firewall-cmd --list-all

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


设置访问权限

假设现在我们需要设置:只允许172.25.254.74这台主机可以访问;

[root@client ~] # firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

[root@localhost Desktop]# systemctl start httpd ##启动appache服务
[root@localhost Desktop]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.74 -j ACCEPT     ##只允许172.25.254.74访问
success
//解释:filter:表 INPUT:列 1:第几行 (先是表再是列) -p:正在使用的协议  --dport:目的地端口 -s:数据来源  -j: 动作  (有3种状态:REJECT 拒绝 ACCEPT 接受 DROP 丢弃)
[root@localhost Desktop]# firewall-cmd --direct --get-all-rule    ##查看规则
ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.74 -j ACCEPT

**注意**:对于动作drop和reject两者的区别要明确;
drop动作相当于是客户端对服务器发送访问请求,服务端产生回应,并拒绝当前的客户端进行访问;而,drop动作是客户端对服务器发送请求,但是服务器是不会给予回应的,相当于丢弃;

测试:首先对在172.25.254.74这台主机的浏览器进行测试(成功说明不了问题,我们也要在其他主机进行测试)
这里写图片描述
在172.25.254.247这台主机的浏览器进行测试:
这里写图片描述


跳转端口

[root@localhost Desktop]# firewall-cmd --permanent  --add-masquerade success
[root@localhost Desktop]# firewall-cmd --reload
success
[root@localhost Desktop]# firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.74
success
//ssh 的端口是 22;当ssh链接到172.25.254.174将会跳转到IP为172.25.254.74的主机
[root@localhost Desktop]# firewall-cmd --reload
success
[root@localhost Desktop]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: yes
  forward-ports: port=22:proto=tcp:toport=22:toaddr=172.25.254.74
  icmp-blocks: 
  rich rules: 

当我们闲置的虚拟机(server)中打开一个shell窗口,利用ssh服务连接172.25.254.174这台主机,会发现这台主机将会自动跳转到172.25.254.74,效果如图所示:
这里写图片描述


地址伪装

首先,在这台虚拟机(desktop)添加第二块网卡eth1;

//desktop(在含有2个网卡的主机中做如下配置)
[root@client Desktop]# cd /etc/sysconfig/network-scripts/
[root@client network-scripts]# cp ifcfg-eth0 ifcfg-eth1
cp: overwrite ‘ifcfg-eth1’? y
[root@client network-scripts]# vim ifcfg-eth1
[root@client network-scripts]# systemctl restart network
[root@client network-scripts]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.174  netmask 255.255.255.0  broadcast 172.25.4.255
        inet6 fe80::5054:ff:fe39:b057  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:39:b0:57  txqueuelen 1000  (Ethernet)
        RX packets 44535  bytes 10526793 (10.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1529  bytes 151845 (148.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.7.174  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fe34:af97  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:34:af:97  txqueuelen 1000  (Ethernet)
        RX packets 42928  bytes 5178592 (4.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26  bytes 3823 (3.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@client network-scripts]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
[root@client network-scripts]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1  //在末尾添加这一行;
[root@client network-scripts]# sysctl -p         ##启动
net.ipv4.ip_forward = 1
[root@client network-scripts]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0 eth1
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: yes          ##确保这个为开启状态
  forward-ports: port=22:proto=tcp:toport=22:toaddr=172.25.254.74        
  icmp-blocks: 
  rich rules: 

 ##用 firewall-cmd --permanent --remove-forwardport=port=22:proto=tcp:toport=22:toaddr=172.25.254.74  //删除
//server(单网卡主机)
[root@server]# vim /etc/sysconfig/network-scripts/ifcfg-eth0   ##更改IP为172.25.7.247
[root@server]# systemctl restart network  ##重启网络
[root@server]# ifconfig  //查看ip是否更改
[root@server]# route add default gw  172.25.7.174    ##添加网关(desktop里与server同网段的IP为这个ip的网关)
[root@server]# route -n     ##查看网关

这里写图片描述
在单网卡的主机(server)进行测试,看地址伪装是否成功:

[root@server]# ping 172.25.254.74     ##可以ping的通
[root@server]# ssh root@172.25.254.74            ##也可以连接

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

172.25.7.247和172.25.254.74因为不在一个网段里,网络是不能连通,这里为什么可以利用ssh服务可以连接呢?实际上是172.25.254.174登陆172.25.254.74,而不是172.25.7.247登陆172.25.254.74;这里也就完成了地址伪装。
当然,我们可以利用w -i 查询真正登陆的ip;

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值