linux中的火墙优化策略
一.火墙介绍
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
iptables -------> fiewalld
dnf install firewalld -y
systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl enable --now firewalld
三. iptables 的使用
1.火墙策略保存
火墙策略的永久保存
/etc/sysconfig/iptables iptables 策略记录文件
永久保存策略
iptales-save > /etc/sysconfig/iptables
service iptables save
2.火墙默认策略
默认策略中的5条链
input 输入
output 输出
forward 转发
postrouting 路由之后
prerouting 路由之前
默认的3张表
filter 经过本机内核的数据(input output forward)
nat 不经过内核的数据(postrouting,prerouting,input,output)
mangle 当filter和nat表不够用时使用(input output forward postrouting,)
3.iptables命令
iptables
-F 清空iptables
-t 指定表名称
-n 不做解析
-L 查看
-A 添加策略
-p 协议
--dport 目的地端口
-s 来源
-j 动作
ACCEPT 允许
DROP 丢弃
REJECT 拒绝
SNAT 源地址转换
DNAT 目的地地址转换
-N 新建链
-E 更改链名称
-X 删除链
-D 删除规则
-I 插入规则
-R 更改规则
-P 更改默认规则
清空iptable 指定表名称
查看指定表
允许指定ip访问
-I INPUT 2 插入指定次序规则(INPUT 默认第一行)
指定tcp协议
指定dport
指定sport
新建westos链
删除westos链
4.数据包状态
RELATED 建立过连接的
ESTABLISHED 正在连接的
NEW 新的
设置只有 dns 和 ssh能访问
5.nat表中的dnat snat
更改ip_forward
snat
254.10----------(254.110 0.110)---------0.210
iptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 172.25.254.110
dnat
254.10---------(254.110 0.110)------0.210
iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 172.25.0.210
三.firewalld的使用
1.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
2.firewalld的设定原理及数据存储
/etc/firewalld 火墙配置目录
/lib/firewalld 火墙模块目录
3. 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 更改网络接口到指定域
更改默认域
查看火墙服务
允许指定ip访问
查看生效域
地址伪装
4.firewalld中的NAT
所有配置皆在双网卡主机(0.21 254.210网段)
SNAT
0.110(网关0.210)------>(0.210 254.210)-------->254.10
从内网访问外网
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
在0.110进行测试
DNAT
254.10----->(254.210 0.210)-------->0.110
从外网进入内网
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.0.110
firewall-cmd --reload
在254.10测试