1 、实验环境设定
第一台主机需要两个网卡

另一台主机一个网卡桥接到VMnet0上
第一台主机保证能够和windows保持连接
设定第一块儿网卡能够与Windows连接


设定第二台主机能够与第一台主机连接


二、火墙中的基本名词及知识
火墙就相当于是一个表格,这个表格里写着很多的信息,别的用户、服务在访问主机时,先访问的是火墙,先看他能不能通过火墙
netfilter 火墙所用的插件
iptables 往火墙表格里写东西、信息的工具
iptables / firewalld 用于操控iptables的软件
默认策略中的5条链
| input | 输入 |
|---|---|
| output | 输出 |
| forward | 转发 |
| postrouting | 路由之后 |
| prerouting | 路由之前 |
默认的3张表
| filter | 经过本机内核的数据(input output forward) |
|---|---|
| nat | 不经过内核的数据(postrouting,prerouting,input,output) |
| mangle | 当filter和nat表不够用时使用(input output forward postrouting,prerouting,) |
三、火墙管理工具的切换
在rhel8中默认使用的是firewalld
查看火墙规格
firewall-cmd --list-all

从firewalld切换到iptables
先安装iptables服务
dnf install iptables-services -y

关闭firewalld
systemctl disable --now firewalld

锁定firewalld
systemctl mask firewalld

开启iptabless服务
systemctl enable --now iptables.service
查看iptables服务
systemctl status iptables.service

切换成功
iptales -------> fiewalld
systemctl disable --now iptables.service
systemctl mask iptables.service
systemctl enable --now firewalld
四、iptables命令的常用参数
首先关闭firewalld 开启iptables服务
systemctl stop firewalld
systemctl disable firewalld
systemctl mask firewalld
systemctl unmask iptables
systemctl enable --now iptables

iptables命令
| -t | 指定表名称 |
|---|---|
| -n | 不做解析 |
| -L | 查看 |
| -A | 添加策略 |
| -p | 协议 |
| - -dport | 目的地端口 |
| -s | 来源 |
| -j | 动作 |
| -N | 新建链 |
| -E | 更改链名称 |
| -X | 删除链 |
| -D | 删除规则 |
| -I | 插入规则 |
| -R | 更改规则 |
| -P | 更改默认规则 |
| -F | 清理 |
参数语言含义
| ACCEPT | 允许 |
|---|---|
| DROP | 丢弃 |
| REJECT | 拒绝 |
| SNAT | 源地址转换 |
| DNAT | 目的地地址转换 |
五、数据包状态跟踪

状态跟踪必须要做优化,否则数据在通过火墙的时候会受到很大影响,从而降低数据通过效率,后面再做优化也是白费
六、iptables的nat
nat即网络地址转换(network address translation),主要用于在内部网络地址和外部网络地址间互相转换,从而让使用内部网络地址的设备或软件能够访问外部网络,或者让外部网络可以访问到没有外部网络地址的内部设备。nat一般运行在网关/路由设备上,内部设备访问外网或外部网络访问内部设备的报文都需要经过网关转发,网关在内外网转发报文的过程中执行nat转换。
iptables服务开启

设定的时候先将火墙刷掉
iptables -F

将双网卡的主机变为路由器
把所有从这台主机出去的信息IP地址伪装为41的
iptables -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.0.41

让1网段的可以ping通192网段

目的地地址转换
iptables -t nat -A POSTROUTING -i ens160 -j DNAT --to-dest 1.1.1.2
七、firewalld
(一)firewalld的开启

修改下核心模块
vim /etc/firewalld/firewalld.conf

重启服务
systemctl restart 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 |
| external | ipv4网络地址伪装转发 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的高级规则
firewall-cmd --direct --get-all-rules ##查看高级规则
firewall-cmd --direct --add-rule ipv4 filter INPUT 0 ! -s IP -p tcp --dport 22 -j
REJECT