10.12-10.18

netfilter   contos 5、6   使用

centos7 时改名firewalld

 

  •  

filter:
                  This  is the default table (if no -t option is passed). It contains the built-in chains INPUT (for packets destined to local                sockets),
                  FORWARD (for packets being routed through the box), and OUTPUT (for locally-generated packets).

过滤器:

这是默认表(如果没有-T选项被传递)。它包含内置的链输入(用于指定到本地套接字的分组),

转发(对于通过盒子路由的数据包)和输出(对于本地生成的数据包)。

  nat:
                  This table is consulted when a packet that creates a new connection is encountered.  It consists of three built-ins:  PREROUTING  (for
                  altering packets as soon as they come in), OUTPUT (for altering locally-generated packets before routing), and POSTROUTING (for alter?
                  ing packets as they are about to go out).  IPv6 NAT support is available since kernel 3.7.
 

NAT:

当遇到创建新连接的包时,查阅此表。它由三个内置:预处理(用于

一旦数据包进入就对其进行更改、OUTPUT(用于在路由之前更改本地生成的数据包)和POSTROUTING(用于更改?

当他们即将外出的时候。自从内核3.7以来,IPv6 NAT支持是可用的。

 mangle:
                  This table is used for specialized packet alteration.  Until kernel 2.4.17 it had two built-in chains: PREROUTING (for altering incom?
                  ing  packets  before  routing)  and  OUTPUT (for altering locally-generated packets before routing).  Since kernel 2.4.18, three other
                  built-in chains are also supported: INPUT (for packets coming into the box itself), FORWARD (for altering packets being routed through
                  the box), and POSTROUTING (for altering packets as they are about to go out).

轧辊:

此表用于专门的数据包更改。直到内核2.4.17它有两个内置的链:预处理(改变iNCOM)?

路由之前的数据包和输出(用于在路由之前更改本地生成的数据包)。由于内核2.4.18,三其他

还支持内置链:INPUT(用于进入盒子本身的数据包)、FORWARD(用于更改通过路由的数据包

盒子),并将数据发送出去(以改变数据包的长度)。

raw:
                  This table is used mainly for configuring exemptions from connection tracking in combination with the NOTRACK target.  It registers at
                  the netfilter hooks with higher priority and is thus called before ip_conntrack, or any other IP tables.  It  provides  the  following
                  built-in chains: PREROUTING (for packets arriving via any network interface) OUTPUT (for packets generated by local processes)
 

原始的:

此表主要用于与NoTror目标结合来配置连接跟踪的豁免。它登记在

具有较高优先级的NETFLASH挂钩,因此在IPH-CONTRONT或任何其他IP表之前调用。它提供以下内容

内置链:预处理(对于通过任何网络接口到达的数据包)输出(用于本地进程生成的数据包)

  security:
                  This  table is used for Mandatory Access Control (MAC) networking rules, such as those enabled by the SECMARK and CONNSECMARK targets.
                  Mandatory Access Control is implemented by Linux Security Modules such as SELinux.  The security table is called after the filter  ta?
                  ble,  allowing  any Discretionary Access Control (DAC) rules in the filter table to take effect before MAC rules.  This table provides
                  the following built-in chains: INPUT (for packets coming into the box itself), OUTPUT (for altering locally-generated  packets  before
                  routing), and FORWARD (for altering packets being routed through the box).

安全性:

这个表用于强制访问控制(MAC)网络规则,例如SECMARK和CONNSECMARK目标启用的那些规则。

强制访问控制是由Linux安全模块(如SELinux)实现的。安全表在过滤器TA之后调用?

BLE,允许过滤器表中的任意自主访问控制(DAC)规则在MAC规则之前生效。本表提供

以下内置链:INPUT(对于进入盒子本身的包),OUTPUT(用于在以前更改本地生成的包)

路由),并转发(用于更改通过盒子路由的数据包)。

iptables -t tablename option

 tablename可选为:filter、nat、mangle、raw
 option:
   -L   列出table内容
   -F   清除table内容
   -A   加入新的规则
   -P   设定Default Policy
   -I   插入新的规则
   -R   取代规则
   -D   删除规则

iptables的语法

1.定义默认策略
当数据包不符合链中任一条规则时,iptables将根据该链预先定义的默认策略来处理数据包,默认策略的定义格式如下。
iptables  [-t表名] <-P> <链名> <动作> ?参数说明如下。
[-t表名]:指默认策略将应用于哪个表,可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认使用filter表。
<-P>:定义默认策略。
<链名>:指默认策略将应用于哪个链,可以使用INPUT、OUTPUT、FORWARD、PREROUTING、OUTPUT和POSTROUTING。
<动作>:处理数据包的动作,可以使用ACCEPT(接受数据包)和DROP(丢弃数据包)。

2.查看iptables规则
查看iptables规则的命令格式为:
iptables  [-t表名] <-L> [链名]
参数说明如下。
[-t表名]:指查看哪个表的规则列表,表名用可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认查看filter表的规则列表。
<-L>:查看指定表和指定链的规则列表。
[链名]:指查看指定表中哪个链的规则列表,可以使用INPUT、OUTPUT、FORWARD、PREROUTING、OUTPUT和POSTROUTING,如果不指明哪个链,则将查看某个表中所有链的规则列表。

3.增加、插入、删除和替换规则
相关规则定义的格式为:
iptables  [-t表名]  <-A | I | D | R> 链名 [规则编号] [-i | o 网卡名称] [-p 协议类型] [-s 源IP地址 | 源子网] [--sport 源端口号] [-d目标IP地址 | 目标子网] [--dport目标端口号] <-j动作>
参数说明如下。
[-t表名]:定义默认策略将应用于哪个表,可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认使用filter表。
-A:新增加一条规则,该规则将会增加到规则列表的最后一行,该参数不能使用规则编号。
-I:插入一条规则,原本该位置上的规则将会往后顺序移动,如果没有指定规则编号,则在第一条规则前插入。
-D:从规则列表中删除一条规则,可以输入完整规则,或直接指定规则编号加以删除。
-R:替换某条规则,规则被替换并不会改变顺序,必须要指定替换的规则编号。
<链名>:指定查看指定表中哪个链的规则列表,可以使用INPUT、OUTPUT、FORWARD、PREROUTING、OUTPUT和POSTROUTING。
[规则编号]:规则编号用于插入、删除和替换规则时用,编号是按照规则列表的顺序排列,规则列表中第一条规则的编号为1。
[-i | o 网卡名称]:i是指定数据包从哪块网卡进入,o是指定数据包从哪块网卡输出。网卡名称可以使用ppp0、eth0和eth1等。
[-p 协议类型]:可以指定规则应用的协议,包含TCP、UDP和ICMP等。
[-s 源IP地址 | 源子网]:源主机的IP地址或子网地址。
[--sport 源端口号]:数据包的IP的源端口号。
[-d目标IP地址 | 目标子网]:目标主机的IP地址或子网地址。
[--dport目标端口号]:数据包的IP的目标端口号。
<-j动作>:处理数据包的动作,各个动作的详细说明可以参考前面的说明。

4.清除规则和计数器
在新建规则时,往往需要清除原有的、旧的规则,以免它们影 ?响新设定的规则。如果规则比较多,一条条删除就会十分麻烦, ?这时可以使用iptables提供的清除规则参数达到快速删除所有的规 ?则的目的。
定义参数的格式为:
iptables  [-t表名] <-F | Z>
参数说明如下。
[-t表名]:指定默认策略将应用于哪个表,可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认使用filter表。
-F:删除指定表中所有规则。
-Z:将指定表中的数据包计数器和流量计数器归零

重启防火墙(iptables)命令#service iptable restart失效

 

#service iptable restart

Redirecting to /bin/systemctl restart  iptable.service

Failed to issue method call: Unit iptable.service failed to load: No such file or directory.

据说从某个版本的Linux系统(CenterOS/RedHat)后就将service命令改了,如下

#systemctl restart iptables.service

#iptables -L

重启防火墙成功~~~~

显示规则序列号

增加一条规则

删除规则

使用规则序列号  删除规则

案例

iptables实例

禁止客户机访问不健康网站
【例1】添加iptables规则禁止用户访问域名为www.sexy.com的网站。
iptables -I FORWARD -d www.sexy.com -j DROP
【例2】添加iptables规则禁止用户访问IP地址为20.20.20.20的网站。
iptables -I FORWARD -d 20.20.20.20 -j DROP

禁止某些客户机上网
【例1】添加iptables规则禁止IP地址为192.168.1.X的客户机上网。
iptables -I FORWARD -s 192.168.1.X -j DROP
【例2】添加iptables规则禁止192.168.1.0子网里所有的客户机上网。
iptables -I FORWARD -s 192.168.1.0/24 -j DROP

禁止客户机访问某些服务
【例1】禁止192.168.1.0子网里所有的客户机使用FTP协议下载。
iptables -I FORWARD -s 192.168.1.0/24 -p tcp –dport 21 -j DROP
【例2】禁止192.168.1.0子网里所有的客户机使用Telnet协议连接远程计算机。
iptables -I FORWARD -s 192.168.1.0/24 -p tcp –dport 23 -j DROP

强制访问指定的站点
【例】强制所有的客户机访问192.168.1.x这台Web服务器。
iptables -t nat -I PREROUTING -i eth0 -p tcp –dport 80 -j DNAT –to-destination 192.168.1.x:80

禁止使用ICMP协议
【例】禁止Internet上的计算机通过ICMP协议ping到NAT服务器的ppp0接口,但允许内网的客户机通过ICMP协议ping的计算机。
iptables -I INPUT -i ppp0 -p icmp -j DROP

脚本案例

准备

打开路由转发 

猜你喜欢

转载自blog.csdn.net/qq_42720896/article/details/82054610