IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Linux中的火墙策略优化 -> 正文阅读

[系统运维]Linux中的火墙策略优化

1.实验环境配置

一台双网卡主机westosa:ip 172.25.254.109 和 1.1.1…109
一台单网卡主机westosb:ip 1.1.1.209

#westosa

cd /etc/sysconfig/network-scripts
vim ifcfg-ens3
///
DEVICE=ens3
ONBOOT=YES
BOOTPROTO=none
IPADDR=172.25.254.103
PREFIX=24
///

vim ifcfg-en11
///
DEVICE=ens11
ONBOOT=YES
BOOTPROTO=none
IPADDR=1.1.1.103
PREFIX=24
///
nmcli connection reload
nmcli connection up System ens3
nmcli connection up System ens11
ifconfig

#westosb

cd /etc/sysconfig/network-scripts
vim ifcfg-ens3
///
DEVICE=ens3
ONBOOT=YES
BOOTPROTO=none
IPADDR=1.1.1.203
PREFIX=24
///
nmcli connection reload
nmcli connection up System ens3
///

网络软件仓库配置

2.火墙管理工具切换
firewalld----->iptables
dnf install iptables-services -y
systemctl disable firewalld           #关闭firewall服务
systemctl mask firewalld              #冻结firewall服务
systemctl enable --now iptable.service        #开启iptables服务
systemctl status iptable.service
systemctl disable --now iptable.service
systemctl mask iptables.service
3.iptables的使用

/etc/sysconfig/iptables #iptables 策略记录文件

<1> 永久保存策略:
iptables-save > /etc/sysconfig/iptables
service iptables save
<2> 默认策略中的5条链
input #输入
output #输出
forward #转发
postrouting #路由之后
prerouting #路由之前

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

<4> iptables 命令

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

实验iptables命令:

iptables -t filter -L                ##-L查看指定filter表,可以看到表中包含的经过本机内核的数据链input、output、forward
iptables -t filter -nL              ##-nL查看指定filter表,可以看到表中的源地址、目的地地址未做解析
iptables -t filter -F               ##使用-F参数清空filter表
iptables -t filter -nL             ##-nL查看filter表可以看到表中无数据

systemctl restart iptables.service         ##重启iptables服务可以重新查看到表中数据
iptables  -nL                                      ##-nL不指定表名称时默认查看的是filter表
iptables -t filter -nL                           ##使用-t参数可以指定其他需要进行操作的表

防火墙中传输的数据包有以下三种状态,为了提高火墙数据传输速度,我们可以跟踪防火墙中数据包状态,根据数据包的状态设定火墙策略。

数据包状态:
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    #接受所有通过80端口、使用tcp协议进行访问请求的新的数据包		
iptables -A INPUT -m state --state NEW  -p tcp --dport 22 -j ACCEPT    #接受所有通过22端口、使用tcp协议进行访问请求的新的数据包
iptables -A INPUT -m state --state NEW  -j ACCEPT     #拒绝其他所有新的数据包(-m参数表示指定特殊匹配模式)
iptables-save > /etc/sysconfig/iptables

<5> SNAT源地址转换

nat表中的dnat snat

snat

iptable -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 172.25.254.103

dnat

iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 172.25.254.203

SNAT源地址转换是在路由之后POSTROUTING链中进行的
实验:

iptables -F     ##-F清空之前filter表的设定,避免影响实验效果
iptables -t nat -nL
iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.103  ##在nat表的POSTROUTING链中添加策略,指定对所有从ens3接口(254网段)输出的数据SNAT转换其源地址为172.25.254.103 

做完规则设定后还要检查内核路由功能是否开启

sysctl -a | grep ip_forward  ##检查内核路由功能是否开启
///
net.ipv4.ip_forward = 0		##内核路由功能,0表示关闭
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
///
vim /etc/sysctl.conf 
///
net.ipv4.ip_forward =1
///
sysctl -p  ##使开启设定生效

测试:
此时在虚拟机westosb(1.1.1.203)中可以ping通真实主机,也可以远程连接上真实主机,连接后(w -i)显示的是从westosa的172.25.254.103连接的真实主机(-F清空之前filter表的设定,避免影响实验效果).

4.firewalld 的使用

<1> firewalld的开启

systemctl stop iptables 
systemctl disable iptables
systemctl mask iptables 

systemctl unmask firewalld
systemctl enable --now firewalld 

火墙图形管理
dnf install firewall-config-0.8.0-4.el8.noarch -y
firewall-cmd --reload
<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 ##火墙配置目录
/usr/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  #查看指定域的火墙策略
irewall-cmd --set-default-zone=trusted   #设定默认域

firewall-cmd --get-service   #查看所有可以设定的服务
firewall-cmd --permanent --remove-service=cockpit  #移除服务
firewall-cmd --reload
firewall-cmd --permanent --add-source=172.25.254.103/24 --zone=block  #指定数据来源访问指定域
firewall-cmd --reload
firewall-cmd --permanent --remove-source=172.25.254.103/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 1 ! -s 172.25.254.3 -p tcp --dport 80 -j REJECT    #设定规则172.25.254.3可以访问此服务端
firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 '!' -s 172.25.254.3 -p tcp --dport 80 -j REJECT       #删除规则

<6> firewalld中的NAT
SNAT

firewall-cmd --permanent --add-masquerade      #添加地址伪装
firewall-cmd --reload

单网卡主机可以ping通双网卡主机中的两个ip

DNAT

firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=172.25.254.103   #伪装源地址到172.25.254.103
firewall-cmd --reload
firewall-cmd --permanent --remove-forward-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.103	##删除地址转换

用真机连双网卡主机的1.1.1.103ip ,ifconfig看到的ip是172.25.254.103

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-08-13 12:43:23  更:2021-08-13 12:47:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 19:08:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码