Linux —— iptables和firewalld

目录

1. 火墙介绍

2. 火墙管理工具切换

2.1 将firewalld切换为iptables

2.2 将iptables切换为firewalld

3. iptables

3.1 iptables的使用

3.2 火墙默认策略

3.3 iptables的命令

3.4 数据包状态

3.5 nat表中的dnat和snat

4. firewalld

4.1 firewalld的开启

4.2 firewalld的域

4.3 firewalld的设定原理及数据存储

4.4  firewalld的管理命令

4.5 firewalld的高级规则

4.6 firewalld中的NAT


1. 火墙介绍

Netfilter/IPTables是Linux2.4.x之后新一代的Linux防火墙机制,是linux内核的一个子系统。Netfilter采用模块化设计,具有良好的可扩充性。其重要工具模块IPTables从用户态的iptables连接到内核态的Netfilter的架构中,Netfilter与IP协议栈是无缝契合的,并允许使用者对数据报进行过滤、地址转换、处理等操作。

管理方式iptables|firewalld

2. 火墙管理工具切换

在rhel8中默认为firewalld

2.1 将firewalld切换为iptables

  • 将firewalld服务关闭并冻结
systemctl stop firewalld
systemctl disable firewalld
systemctl mask firewalld

  • 安装并启用iptables服务
dnf search iptables
dnf install iptables-services.x86_64 -y
systemctl enable --now iptables.service

2.2 将iptables切换为firewalld

  • 将iptables服务关闭,并开启firewalld服务
systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl enable --now firewalld

3. iptables

3.1 iptables的使用

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

3.2 火墙默认策略

  • 默认策略中的5条链
作用
input输入
output输出
forward转发
postrouting路由之后
prerouting路由之前
  • 默认的3张表
功能
filter经过本机内核的数据(input output forward)
nat不经过内核的数据(postrouting prerouting input output)
mangle当filter和nat表不够用时使用该表(input output forward postrouting prerouting)

3.3 iptables的命令

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

测试:

iptables -t filter -L         ## 查看filter表,-t指定表,-L查看内容,显示主机名称
iptables -t filter -nL        ## 不做解析,显示的是ip
iptables -t nat    -nl
iptables -A                   ## 默认的表是filter,该命令指向filter表中添加策略
iptables -A INPUT -j REJECT   ## 添加该策略,指所有的主机到达当前主机都是被拒绝的
iptables -A INPUT -j DROP     ## 添加该策略,将数据丢弃,远程主机一直在尝试连接
iptables -I INPUT 1 -s 192.168.43.10 -j ACCEPT  ## 将该策略插入到第一个
iptables -I INPUT -s 192.168.43.10 -p tcp --dport 22 -j ACCEPT 

REJECT: 

 

DROP:

 REJECT all & ACCEPT 192.168.43.10

此时,先REJECT所有, 后ACCEPT ip:192.168.43.10主机连接,发现192.168.43.10主机还是无法连接,这里我们先进行ACCEPT ip:192.168.43.10主机连接再REJECT所有:

 

上述实验说明:火墙策略是由上向下依次读取,只要读到匹配的策略就停止,不再继续向下读

 

3.4 数据包状态

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 iptables -A INPUT -m state --state NEW -j REJECT 
service iptables  save

3.5 nat表中的dnat和snat

## snat 
iptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.43.20 
## 在路由之后通过网卡ens160出去的所有数据做源地址转换,将源地址都转换为192.168.43.20
## dnat 
iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 172.25.254.30
## 目的地地址转换,我们访问192.168.43.20路由主机IP会转换成172.25.254.30
  • 首先先添加一个网卡,并给它设置ip为172.25.254.20

  • 查看路由主机中路由功能是否开启,此时可以看到已经开启

  • 在ip为172.25.254.30主机无法ping通192.168.43.10,我们在路由主机中设置策略

  • 添加iptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.43.20源地址转换策略后,结果如下

上述策略可以使172.25.254.30主机访问192.168.43.10主机,那反着是不是也可以访问呢? 

可以发现,无法ping通,这是因为我们没有给其设置策略,允许访问172.25.254.30主机

  • 设置策略,使得192.168.43.10主机同样可以访问172.25.254.30主机

4. firewalld

4.1 firewalld的开启

## 关闭iptables
systemctl stop iptables 
systemctl disable iptables 
systemctl mask iptables 
## 开启firewalld
systemctl unmask firewalld 
systemctl enable --now firewalld 

4.2 firewalld的域

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

rhel8中默认域为public公共网络

4.3 firewalld的设定原理及数据存储

## 临时更改:
firewall-cmd --add-service=http 

## 永久更改(只更改/etc/firewalld/zones/public.xml文件内容):
firewall-cmd --permanent --add-service=http  
firewall-cmd --reload

/etc/firewalld ##火墙配置目录 
/lib/firewalld ##火墙模块目录
  • 临时更改,更改后立即生效,但是重启firewalld后就会消失

  • 永久更改,更改之后/etc/firewalld/zones/public.xml文件内容也会改变

反过来,通过修改配置文件,直接在配置文件中添加service也可以永久修改。

那么上述添加的都是我们系统中已存在的服务,如果如果想自行添加一个自定义服务?

  • 在配置文件中添加一个dsd服务,并查看服务列表

 可以看到,此时我们再重载服务时会报错,这是因为上述例如http、https等服务,系统中已经有明确的定义,而我们自定义的服务还没有定义 

  • 切换到/usr/lib/firewalld/services目录下,我们可以看到这里是所有系统服务定义的xml文件

  •  在这里我们编辑dsd.xml文件,复制一份系统服务文件,这里我们复制http.xml文件并修改如下

  • 查看火墙允许列表,发现自定义的服务已经被火墙所允许 

4.4  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=dsd  ##移除服务 

  • firewall-cmd --reload ## 更新防火墙规则
  • firewall-cmd --permanent --add-source=172.25.254.0/24 --zone=block ##指定数据来源访问指定域

  • 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 ##更改网络接口到指定域

4.5 firewalld的高级规则

firewall-cmd --direct --get-all-rules ##查看高级规则 
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.0/24 -p tcp -dport 22  -j ACCEPT

4.6 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

此时我们在主机:192.168.43.10中连接路由主机,发现访问的就是真实的路由主机,而非我们想要访问的172.25.254.30

  • 添加协议

  • 在192.168.43.10主机访问192.168.43.20路由主机,我们实际访问到的是172.25.254.30主机

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值