博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iptables+firewalld火墙策略优化
阅读量:3961 次
发布时间:2019-05-24

本文共 7549 字,大约阅读时间需要 25 分钟。

iptables+firewalld火墙策略优化

1.火墙介绍

  • 1.netfilter
    netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集。
  • 2.iptables
    IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。
    防火墙在做数据包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的数据包过滤表中,而这些表集成在 Linux 内核中。在数据包过滤表中,规则被分组放在我们所谓的链(chain)中。而netfilter/iptables IP 数据包过滤系统是一款功能强大的工具,可用于添加、编辑和移除规则。
    虽然 netfilter/iptables IP 信息包过滤系统被称为单个实体,但它实际上由两个组件netfilter 和 iptables 组成。
    netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集。
    iptables 组件是一种工具,也称为用户空间(userspace),它使插入、修改和除去信息包过滤表中的规则变得容易。
  • 3.iptables|firewalld
    iptables的防火墙策略是交由内核层面的netfilter网络过滤器来处理的,而firewalld则是交由内核层面的nftables包过滤框架来处理,相较于iptabels防火墙来说,firewalld支持动态更新技术并加入了区域的概念,即不同的防火墙策略集合,用户可以根据生产场景的不同而选择合适的测率集合,从而实现防火墙策略之间的快速切换。

2.火墙管理工具切换

在rhel8中默认使用的是firewalldfirewalld----->iptablesdnf install iptables-services -ysystemctl stop firewalldsystemctl disable firewalld systemctl mask firewalld systemctl enable --now iptablesiptales -------> fiewalld dnf install firewalld -ysystemctl stop iptablessystemctl disable iptablessystemctl mask iptablessystemctl enable --now 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,)

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

数据包状态

RELATED ##建立过连接的
ESTABLISHED ##正在连接的
NEW ##新的

[root@node1 named]# iptables  -nL  ##默认是filter表 n表示不做解析[root@node1 named]# iptables -t nat -nL[root@node1 named]# iptables -t mangle -nL[root@node1 named]# iptables -t filter -L[root@node1 named]# vim /etc/sysconfig/iptables[root@node1 named]# service iptables save[root@node1 named]# iptables -F    ##清理 [root@node1 named]# service iptables save  ##永久清理[root@node1 named]# systemctl restart iptables.service [root@node1 named]# iptables -nL[root@node1 network-scripts]# iptables -A INPUT -p tcp --dport 53 -j ACCEPT[root@node1 network-scripts]# iptables -A INPUT -j REJECT[root@node1 network-scripts]# iptables -nL[root@node1 network-scripts]# iptables -D  INPUT 2 ##删除第2条[root@node1 network-scripts]# iptables -I  INPUT 2 -p tcp --dport 22 -j ACCEPT  ##ping不通 但是ssh能连接

[root@node1 network-scripts]# iptables -R INPUT 2 ! -s 172.25.254.250 -p  tcp --dport 22 -j ACCEPT  ##不允许250访问[root@node1 network-scripts]# iptables -N westos  ##新建一个链[root@node1 network-scripts]# iptables -E westos REDHAT  ##更改链名称[root@node1 network-scripts]# iptables -E REDHAT westos[root@node1 network-scripts]# iptables -X westos   ##删除[root@node1 network-scripts]# iptables -P INPUT  DROP[root@node1 network-scripts]# iptables -P INPUT ACCEPT  ##更改默认规则
[root@node1 named]# iptables -nL[root@node1 named]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT[root@node1 named]# iptables -A INPUT -i lo -m state --state NEW  -j ACCEPT[root@node1 named]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW  -j ACCEPT  ##dns53  httpd 80 sshd 22 [root@node1 named]# iptables -A INPUT -p tcp --dport 80 -m state --state NEW  -j ACCEPT  ##[root@node1 named]# iptables -A INPUT -m state --state NEW  -j ACCEPT[root@node1 named]# iptables -nL[root@node1 named]# iptables -D INPUT 5[root@node1 named]# iptables -A INPUT -m state --state NEW  -j REJECT[root@node1 named]# iptables -nL
1.设定建立和正在连接的数据包的火墙策略为允许iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT2.设定本机的回环接口允许新的数据包iptables -A INPUT -m state --state NEW -i lo -j ACCEPT3.对于新的指定服务的数据包添加火墙策略:设定为允许iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPTiptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPTiptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT4.添加火墙策略。设定除指定ip的所有主机都可以进行的服务iptables -A INPUT -m state --state NEW ! -s 192.168.0.10 -p tcp --dport 22 -j ACCEPTiptables -A INPUT -m state --state NEW -j REJECTservice iptables  save  %火墙策略永久保存注意:iptables命令添加策略时优先级是从上至下nat表中的dnat snatsnatiptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.0.20 ##外网dnatiptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 172.25.254.30 ## 内网

