Linux中的火墙策略优化

本文详细介绍了如何在firewalld和iptables间切换,包括操作步骤,并深入讲解iptables的使用、默认策略、firewalld的域和管理命令,以及高级规则和NAT功能。涵盖了firewalld的多种应用场景和配置技巧。

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

一、火墙介绍

1.netfilter
2.iptables
3.iptables|firewalld

二、火墙管理工具切换

1.从firewalld----->iptables

首先要下载 dnf instal iptables-services -y
然后在写

systemctl status firewalld	#查看防火墙状态
systemctl disable firewalld
systemctl mask firewalld	#冷藏防火墙
systemctl enable --now iptables	#开启iptables表格,这样就从firewalld转换到iptables了

在这里插入图片描述

2.从iptales -------> fiewalld

systemctl disable --now iptables	#关闭iptables
systemctl mask iptables	#冷藏iptables
systemctl unmask firewalld	#取消对防火墙的冷藏
systemctl enable --now firewalld	#开启防火墙
systemctl status firewalld	#查看防火墙状态

在这里插入图片描述

三、iptables 的使用

#火墙策略的永久保存#
/etc/sysconfig/iptables
#iptables 策略记录文件
永久保存策略
iptales-save > /etc/sysconfig/iptables
service iptables save

四、火墙默认策略

默认策略中的5条链

input	#输入
output	#输出
forward	#转发
postrouting	#路由之后
prerouting	#路由之前

默认的3张表

filter	#经过本机内核的数据(input output forward)
nat	#不经过内核的数据(postrouting,prerouting,input,output
mangle	#当filter和nat表不够用时使用(input output forward postrouting,prerouting,)

iptables命令

iptables
		-t	#指定表名称
		-n	#不做解析
		-L	#查看
		-A	#添加策略
		-p	#协议
		--dport	#目的地端口
		-s	#来源
		-j	#动作
			ACCEPT	#允许
			DROP	#丢弃
			REJECT	#拒绝
			SNAT	#源地址转换
			DNAT	#目的地地址转换
		-N	#新建链
		-E	#更改链名称
		-X	#删除链
		-D	#删除规则
		-I	#插入规则
		-R	#更改规则
		-P	#更改默认规则

在这里插入图片描述
#查看filter表的规则
(没加-t,默认filter表)
在这里插入图片描述
如果删除表内容,可以重启服务来恢复

iptables -nL	#查看filter表的规则
iptables -t filter -F	#清空filter表规则
iptables -t filter -nL	#(没加-t,默认filter表)
systemctl restart iptables.service	#重启服务
iptables -t filter -nL	#会发现被清空的表规则都回来了

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这是因为配置文件没发生改变
是/etc/sysconfig/iptables
如果我更改了配置,则

iptables -t filter -F
service iptables save	#保存所有配置
iptables-save
iptables-save > /etc/sysconfig/iptables	#将保存的导入配置文件,会发现被修改
systemctl restart iptables.service 
iptables -nL

在这里插入图片描述

火墙是从上到下
读一条之后,后面就不读了

数据包状态

RELATED	#建立过连接的
ESTABLISHED	#正在连接的
NEW	#新的

拒绝别人访问

iptables -A INPUT -j ACCEPT	#-A为添加规则 -j规则要求
iptables -nL

iptables -D INPUT 1
iptables -nL

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

iptables -A INPUT -J REJECT	#拒绝别人进入
iptables -A INPUT -p tcp --dport 22 -j ACCEPT	#允许22端口的进入
iptables -nL	#查看
iptables -D INPUT 2	#删除2

iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT	#-I为插入,插入到第一条前面
会发现可以ping了,因为火墙是从上到下
读一条之后,后面就不读了
iptables -R INPUT 1  -s 172.25.254.0.0/24 -p tcp --dport 22 -j ACCEPT	#让这个网段的能ping通
iptables -nL	#查看

因为防火墙规则是先读上再读下

nat表中的dnat snat
iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.141	#将出去的伪装为172.25.254.120
iptables -t nat -A PREROUTING -i ens3 -j DNAT --to-dest 172.25.41.200	#将进来的伪装成172.25.41.200	

五、关于firewalld的域

trusted	#接受所有的网络连接
home	#用于家庭网络,允许接受ssh mdns ipp-client samba-client dhcp-client
work	#工作网络 ssh ipp-client dhcp-client	
public	#公共网络 ssh dhcp-client
dmz	#军级网络 ssh
block	#拒绝所有
drop	#丢弃所有数据全部丢弃无任何回复
internal	#内部网络 ssh mdns ipp-client samba-client dhcp-client
external	#ipv4网络地址伪装转发 sshd

六、firewalld的管理命令

firewall-cmd --state
##查看火墙状态
firewall-cmd --get-active-zones ##查看当前火墙中生效的域
firewall-cmd --get-default-zone ##查看默认域
firewall-cmd --list-all
##查看默认域中的火墙策略
firewall-cmd --list-all --zone=work ##查看指定域的火墙策略
firewall-cmd --set-default-zone=trusted ##设定默认域
firewall-cmd --get-services
##查看所有可以设定的服务
firewall-cmd --permanent --remove-service=cockpit
firewall-cmd --reload
##移除服务
firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=block ##指定数据来源访问指定域
firewall-cmd --reload
firewall-cmd --permanent --remove-source=172.25.254.0/24 --zone=block ##删除自定域中的数据来源
firewall-cmd --permanent --remove-interface=ens224 --zone=public ##删除指定域的网络接口
firewall-cmd --permanent --add-interface=ens224 --zone=block
##添加指定域的网络接口
firewall-cmd --permanent --change-interface=ens224 --zone=public ##更改网络接口到指定域

七、firewalld的高级规则

firewall-cmd --direct --get-all-rules
##查看高级规则
firewall-cmd --direct --add-rule ipv4 filter INPUT 0 ! -s 172.25.254.250 -p tcp –
dport 22 -j REJECT

八、firewalld中的NAT

SNAT
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
DNAT
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.254.30
firewall-cmd --reload

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值