linux中的防火墙管理以及优化之iptables

一、防火墙介绍

系统的安全管理一般有两种服务:firewalld.service 和 iptables
两种服务是在一个系统中不可以同时启用的

二、firewalld 与 iptables的切换

iptables与firewalld的切换,其实就是在关闭系统默认使用的firewalld服务后,对iptables服务,安装并且启用。
1、先查询\安装iptables:

dnf serach iptables
dnf install iptables-services.x86_64

2、在启用iptables之前首先要关闭firewalld.service,冻结firewalld.service。

systemctl disable firewalld
systemctl stop firewalld
systemctl mask firewalld

3、在完成对firewalld.service的关闭与冻结后,开启iptables。

systemctl enable iptables
systemctl start iptables

同样,想要从iptables服务对的环境下,切换到firewalld服务的环境下,如同以上firewalld切换到iptables的操作,就可以。

三、iptables的默认策略

1、火墙策略默认的五条链(实际应用过程中不一定都可会用到)

input输入
output输出
forward转发
postrouting路由之后
prerouting路由之前

2.火墙策略中默认的3张表

filter经过本机内核的数据(input output forward)
nat不经过内核的数据(postrouting,prerouting,input,output)
mangel当filter和nat表不够用时使用(input output forward postrouting,prerouting)

四、IPtables的使用方式

在学习iptables的使用方法之前,我们先搭建学习所需要的操作环境:
计算机1的IP:172.25.254.37

计算机2的IP:
双网卡设置
ens3:1.1.1.137
ens9:172.25.254.137
计算机3的IP:1.1.1.237

1、iptables的命令使用方法

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

操作及含义:


在进行iptables命令使用方法的操作前,可以先开一个监控面板:
watch -n 1 iptables -nL ##用于监控iptables策略文件的变化,便于进行实验观察
```bash
iptables -L ##不解析显示表的内容
iptables -nL ##解析显示表的内容
iptables -F ##删除表的内容
service iptables save ##保存表的数据到/etc/sysconfig/iptabls
iptables save > /etc/sysconfig/iptables ##保存表的数据到/etc/sysconfig/iptabls
iptables -t filter -A INPUT -j ACCEPT  ## 添加策略
iptables -t filter -A INPUT -s 172.25.254.137 -j REJECT ##添加策略
iptables -t filter -A INPUT -s 1.1.1.137 -j REJECT ##添加策略
iptables -D INPUT 3 ##删除第三行策略
iptables -R INPUT 1 -j ACCEPT ##更改策略
iptables -R INPUT 2 -j ACCEPT##更改策略
iptables -N westos ##新建一个链
iptables -E westos XYF ## 更改链的名字
iptables -X XYF  ##删除链

2、使用iptables建立策略改变连接主机之间的源地址转换,以及目的地转换:

iptables -t nat -A POSTROUTING -o ens3 -j SNAT --to-source 172.25.254.137
检测:单网卡1网段可以连接双网卡的两个ip
iptables -t nat -A PREROUTING -i ens3 -j DNAT --to-dest 1.1.1.237
检测:其他机器连接双网卡的172网段,查看ip时1网段的237。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值