1.IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。
2防火墙在做数据包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的数据包过滤表中,而这些表集成在 Linux 内核中。在数据包过滤表中,规则被分组放在我们所谓的链(chain)中。而netfilter/iptables IP 数据包过滤系统是一款功能强大的工具,可用于添加、编辑和移除规则。
3.虽然 netfilter/iptables IP 信息包过滤系统被称为单个实体,但它实际上由两个组件netfilter 和 iptables 组成。
4.iptables 组件是一种工具,也称为用户空间(userspace),它使插入、修改和除去信息包过滤表中的规则变得容易。
5.netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集。
iptables [-p table] 链管理 chain
-t table : filter,nat,mangle,raw (默认为 filter)
链管理:
-F 清空规则链
-N 创建新的自定义规则链
-X drop 删除用户自定义的规则链
-P Policy 为指定链设置默认策略;iptables -t filter -P FORWARD DROP
-E 重命令自定义链
规则管理:
-A 蒋新规则添加到指定的链上
-I 将新规则插入到指定的位置
-D 删除链上的指定规则
-R 替代指定链上的规则
查看:
-L列表,列出指定链上的指定的规则
基本匹配:
-s 原地址IP
-d 目的IP
-p 协议{tcp
-i 数据报文的流入接口
-o 数据报文的流出接口
扩展匹配:-m match_name
–dport PORT: 目标端口,可以是单个端口
–sport PORT: 源端口
-p udp|tcp|icmp
–icmp-type
0 : echo-reply
8 : echo-request
目标:
-j TARGET:jump至指定的TARGET
ACCEPT 接受
DROP 丢弃
REJECT 拒绝访问
RETURN 返回调用链
iptables 企业7之前自动开,企业7之后的,需要先关闭默认使用的firewalld。再安装iptabels
实验背景:
双网卡主机 server172.25.254.110 (eth0) 和server 1.1.1.110 (eth1)
单网卡主机 desktop 1.1.1.210
实验步骤:
双网卡主机中:
一、iptables的开启
1.yum list iptables-services
没有安装时
yum install iptables-services
2.systemctl stop firewalld
systemctl disable firewalld
systemctl start iptables.service
二、规则的查看
iptables -nL == iptables -t filter -nL #列出指定链上的指定的规则,不加参数时默认查看filter表
三、保存清空策略的两种方式
iptables -F 清空规则链
iptables -nL 查看时规则不存在
systemctl rsetart iptables后,规则又出现
iptables -F 清空规则链
iptables-save > /etc/sysconfig/iptables
或者 service iptables save
将清空的策略保存在文件中 此时硬盘中的策略
四.fileter表
1.INPUT链规则的更改、删除、添加、插入
iptables -P(Policy 为指定链设置默认策略) INPUT DROP
测试 :
真机ssh 172.25.254.110 一直尝试连接,因为110会把外来的数据包不停的丢弃
iptables -P INPUT ACCEPT
iptables -nL
策略默认只能添加 ACCEPT和DROP,REJECT设置如下
iptables -t filter -A(蒋新规则添加到指定的链上) INPUT -p(协议) tcp --dport(目标端口,可以是单个端口) 22 -j REJECT(jump至指定的TARGET) #设置主机禁止被ssh连接
测试:
真机ssh 172.25.254.110 直接显示拒绝
1.accept的设置
2.
3.reject的设置
删除和插入
iptables -A INPUT -s(原地址IP) 172.25.254.2 -p tcp --dport 22 -j ACCEPT #让2可以ssh 连接
测试:
真机 ssh 172.25.254.2
显示连接失败
因为从上到下验证策略,当符合上面的要求时,不再看下面的策略。
解决方法:
iptables -D INPUT 2
iptables -nL
iptables -I INPUT 1 -s 172.25.254.2 -p tcp --dport 22 -j ACCEPT
iptables -I(将新规则插入到指定的位置) INPUT 1 -i(数据报文的流入接口) eth1 -j ACCEPT #设置eth1网段的ip可以ssh
iptables -nL
测试:
单网卡主机
ssh 1.1.1.110
iptables -D INPUT 3
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #设置开放httpd服务
iptables -nL
iptables -R (替代指定链上的规则) INPUT 2 -s 172.25.254.3 -p tcp --dport 22 -j ACCEPT 更改第二个策略为允许172.5.254.3ssh
2.链的增加 更改 删除
iptables -N(创建新的自定义规则链) westos #添加新链westos
iptables -E(重命令自定义链) westos WESTOS 更改为WESTOS
iptables -X(drop 删除用户自定义的规则链) WESTOS 删除链
更改完规则后保存,否则restart iptables,所有的规则会不见
service iptables save
NAT链
1.内网访问外网时的地址转换 (内核路由之后 )让1.1.1.210可以去访问172.25.254.110
在单网卡主机1.1.1.210上
添加网关
vim /etc/sysconfig/network-scripts/ifcfg-eth0
添加
GATEWAY=1.1.1.110
systemctl restart network
iptables -t nat -nL #查看nat表
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.110
iptables -t nat -nL #查看nat表
sysctl -a | grep ip_forward 开启内核路由
vim /etc/sysctl.conf
添加
net.ipv4.ip_forward=1
sysctl -p
测试:
在单网卡主机 ping 172.25.254.2(真机)
2.外网访问内网(内核路由之前 )
172.25.254.2访问172.25.254.110 做操作让它其实访问1.1.1.210
在双网卡主机
iptables -t nat -A PREOUTING -i eth0 -j DNAT --to-dest 1.1.1.210
iptables -t nat -nL
ssh root@172.25.254.110
ifconfig 结果为1.1.1.210