centos iptables_杭州云计算培训课程之iptables名词及相关操作介绍

1. 什么是 iptables

举个例子,如果把Netfilter看成是某个小区的一栋楼。那么表(tables)就是楼里的其中的一套房子。这套房子"表(tables)"属于这栋楼“Netfilter/iptables”。

87611e32d832623e7ef2a5ec948e1e87.png

杭州云计算培训课程之iptables名词及相关操作介绍

2. 什么是表(tables)

表(tables)是链的容器,即所有的链(chains)都属于其对应的表(tables).如上,如果把Netfilter看成是某个小区的一栋楼.那么表(tables)就是楼里的其中的一套房子。

3 什么是链(chains)

链(chains)是规则(Policys)的容器。如果把表(tables)当作有一套房子,那么链(chains)就可以说是房子里的家具(柜子等)。

4 什么是规则(Policy)

规则(Policy)就比较容易理解了,就是iptables系列过滤信息的规范和具体方法条款了.可以理解为柜子如何增加并摆放柜子东西等。

基本术语如下表格所示:

Netfilter/iptables 表(tables**)** 链(chains**)** 规则(Policy**)**

一栋楼 楼里的房子 房子里的柜子 柜子里衣服,摆放规则

三、iptables表和链

默认情况下,iptables根据功能和表的定义划分包含三个表,filter,nat,mangle,其每个表又包含不同的操作链(chains )。 实际iptables包含4张表和五个链,主要记住filter即可。

1、四个表:

必须是小写

raw ------------追踪数据包, ----此表用处较少,可以忽略不计

mangle -------- 给数据包打标记,做标记

nat ---------网络地址转换即来源与目的的IP地址和port的转换。

filter --------做过滤的,防火墙里面用的最多的表。

#表的应用顺序:raw-》mangle-》nat-》filter

2、五个链

五链:(必须是大写)链里面写的是规则。

PREROUTING ---------------------进路由之前数据包

INPUT -----------------就是过滤进来的数据包(输入)

FORWARD -----------------转发

OUTPUT ---------------发出去的数据包

POSTROUTING --------------路由之后数据包

#所有的访问都是按顺序:

入站:比如访问自身的web服务流量。先PREROUTING(是否改地址),再INPUT(是否允许)到达程序。

转发:经过linux网关的流量.先PREROUTING(是否改地址),然后路由。转发给FORWARD(转发或者丢弃),最后经过POSTROUTING(看看改不改地址。)

出站:源自linux自身的流量.先OUTPUT,然后路由。再给POSTROUTING(是否改IP)。

#规则顺序:逐条匹配,匹配即停止。

3、四表五链

raw表里面:

PREROUTING

OUTPUT

总结:数据包跟踪 内核模块iptables_raw

mangel表里面有5个链:

PREROUTING

INPUT

FORWARD

OUTPUT

POSTROUTING

路由标记用的表。内核模块iptables_mangle

nat表里面的链:

PREROUTING

INPUT

OUTPUT

POSTROUTING

转换地址的表(改IP,改端口。当网关使用的linux。保护内外网流量。内核模块叫iptable_nat)

filter表有三个链:重点

INPUT #负责过滤所有目标是本机地址的数据包通俗来说:就是过滤进入主机的数据包

FORWARD #负责转发经过主机的数据包。起到转发的作用

OUTPUT #处理所有源地址是本机地址的数据包通俗的讲:就是处理从主机发出的数据包

总结:根据规则来处理数据包,如转或者丢。就是实现主机型防火墙的主要表。

内核模块 iptable_filter

四 iptables操作

1、安装

centos(5/6)

启动防火墙:#/etc/init.d/iptables start

centos7

启动防火墙 -----192.168.246.200服务器实验。

# yum install -y iptables iptables-services

# systemctl stop firewalld

# systemctl disable firewalld

# systemctl start iptables

查看版本:

[root@iptables-server ~]# iptables -V

iptables v1.4.21

配置文件:

/etc/sysconfig/iptables-config

/etc/sysconfig/iptables #记录规则文件

2、参数解释

-L:列出一个链或所有链中的规则信息

-n:以数字形式显示地址、端口等信息

-v:以更详细的方式显示规则信息

