linux中的火墙优化策略

1.火墙介绍

  1. netfilter
  2. iptables
  3. iptables | firewalld

2.火墙管理工具切换

在rocky9 中默认使用的是 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 firewall

安装iptables

开启iptables

切回firewalld

3. iptables 的使用

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

删除策略

重启服务后 策略恢复

永久删除策略

4.火墙默认策略

默认策略中的5条链
input

输入

output

输出

forward

转发

postrouting ## 路由之后
prerouting ## 路由之前
默认的 3 张表
filter ## 经过本机内核的数据 ( input output forward )
nat ## 不经过内核的数据 ( postrouting , prerouting,input,output )
mangle ## 当 filter 和 nat 表不够用时使用 ( input output forward postrouting , prerouting, )

查看nat表

查看mangle表

iptables 命令
iptables
- t ## 指定表名称
- n ## 不做解析
- L ## 查看
- A ## 添加策略
- p ## 协议
-- dport ## 目的地端口
- s ## 来源
- j

动作

ACCEPT ## 允许
DROP ## 丢弃
REJECT ## 拒绝
SNAT ## 源地址转换
DNAT ## 目的地地址转换 - N ## 新建链
- E ## 更改链名称
- X ## 删除链
- D ## 删除规则
- I

插入规则

- R ## 更改规则
- P ## 更改默认规则

添加策略

为filter表的input添加拒绝掉来自1.1.1.60的数据通过80端口

插入允许

删除策略

删除input的第二条策略

修改策略

加链

默认只有三个

增加

删除链

5.iptables数据状态跟踪

数据包状态
RELATED ## 建立过连接的
ESTABLISHED ## 正在连接的
NEW ## 新的

允许与已有连接相关或已建立的连接、本机内部的新连接请求、以及到本机80、22和53端口的新连接请求。对于所有其他新的连接请求,防火墙会拒绝它们

6.nat表中的dnat snat

snat
iptable - t nat - A POSTROUTING - o ens160 - j SNAT -- to - source 192.168.0.20
dnat
iptables - t nat - A PREROUTING - i ens160 - j DNAT -- to - dest 172.25.254.30

snat

dnat

完成后用192网段连接R4实际是接入1.1.1.60主机

7.firewalld

1 firewalld的开启

systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl unmask firewalld
systemctl enable -- now firewalld
修改配置文件

2.关于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

3.关于firewalld的设定原理及数据存储

/ etc / firewalld ## 火墙配置目录
/ lib / firewalld ## 火墙模块目录

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

图形管理

命令方式

查看状态

查看生效的域

查看默认域

修改默认域

查看域

添加服务

指定1.1.1.60访问域

5. 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

类似iptables

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

源地址转换

开启后192网段的主机可以ping通1.1.1网段的主机。

目的地地址转换

相关推荐
郑州光合科技余经理16 小时前
定制开发实战:海外版外卖系统PHP全栈解决方案
java·服务器·开发语言·javascript·git·uni-app·php
郝学胜-神的一滴16 小时前
Linux线程编程:从原理到实践
linux·服务器·开发语言·c++·程序人生·设计模式·软件工程
_OP_CHEN16 小时前
【Linux系统编程】(十四)深入 Linux 内核:进程优先级调度与切换的底层逻辑全解析
linux·运维·linux内核·进程·进程切换·进程优先级·调度算法
Orange裴16 小时前
Kali linux2025.3 安装nessus(Mac M4芯片)
linux·运维·macos·kali linux
✿ ༺ ོIT技术༻16 小时前
服务端高并发分布式结构演进之路
运维·服务器·redis·分布式·架构
..空空的人16 小时前
C++基于protobuf实现仿RabbitMQ消息队列---技术认识2
服务器·数据库·c++·网络协议·gtest·异步·protobuf
jinxinyuuuus16 小时前
GTA 风格 AI 生成器:提示词工程、LLM创造性联想与模因的自动化生成
运维·人工智能·自动化
派大鑫wink16 小时前
Python 自动化办公实战:Excel 批量处理 + 邮件自动发送(附完整源码)
linux·windows·github
RPA机器人就用八爪鱼16 小时前
RPA重塑IT运维:自动化破局数字化转型痛点
运维·自动化·rpa
Sleepy MargulisItG17 小时前
【Linux网络编程】TCP Socket
linux·网络·tcp/ip