iptables防火墙的配置管理

iptables概述:

iptables是与内核集成的IP信息包过滤系统。它是一种工具,也称为用户空间(userspace)

1.启用iptables

##1.先关闭firewalld火墙
[root@localhost Desktop]# systemctl stop firewalld
[root@localhost Desktop]# systemctl disable firewalld
##冻结firewalld(注意:mask表示冻结,unmask表示解冻)
[root@localhost Desktop]# systemctl mask firewalld

在这里插入图片描述

##2.安装iptables火墙
[root@localhost Desktop]# yum install -y iptables-services
##3.开启iptables
[root@localhost Desktop]# systemctl start iptables
##4.开机自启
[root@localhost Desktop]# systemctl enable iptables

在这里插入图片描述
2.iptables火墙策略的查看

##1.以域名(主机名)的方式显示;
[root@localhost Desktop]# iptables -L

在这里插入图片描述

##2.以ip的方式显示; -n表示不做解析
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##3.默认查看的是filter表信息
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##4.显示nat表信息
[root@localhost Desktop]# iptables -t nat -nL

在这里插入图片描述

##5.显示mangle表信息
[root@localhost Desktop]# iptables -t mangle -nL

在这里插入图片描述
3.iptables火墙策略的清除

##清除策略
[root@localhost Desktop]# iptables -F
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##但发现重启后被清除的策略又会恢复
[root@localhost Desktop]# systemctl restart iptables.service 
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##这是由于这些策略都保存在该文件中
[root@localhost Desktop]# vim /etc/sysconfig/iptables

在这里插入图片描述

##1.刷新策略
[root@localhost Desktop]# iptables -F
##2.保存策略
[root@localhost Desktop]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

在这里插入图片描述

##此时发现保存在文件中的略策略被清除
[root@localhost Desktop]# cat /etc/sysconfig/iptables

在这里插入图片描述

##此时即使重启iptables,被清理的策略也不会恢复
[root@localhost Desktop]# iptables -nL
[root@localhost Desktop]# systemctl restart iptables.service 
[root@localhost Desktop]# iptables -nL

在这里插入图片描述
4.iptables火墙策略信息的更改

(1)表中链信息的更改

##1.默认策略为ACCEPT
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##此时可以ssh连接
[root@foundation26 ~]# ssh root@172.25.254.126

在这里插入图片描述

##2.将策略更改为DROP(丢弃状态);此时将无法访问本机的所有服务,下边以sshd服务为例
[root@localhost Desktop]# iptables -P INPUT DROP
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##此时便无法ssh连接;会一直卡住
[kiosk@foundation26 ~]$ ssh root@172.25.254.126

在这里插入图片描述

##还原:
[root@localhost Desktop]# iptables -P INPUT ACCEPT
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##3.添加westos链
[root@localhost Desktop]# iptables -N westos
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##4.更改链名称
[root@localhost Desktop]# iptables -E westos WESTOS
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##5.删除链
[root@localhost Desktop]# iptables -X WESTOS
[root@localhost Desktop]# iptables -nL

在这里插入图片描述
(2)iptables对ssh服务的访问控制

@1.仅拒绝26主机ssh连接本机

##默认可以ssh连接
[root@foundation26 ~]# ssh root@172.25.254.126

在这里插入图片描述

##不允许26主机ssh连接
[root@localhost Desktop]# iptables -t filter -A INPUT -s 172.25.254.26 -p tcp --dport 22 -j REJECT

具体参数含义:
-t filter              ##filter表
-A INPUT               ##INPUT链
-s 172.25.254.26       ##ip源
-p tcp                 ##tcp协议
--dport 22             ##22端口
-j REJECT              ##执行动作为拒绝