firewalld

1 firewalld的开启

systemctl stop iptables systemctl disable iptablessystemctl mask iptables systemctl unmask firewalldsystemctl enable --now firewalld

2.关于firewalld的域

[root@node1 ~]# sysctl -a | grep ip_forwardnet.ipv4.ip_forward = 1trusted		##接受所有的网络连接home		##用于家庭网络,允许接受ssh mdns ipp-client samba-client dhcp-clientwork		##工作网络 ssh ipp-client dhcp-clientpublic		##公共网络 ssh dhcp-clientdmz		    ##军级网络 sshblock		##拒绝所有drop		##丢弃	所有数据全部丢弃无任何回复internal	##内部网络 ssh mdns ipp-client samba-client dhcp-clientexternal	##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 ##更改网络接口到指定域

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 ##设定只有172.25.254.250主机可以连接本机。

6.firewalld中的NAT

SNATfirewall-cmd --permanent --add-masqueradefirewall-cmd --reload DNATfirewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.254.30firewall-cmd --reload
[root@node1 firewalld]# firewall-cmd --permanent  --direct  --get-all-rules [root@node1 firewalld]# firewall-cmd --permanent  --direct  --add-rule ipv4 filter INPUT 1 -s 172.25.254.250 -p tcp --dport 22 -j REJECT   ##250 ssh no  [root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd --direct  --get-all-rules[root@node1 firewalld]# firewall-cmd --list-all[root@node1 firewalld]# firewall-cmd --permanent  --direct  --remove-rule ipv4 filter INPUT 1 -s 172.25.254.250 -p tcp --dport 22 -j REJECT[root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd  --direct  --get-all-rules[root@node1 firewalld]# firewall-cmd --permanent  --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.212:toport=22 ##目的地址转换   [root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd  --list-all[root@node1 firewalld]# firewall-cmd --permanent --add-masquerade  ##原地址转换 [root@node1 firewalld]# firewall-cmd --reload[root@node1 firewalld]# firewall-cmd  --list-all

补充

Cockpit 是红帽开发的网页版图像化服务管理工具,优点是无需中间层,且可以管理多种服务。根据其项目主站描述,Cockpit 有如下特点:    从易用性考虑设计,方便管理人员使用,而不是仅仅的终端命令按钮化。    不会打乱已有终端或脚本服务配置,通过 Cockpit 启用的服务可以在终端停止,脚本运行的错误亦会被 Cockpit 捕获。    支持一次性管理多个服务,实现自动化和批处理。

转载地址:http://fnhzi.baihongyu.com/

你可能感兴趣的文章
简单邮件系统程序
查看>>
STL里的multimap使用详解
查看>>
STL 库其中的 std::string用法总结
查看>>
模态对话框的销毁过程与非模态对话的几种销毁方法
查看>>
C++实现http下载 && 24点计算编码风格
查看>>
memcached了解使用和常用命令详解
查看>>
GDB调试各功能总结
查看>>
"undefined reference to" 多种可能出现的问题解决方法
查看>>
类结构定义
查看>>
Windows下关于多线程类 CSemaphore,CMutex,CCriticalSection,CEvent,信号量CSemaphore的使用介绍
查看>>
图像处理基本算法(汇总)以及实现
查看>>
C++编程获取本机网卡信息 本机IP 包括Windows和Linux
查看>>
C++连接CTP接口实现简单量化交易
查看>>
服务端使用c++实现websocket协议解析及通信
查看>>
C# string.Format使用说明
查看>>
Linux下安装Mysql数据库开发环境
查看>>
Linux用户及用户组添加和删除操作
查看>>
通用 Makefile 的编写方法以及多目录 makefile 写法
查看>>
C++的4种智能指针剖析使用
查看>>
RPC框架实现之容灾策略
查看>>