--line-numbers:查看规则时,显示规则的序号(方便之处,通过需要删除规则-D INPUT 1

-F:清空所有的规则(-X是清理自定义的链,用的少;-Z清零规则序号)

-D:删除链内指定序号(或内容)的一条规则

-P:为指定的链设置默认规则

-A:在链的末尾追加一条规则

-I:在链的开头(或指定序号)插入一条规则

-t: 指定表名

.... 更多参数可通过--help查看

3、参数使用

1.如果不写-t 默认使用filter表

指定表名查看规则

[root@iptables-server ~]# iptables -t nat -L

默认查看规则:

# iptables -L

以数字的形式显示ip和端口与协议

# iptables -nL

显示规则行号

# iptables -nL --line

清空规则:

#iptables -F

清空单独的某一个链里面的规则

#iptables -F 链名

清空单独的某一个表里的,某一个链里面的规则

# iptables -t nat -F INPUT

保存规则:

# service iptables save

# iptables-save > /etc/sysconfig/iptables

4、iptables语法

iptables -t 表名 动作 [链名] [-p 匹配条件] [-j 控制类型]

-j:控制类型, 通过前面匹配到之后是丢弃还是保留数据包的处理方式:

ACCEPT允许,

REJECT拒绝,

DROP丢弃。 不会给用户返回任何的拒绝消息,不推荐使用。

LOG写日志(log不适用匹配,只是记录一下)

动作:添规则还是删除规则

-p:匹配条件:数据包特征ip,端口等

如果不写-t 默认使用filter表

动作

修改默认规则: -P (大p)

删除规则:-D

修改规则:-R

追加规则: -A 默认追加到链的末尾

插入规则:-I (大i),在链的开头(或指定序号)插入一条规则

5、查看添加删除规则

观察iptable规则添加的方法,删除和查询的方法。

iptables -t filter -A INPUT -p tcp -j ACCEPT #最后一行

iptables -I INPUT -p udp -j ACCEPT #第一行

iptables -I INPUT 4 -p icmp -j ACCEPT #(插入到第4行)#第4行

iptables -L #看一看

iptables -D INPUT 3 # 删除第三行

iptables -F #全清空

service iptables save #保存

systemctl restart iptables #重启

注意:如果不保存重启之后规则就不在了。

2、规则匹配条件

1、通用匹配(协议),可以独立使用

协议:-p (小p)

tcp ---用的最多

udp

icmp ---ping的时候用的协议

#使用协议的时候可以不指定端口,使用端口的时候必须指定协议。

案例:

禁止自己被ping,在filter表的INPUT链插入一个丢弃icmp的规则。

# iptables -F

# iptables -A INPUT -p icmp -j REJECT ----拒绝

验证:

[root@iptables-test ~]# ping 192.168.246.200

PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data.

From 192.168.246.200 icmp_seq=1 Destination Port Unreachable

2、通过端口规则匹配:

端口:

--sport ---源端口

--dport --目标端口

案例:

拒绝192.168.246.201这台机器通过ssh连接到这台服务器

# iptables -I INPUT -s 192.168.246.201 -p tcp --dport 22 -j REJECT

例子:端口的范围: 拒绝192.168.246.201这台机器通过22端口到80端口的访问,包括22和80端口在内

# iptables -I INPUT -s 192.168.246.201 -p tcp --dport 22:80 -j REJECT

验证:

# curl -I http://192.168.246.200

curl: (7) Failed connect to 192.168.246.200:80; Connection refused

# ssh root@192.168.246.200

ssh: connect to host 192.168.246.200 port 22: Connection refused

拒绝所有机器通过ssh连接到这台服务器

# iptables -I INPUT -p tcp --dport 22 -j REJECT

例子:端口的范围: 拒绝所有机器通过22端口到80端口的访问,包括22和80端口在内

# iptables -I INPUT -p tcp --dport 22:80 -j REJECT

3、通过ip地址

1.#禁止源246.201主机进来。(换个主机ping一下,就可以通信)

[root@iptables-server ~]# iptables -I INPUT -s 192.168.246.201 -p icmp -j REJECT

-s: 源ip地址

在源ip机器验证:

[root@iptables-test ~]# ping 192.168.246.200

PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data.

From 192.168.246.200 icmp_seq=1 Destination Port Unreachable

2.拒绝多个ip地址:后面跟ip地址可以更多个ip地址用逗号隔开

# iptables -t filter -I INPUT -s 192.168.246.201,192.168.246.133 -p icmp -j REJECT

# iptables -t filter -I INPUT -s 192.168.246.201,192.168.246.133 -p tcp --dport 22:80 -j REJECT

验证:在源ip地址通过curl访问。在246.133和246.201机器分别验证

# curl -I http://192.168.246.200

curl: (7) Failed connect to 192.168.246.200:80; Connection refused

# ssh root@192.168.246.200

ssh: connect to host 192.168.246.200 port 22: Connection refused

3.举例::#限制源10网段的数据包。

# iptables -I INPUT -s 192.168.10.0/24 -j REJECT

4、修改规则:

# iptables -L

target prot opt source destination

REJECT tcp -- 192.168.246.133 anywhere tcp dpts:ssh:http reject-wi

REJECT tcp -- 192.168.246.201 anywhere tcp dpts:ssh:http reject-wi

REJECT icmp -- 192.168.246.201 anywhere reject-with icmp-port-unreachable

将修改第二条规则访问80端口:

# iptables -R INPUT 2 -p tcp --dport 80 -s 192.168.246.201 -j ACCEPT

# iptables -L

Chain INPUT (policy ACCEPT)

target prot opt source destination

REJECT tcp -- 192.168.246.133 anywhere tcp dpts:ssh:http reject-with icmp-port-unreachable

ACCEPT tcp -- 192.168.246.201 anywhere tcp dpt:http

REJECT icmp -- 192.168.246.201 anywhere reject-with icmp-port-unreachable

验证在修改为允许访问的源ip机器上:

# curl -I http://192.168.246.200

HTTP/1.1 200 OK

# iptables -R INPUT 1 -p tcp -s 192.168.62.185 --dport 22 -j ACCEPT

验证在修改为允许访问的源ip机器上:

# ssh 192.168.62.135

The authenticity of host '192.168.62.135 (192.168.62.135)' can't be established.

ECDSA key fingerprint is SHA256:cUexa/Lv/EtkmiiTrsHUJ1zOWsjT9cihPqLxi23w5ws.

ECDSA key fingerprint is MD5:8c:9c:65:99:b7:6e:df:93:86:c1:7f:38:d9:73:4c:3d.

Are you sure you want to continue connecting (yes/no)?

5、icmp类型匹配

禁止ping策略原则

iptables服务器是ping命令发起者或是接受者

-i --in-interface:在INPUT链配置规则中,指定从哪一个网卡接口进入的流量(只能配置在INPUT链上)

-o --out-interface:在OUTPUT链配置规则中,指定从哪一个网接口出去的流量(只能配置在OUTPUT链上)

icmp的类型:

0: Echo Reply——回显应答(Ping应答)ping的结果返回。

8: Echo request——回显请求(Ping请求),发出去的请求。

iptables服务器-----发起者:ping 别的机器

1.自己不能ping别人,但是别人可以ping自己:

[root@iptables-server ~]# iptables -I OUTPUT -o ens33 -p icmp --icmp-type 8 -j REJECT #ping发出的请求禁止掉了

验证:

[root@iptables-server ~]# ping 192.168.246.133 #将ping请求给禁止掉了。

PING 192.168.246.133 (192.168.246.133) 56(84) bytes of data.

ping: sendmsg: Operation not permitted

[root@jenkins-server ~]# ping 192.168.246.200 #可以ping通

PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data.

64 bytes from 192.168.246.200: icmp_seq=1 ttl=64 time=0.280 ms

iptables服务器作为接受者。也就是别人ping自己:

本机可以ping其他机器。其他机器不能ping通本机:

[root@iptables-server ~]# iptables -I OUTPUT -o ens33 -p icmp --icmp-type 8 -j ACCEPT #允许自己ping别人

[root@iptables-server ~]# iptables -A INPUT -i ens33 -p icmp --icmp-type 8 -j DROP #将进来的ping请求给丢弃了。

换一种方法:

[root@iptables-server ~]# iptables -I OUTPUT -o ens33 -p icmp --icmp-type 0 -j REJECT #不给回应icmp包

验证:

[root@iptables-server ~]# ping 192.168.246.201 #ping其他机器通

PING 192.168.246.201 (192.168.246.201) 56(84) bytes of data.

64 bytes from 192.168.246.201: icmp_seq=1 ttl=64 time=0.491 ms

[root@iptables-test ~]# ping 192.168.246.200 #其他机器ping不同

PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data.

拒绝任何ping的协议:

[root@iptables-server ~]# iptables -A INPUT -p icmp -j DROP

3、扩展匹配

显示匹配:如端口匹配,IP范围,MAC地址,等特殊匹配

#iptables -m iprange --help

1.指定ip范围:

语法: -m iprange --src-range

# iptables -I INPUT -p tcp --dport 80 -m iprange --src-range 192.168.246.199-192.168.246.206 -j REJECT

2.指定多端口范围:一次拒绝多个指定端口

语法:

-m multiport --sports #源端口

-m multiport --dports #目的端口

# iptables -A INPUT -p tcp -m multiport --dports 22,80 -s 192.168.246.133 -j REJECT

验证:在246.133机器上

# ssh root@192.168.246.200 #不通

ssh: connect to host 192.168.246.200 port 22: Connection refused

3.MAC地址匹配

拒绝MAC地址的匹配:只能匹配源MAC地址

语法: -m mac --mac-source

# iptables -I INPUT -p icmp -m mac --mac-source 0:0c:29:cd:26:77 -j REJECT #拒绝指定的MAC地址服务通过icmp协议请求到本地

# iptables -I INPUT -m mac --mac-source 00:0C:29:64:E3:8D -j REJECT #将指定的MAC地址服务请求全部禁止了

通过网卡接口:

# iptables -I INPUT -i ens33 -j DROP #谁也连不上了.

保存和删除规则

删除:

# iptables -D INPUT 3 #通过查看行号,指定行号删除;

# iptables -D INPUT -p icmp -j REJECT #方式二

保存:

[root@iptables-server ~]# iptables-save > /etc/sysconfig/iptables #保存到文件里面,方式一

[root@iptables-server ~]# service iptables save #第二种方式,推荐

iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值