[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##此时无法连接
[root@foundation26 ~]# ssh root@172.25.254.126
ssh: connect to host 172.25.254.126 port 22: Connection refused

在这里插入图片描述

@2.仅允许26主机ssh连接本机

读取方式: 从上往下依次读取,一旦找到匹配的数据,下边的内容就不再读取,以提高读取速率

##1.刷新策略
[root@localhost Desktop]# iptables -F
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##2.拒绝所有
[root@localhost Desktop]# iptables -A INPUT -j REJECT
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##仅允许26主机;-A表示添加策略到最后
[root@localhost Desktop]# iptables -A INPUT -s 172.25.254.26 -p tcp --dport 22 -j ACCEPT
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##连接失败;因为第一条已经拒绝了所有,故而不会再读取第二条
[kiosk@foundation26 ~]$ ssh root@172.25.254.126
ssh: connect to host 172.25.254.126 port 22: Connection refused

在这里插入图片描述

##3.仅允许26主机;-I 表示插入,可以指定添加策略的位置,默认添加到最前面
[root@localhost Desktop]# iptables -I INPUT -s 172.25.254.26 -p tcp --dport 22 -j ACCEPT
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

测试:

[kiosk@foundation26 ~]$ ssh root@172.25.254.126

在这里插入图片描述

[root@localhost Desktop]# ssh root@172.25.254.126
ssh: connect to host 172.25.254.126 port 22: Connection refused

在这里插入图片描述

(3)表规则的更改

##1.删除filter表中的第三条规则;-D表示删除
[root@localhost Desktop]# iptables -D INPUT 3
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##2.更改第二条规则的内容;-R表示更改
[root@localhost Desktop]# iptables -R INPUT 2 -j ACCEPT
[root@localhost Desktop]# iptables -nL

在这里插入图片描述

##3.清除策略
[root@localhost Desktop]# iptables -F

@仅允许26主机ssh连接本机

##不允许拒绝26主机,即仅允许26主机; ! 表示 非
[root@localhost Desktop]# iptables -A INPUT ! -s 172.25.254.26 -p tcp --dport 22 -j REJECT

在这里插入图片描述
测试:

[root@foundation26 ~]# ssh root@172.25.254.126
root@172.25.254.126's password: 
Last login: Sun Dec  9 14:55:01 2018 from 172.25.254.26

在这里插入图片描述

##清除策略
[root@localhost Desktop]# iptables -F
[root@localhost Desktop]# iptables -nL

5.地址伪装与端口转发

(1)SNAT与DNAT

SNAT(Source Network Address Translation,源地址转换)通常被叫做源映射

DNAT(Destination Network Address Translation,目的地址转换) 通常被叫做目的映射。

(2)应用场景

SNAT: 改变数据包的源地址。当内网数据包到达防火墙后,防火墙会使用外部地址替换掉数据包的源IP地址(目的IP地址不变),使网络内部主机能够与网络外部主机通信。

DNAT: 改变数据包的目的地址。当防火墙收到来自外网的数据包后,会将该数据包的目的IP地址进行替换(源IP地址不变),重新转发到内网的主机。

1.地址伪装(SNAT)

SNAT就是改变转发数据包的源地址

实验环境:

服务端:localhost  
       内网  eth1     1.1.1.126
       外网  eth0     172.25.254.126
       
客户端:client      
       IP             1.1.1.226
       网关           1.1.1.126(服务端的内网)

服务端:

[root@localhost Desktop]# ip addr

在这里插入图片描述
客户端:

[root@client Desktop]# ifconfig eth0

在这里插入图片描述

[root@client Desktop]# route -n

在这里插入图片描述
此时无法与非同网段的主机通信;因为firewalld火墙此时处于关闭状态,而iptables火墙又未添加策略。

[root@client Desktop]# ping 172.25.254.26

在这里插入图片描述
实验:

##将数据包的源地址替换为172.25.254.126
[root@localhost Desktop]# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.126

具体参数含义:
-t      ##指定规则表
-A      ##添加一条规则到某一条规则链中
-s      ##指定数据包的源IP地址或网段
-o      ##指定数据包从哪一块网卡发出去
-j      ##指定一个默认策略来处理数据包(动作)
--to-source    ##指定执行SNAT策略后数据包的源IP

[root@localhost Desktop]# iptables -t nat -nL

在这里插入图片描述
测试:

##发现此时便可以与处在非同网段的主机通信
[root@client Desktop]# ping 172.25.254.26

在这里插入图片描述

[root@client Desktop]# ssh root@172.25.254.26
root@172.25.254.26's password: 
Last login: Wed Dec 12 21:24:43 2018
##发现地址伪装了,1.1.1网段伪装为172.25.254网端
[root@foundation26 ~]# w -i

在这里插入图片描述
2.端口转发(DNAT)

DNAT就是改变转发数据包的目的地址

##注意:22必须是可以ping通的ip
[root@localhost Desktop]# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 22 -j DNAT --to-dest 172.25.254.22

具体参数含义:
-t        ##指定规则表
-A        ##添加一条规则到某一条规则链中
-i        ##指定数据包从哪一块网卡进入
-j        ##指定一个默认策略来处理数据包(动作)
-p        ##指定匹配数据包的通信协议(目前DNAT只支持TCP/UDP/ICMP)
--dport   ##指定协议端口
--to--destination  ##指定执行DNAT策略后数据包的目的IP

[root@localhost Desktop]# iptables -t nat -nL

在这里插入图片描述
测试:(注意:不能在client主机中测试)

[root@foundation26 ~]# ssh root@172.25.254.126
root@172.25.254.126's password: 
Last login: Wed Dec 12 22:55:18 2018 from 172.25.254.126
##发现IP地址由126转发到22
[root@pxe-server ~]# ifconfig eth0

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值