iptables归纳总结

先简单介绍下iptables

IPTABLES的几点概念

 1、容器:包含或者说属于的关系

 2、Netfilter/iptables是表的容器,iptables包含的各个表

filter,NAT,MANGLE,RAW)

 3、iptables的表tables又是链的容器

chains:INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING

 4、链chains是规则容器:

 5、规则Policy:一条条过滤的语句。


示例:

(1)禁止掉当前SSH端口,这里是52113。

[root@ipt ~]# iptables -t filter -A INPUT -p tcp –dport 52113 -j DROP

语法:

Usage:

iptables -t [table] -[AD] chain rule-specification [options]

具体命令:

iptables           -A INPUT -p tcp –dport 52113 -j DROP

iptables -t filter -A INPUT -p tcp –dport 52113 -j DROP

注:

1.iptables默认用的就是filter表,因此,以上两条命令等价。

2. 其中的INPUT DROP要大写。

3. –jump        -j target

   target for rule (may load target extension)

基本的处理行为:ACCEPT(接受)、DROP(丢弃)、REJECT(拒绝)

比较:DROP好于REJECT

(2)禁ping

iptables -t filter  -I INPUT -p icmp –icmp-type 8 -i eth0 -s 10.0.0.0/24 -j DROP 

iptables -t filter  -I INPUT -p icmp –icmp-type 8  -j DROP   

    本机地址:172.16.14.1,允许172.16.0.0/16网络ping本机,但限制每分钟请求不能超过20,每次并发不能超过6个

iptables -A INPUT -s 172.16.0.0/16 -d 172.16.14.1 -p icmp –icmp-type 8 -m limit –limit 20/min –limit-burst 6 -j ACCEPT

iptables -A OUTPUT -s 172.16.14.1 -d 172.16.0.0/16 -p icmp –icmp-type 0 -j ACCEPT

(3)!使用简介

封掉3306端口

iptables -A INPUT -p tcp  –dport 3306 -j DROP

匹配指定协议外的所有协议

iptables -A INPUT -p ! tcp

匹配主机源IP

iptables -A INPUT -s 10.0.0.14

iptables -A INPUT -s ! 10.0.0.14

 匹配网段

iptables -A INPUT -s 10.0.0.0/24

iptables -A INPUT -s ! 10.0.0.0/24

匹配单一端口

iptables -A INPUT -p tcp –sport 53

iptables -A INPUT -p udp –dport 53

 匹配指定端口之外的端口

iptables -A INPUT -p tcp –dport ! 22

iptables -I INPUT -p tcp ! –dport 22  -s 10.0.0.123 -j DROP

匹配单一端口

iptables -A INPUT -p tcp –sport 53

iptables -A INPUT -p udp –dport 53

 匹配指定端口之外的端口

iptables -A INPUT -p tcp –dport ! 22

iptables -I INPUT -p tcp ! –dport 22  -s 10.0.0.123 -j DROP

iptables -I INPUT -p tcp –dport 52000:53000 -j DROP

匹配端口范围:

iptables -A INPUT -p tcp –sport 22:80

iptables -I INPUT -p tcp –dport 21,22,23,24 -j ACCEPT===》错误语法

iptables -I INPUT -p tcp -m multiport –dport 21,22,23,24 -j ACCEPT

iptables -I INPUT -p tcp –dport 3306:8809 -j ACCEPT

iptables -I INPUT -p tcp –dport 18:80 -j DROP

匹配ICMP类型

iptables -A INPUT -p icmp –icmp-type 8

例:iptables -A INPUT -p icmp –icmp-type 8 -j DROP

iptables -A INPUT -p icmp -m icmp –icmp-type any -j ACCEPT

匹配指定的网络接口

iptables -A INPUT -i eth0

iptables -A FORWARD -o eth0

匹配网络状态

-m state –state

    NEW:已经或将启动新的连接

    ESTABLISHED:已建立的连接

    RELATED:正在启动新连接

INVALID:非法或无法识别的

FTP服务是特殊的,需要配状态连接。

企业iptables面试题:自定义链处理syn攻击

iptables -N syn-flood

iptables -A INPUT -i eth0 -syn -j syn-flood

iptables -A syn-flood -m limit -limit 5000/s -limit-burst 200 -j RETURN

iptables -A syn-flood -j DROP

 

 将设置保存为文件(永久生效)

iptables-save >/etc/sysconfig/iptables

局域网共享的两条命令方法:

方法1:适合于有固定外网地址的:

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT –to-source 10.0.0.7

(1)-s 192.168.1.0/24 办公室或IDC内网网段。

(2)-o eth0 为网关的外网卡接口。

(3)-j SNAT –to-source 10.0.0.19 是网关外网卡IP地址。

方法2:适合变化外网地址(ADSL):

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE ç伪装。

原创文章,作者:cnc,如若转载,请注明出处:http://www.178linux.com/74595

(0)
cnccnc
上一篇 2017-05-02 11:08
下一篇 2017-05-02 14:13

相关推荐

  • 在VMware workstations中安装CentOS-7-x86_64-Everything-1611.ISO

      对于最近在Linux学习的过程中,许多同学对CentOS系统的详细配置还不熟悉,于是我编写此详细步骤供大家参考。 工具: VMware workstations; CentOS-7-x86_64-Everything-1611.iso; 准备工作:  1、先要下载一个系统镜像centOS-7-x86_64-Everythin…

    2017-07-11
  • 误删除libc.so.6时,该怎么恢复

           一周一周光阴似流水,转瞬即逝,又到了该说点什么的时候了。这周老师给我们出了一道实验题,内容是当你不小心误删除了libc.so.6这个库文件时,怎么恢复。意不意外?惊不惊喜?那下面就来简单说说(呃……为什么不是详细说说呢,因为这个这个水平有限,也只能是理解多少,给…

    2017-08-11
  • 你会用Python写洗脑神曲吗?

    Python实战班-学员学习成果展示 同样是周末,有些人是闲聊着度过,有些人是学习充电度过。 人与人最大的区别,是下班后的时间。看你怎么去利用。 周末时,马哥Python实战班的学员正在认真上课,他们中的不少人,月薪在10k以上,甚至月薪20k以上。 但他们没有虚度周末时光。 #最浪费时间的就是:思而不学+犹豫不决。# 马哥Python实战班二期的小伙伴们才…

    Linux干货 2016-07-05
  • nginx初步

    一、知识整理 1、查看进程使用的cpu;ni查看nice值 [root@localhost html]# ps axo pid,comm,psr,ni  43769 nginx          &n…

    Linux干货 2016-10-31
  • 马哥教育网络班22期第六周课程练习1

    1、1,$s/^\s\+/#/g 2、1,$s/^\s\+$//g 3、1,$s/^#\s//g 4、1,3s/^/#/g 5、sed -e s/gpgcheck=1/gpgcheck=0/g -e s/enabled=0/enabled=1/g /etc/yum.repos.d/CentOS-Media.repo  6、0 /4 * * * cp…

    Linux干货 2016-09-26
  • shll函数作业

    扫描/etc/passwd文件每一行,如发现GECOS字段为空,则填充用户名和单位电话为62985600,并提示该用户的GECOS信息修改成功。 [root@localhost sh.log]# cat whileGECOS.sh  #!/bin/bash #author:DYW #扫描/etc/passwd文件每一行…

    Linux干货 2016-08-24