Linux中的火墙策略优化(iptables,firewalld)

一、火墙介绍

1.netfilter
2.iptables
3.iptables | firewalld

二、火墙管理工具切换

在rhel8中默认使用的是firewalld

firewalld----->iptables

dnf install iptables-services -y
systemctl stop firewalld
systemctl disable firewalld
systemctl mask firewalld
systemctl enable --now iptables

iptales -------> fiewalld
dnf install firewalld -y
systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl enable --now 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命令
-t指定表名称
-n不做解析
-L查看
-A添加策略
-p协议
--dport

目的地端口

-s来源
-j动作
ACCEPT允许
DROP丢弃
REJECT拒绝
SNAT源地址转换
DNAT目的地地址转换
-N新建链
-E更改链地址
-X删除链
-D删除规则
-I插入规则
-R更改规则
-P更改默认规则
数据包状态
RELATED   建立过连接的
ESTABLISHED正在连接的
NEW新的
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT ## 所有建立过连接的或者正在连接的数据包都被接受
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT   ##属于自身回环的新数据包都被接受
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -m state --state NEW ! -s 192.168.0.10 -p tcp --dport 22 -j ACCEPT
ptables -A INPUT -m state --state NEW -j REJECT
service iptables  save
nat表中的dnat snat

snat
iptables -t nat -A POSTROUTING -o enp1s0 -j SNAT --to-source 172.25.254.101

dnat
iptables -t nat -A PREROUTING -i enp1s0 -j DNAT --to-dest 1.1.1.201

SNAT:局域网共享一个公网IP接入lnternel,好处如下

1、保护内网用户安全,因为公网地址总有一些人恶意扫描,而内网地址在公网没有路由所以无法被扫描,能被扫描的只有防火墙这一台,这样就减少了被攻击的可能。

2、Ipv4地址匮乏,很多公司只有一个ipv4地址,但是却有几百个用户需要上网,这个时候就需要使用SNAT。

3、省钱,公网地址付费,使用SNAT只需要一个公网ip就可以满足几百人同时上网。

DNAT:向internel发布内网服务器

在内网中有服务器,如果想让公网用户访问有有两种方法。

1.配置双网卡,一网卡对内,一网卡对外;一般是高访问量的web服务器,为了避免占用网关的流量才这样做,使用不是很广泛

2.内网web服务器,或是ftp服务器,为了用户在公网也可以访问,有不想买公网ip地址,采用DNAT方案。

 

SNAT

实验环境:

server:双网卡  172 网段 和1 网段

 client:单网卡1.1.1段的

测试:

 

DNAT:

实验环境:网卡配置和上步实验相同

iptables -t nat -A PREROUTING -i enp1s0 -j DNAT --to-dest 1.1.1.201

测试:

使用真机ping 1网段

 

问题: 如果实现不了,试试重启一下网卡。

firewalld

## 1 firewalld的开启 ##
systemctl stop iptables 
systemctl disable iptables
systemctl mask iptables 


systemctl unmask firewalld
systemctl enable --now firewalld 

 

关于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
externalipv4网络地址伪装转发 sshd
关于firewalld的设定原理及数据存储
/etc/firewalld火墙配置目录
/lib/firewalld火墙模块目录

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的高级规则

实验时要把http加到火墙服务里面
firewall-cmd --direct --get-all-rules	##查看高级规则 
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 ! -s 172.25.254.1 -p tcp --dport 80  -j REJECT    #不能直接写允许,所以拒绝所有人的形式允许172.25.254.1一个
!除了
curl http://172.25.254.101

 测试:只允许172.25.254.1访问,其他都拒绝

 

 

 

firewalld中的NAT

SNAT
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload

DNAT
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.201
firewall-cmd --reload

测试方式:和iptables 一样

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值