iptables 命令规则

1.iptables命令帮助信息。

[root@Nagios2 ~]# iptables -h   
iptables v1.4.7

Usage: iptables -[AD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]  
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]    ###删除规则
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]   #(其中-p等同于 –policy规则)
       iptables -h (print this help information)

Commands:
Either long or short options are allowed.
提示:这款请注意。长格式短格式命令都可用、为了打字我们习惯于端格式
  --append  -A chain            Append to chain
  --delete  -D chain            Delete matching rule from chain
  --delete  -D chain rulenum
                                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                                Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                                List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                                Print the rules in a chain or all chains
  --flush   -F [chain]          Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                                Zero counters in chain or all chains
  --new     -N chain            Create a new user-defined chain
  --delete-chain
            -X [chain]          Delete a user-defined chain
  --policy  -P chain target
                                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                                Change chain name, (moving any references)
Options:
[!] --proto     -p proto        protocol: by number or name, eg. `tcp'
[!] --source    -s address[/mask][...]
                                source specification
[!] --destination -d address[/mask][...]
                                destination specification
[!] --in-interface -i input name[+]
                                network interface name ([+] for wildcard)
 --jump -j target
                                target for rule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match       -m match
                                extended match (may load extension)
  --numeric     -n              numeric output of addresses and ports
[!] --out-interface -o output name[+]
                                network interface name ([+] for wildcard)
  --table       -t table        table to manipulate (default: `filter')
  --verbose     -v              verbose mode
  --line-numbers                print line numbers when listing
  --exact       -x              expand numbers (display exact values)
[!] --fragment  -f              match second or further fragments only
  --modprobe=<command>          try to insert modules using this command
  --set-counters PKTS BYTES     set the counter during insert/append
[!] --version   -V              print package version.
[root@Nagios2 ~]#

2 实践iptables 命令规则

2.1 启动并查看iptables

[root@Nagios2 ~]# /etc/init.d/iptables start
iptables: Applying firewall rules: [  OK  ]
[root@Nagios2 ~]#

查看iptables 的所有链和规则

Iptables –L –n 或者 iptables –L –n –t filter 或者 iptables –L –n –x –v 
中文说明:
-L :列出一个或所有链的规则
-v:显示详细信息、包括每条规则匹配包数量和匹配字节数
-x:在v的基础上、进制自动单位换算(K,M)
-n: 只显示IP地址和端口号码。不显示域名和服务名称
-t : 接表名、如果不加-t,默认就是 –t filter
[root@Nagios2 ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

默认是filter的,没有加 –t 默认查看的就是如果想要查看NAT的表

[root@Nagios2 ~]# iptables -L -n -t nat   
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@Nagios2 ~]#

2.2 清除默认规则

为了从头学起,我们需要首先清除掉所有的默认规则、具体的命令为

Iptables –F  ###清除所有规则
Iptables –X  ####删除用户自定义的链
Iptables –Z   ####链的计数器清零

提示:默认情况下,我们清除规则是对filter表的操作、如果是nat表、我们需要iptables –t nat –F

实例演示2:
[root@Nagios2 ~]# iptables -F
[root@Nagios2 ~]# iptables –flush

2.3 iptables 规则语法

2.3.1 禁止ssh默认的22端口
[root@Nagios2 ~]# iptables -A INPUT -p tcp --dport 22 -j DROP
[root@Nagios2 ~]# iptables  -t  filter  -A INPUT -p tcp --dport 22 -j DROP
 提示:
1、iptables 默认用的就是filter表 。因此,以上两条命令等价。
2、其中INPUT DROP 等关键词要大写的
3、行为参数
--jump  -j target 
提示: target 的常见的处理方法有ACCEPT(接受),DROP(丢弃),REJECT(拒接)其中、一般不使用REJECT行为、REJECT会带来安全隐患、在这里我们只需要记住ACCEPT(接受)、DROP(丢弃)即可、

扩展:删除设定的规则

iptables –D INPUT –p tcp –dport 22 –j DROP

删除规则法二:

首先显示规则号

[root@Nagios2 ~]# iptables -L -n --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@Nagios2 ~]#
[root@Nagios2 ~]# iptables -D INPUT 1

删除规则三:

执行iptables –F 清除所有的规则,临时生效。重启防火墙或重启计算机失效

删除规则法四:

重启防火墙/etc/init.d/iptables restart

2.3.2 禁止10.0.0.0/24 网段连入
iptables -A INPUT –i eth0 -s 10.0.0.0/24 -j DROP
iptables  -t filter  -A INPUT –i eth0 -s 10.0.0.0/24 -j DROP 

iptables 默认的标记是filter表。因此上两条命令等价。

2.3.3 测试

1.源地址不是192.168.132.201 的禁止连接

iptables –A INPUT –I eth1 –s ! 192.168.132.201 –j DROP 
iptables –A INPUT –I eth1 –s ! 192.168.132.201 –j ACCEPT

[root@Nagios2 ~]# iptables -I INPUT -p icmp --icmp-type 8 -i eth0 -s ! 192.168.132.1 -j DROP
小提示:
1) 这里-i eht0表示数据包的进入接口为eth0、类似的参数还有-o匹配数据流出的网络接口
例如:-o eth1 表示数据包的进入接口为eth1.
记忆方法:
-in-interface –i [!] input name [+]
                    Network interface name ([+] for wildcard)
-out-interface –o [!] ouput name[+]
                     Network interface name ([+] for wildcard)

2.源地址不是192.168.132.0/24 的禁止连接

iptables -A INPUT -s ! 192.168.132.0/24 -j DROP
等价于
Iptables –t filter –I INPUT –i eht0 –s ! 192.168.132.0/24 –j DROP

3.封掉3306端口

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

4.匹配指定协议

Iptables –A INPUT –p tcp

Iptables –A INPUT –p udp

小提示:-p 参数可以匹配协议名或者协议号。

--proto –p [!] porto

The specified protocol can be one of tcp,udp ,icmp.or all

5.匹配协议外的所有协议

Iptables –A INPUT –p ! tcp

6.匹配主机

Iptables –A INPUT –s 192.168.132.10

Iptables –A INPUT –s ! 192.168.132.10

7.匹配网段

8.匹配端口之外的端口

Iptables –A INPUT –p tcp –dport !22  -j DROP

9.匹配端口范围

Iptables –A INPUT –p tcp –sport 22:80    ###源端口的22 和80端口就是来访主机的端口

iptables -A INPUT -p tcp -m multiport --dport 21,22,23,24 -j ACCEPT  ###目的端口。就是本地端口

Iptables –I INPUT –p tcp –dport 3306:8809 –j ACCEPT

10.匹配ICMP端口和ICMP类型

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

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

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

Iptables –A FORWARD –s 192.168.132.0/24 –p icmp –m icmp –icmp-type any –j ACCEPT

11.匹配指定的网络接口

Iptables –A INPUT –i eth0   ###进入端口的数据包

Iptables –A INPUT –o eth1  ###出入的端口数据包

12.安全保护

Syn-flood protection;

[root@Nagios4 ~]# iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT

Furtive port scanner :

[root@Nagios4 ~]# iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT

Ping of death:

[root@Nagios4 ~]# iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

2.3.4 生产环境常用服务的iptables 规则实践

特别提示:以下规则是以默认规则为拒接规则设计

  1. 仅允许内部合法的IP访问服务器
#setting access rules

#one ,ip access rules,allow all the ips of

iptables -A INPUT -s 192.168.132.0/24 -p all -j ACCEPT

iptables -A INPUT -s 120.42.60.51/27 -p all -j ACCEPT

iptables -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT

iptables -A INPUT -s 172.16.20.0/24 -p all -j ACCEPT
  1. 仅允许内部合法的ip段ip访问监控服务nagios
#second.port access rules

#nagios

iptables –A INPUT –s 192.168.132.0/24 –p tcp –dport 5666 –j ACCEPT

iptables –A INPUT –s 200.100.59.0/24 –p tcp –dport 5666 –j ACCEPT
  1. 仅允许内部合法ip段访问mysql数据库和oracle数据库
#db

iptables –A INPUT –s 192.168.10.0/24 –p tcp –dport 3306 –j ACCEPT

iptables –A INPUT –s 192.168.50.0/24 –p tcp –dport 1521 –j ACCEPT
  1. 仅允许内部合法IP段访问SSH远程连接服务
#ssh difference form other servers here……

iptables –A INPUT –p tcp –s 192.168.10.0/24 –dport 50718 –j ACCEPT
  1. 对HTTP服务的不同限制
a、对外提供HTTP服务的业务,要允许http服务通过、并且不限制IP

#http

iptables –A INPUT –p tcp –dport 80 –j ACCEPT

b、对内部提供http服务的业务、一般用特殊端口、并且限制合法IP连接或者VPN连接

iptables –A INPUT –s 192.168.132.0 –p tcp –m multiport –dport 8080,8081,8082,8888 –j ACCEPT

6、snmp的限制

iptables –A INPUT –s 192.168.132.0/24 –p UDP –dport 161 –j ACCEPT

iptables –A INPUT –s 192.168.155.0/24 –p UDP –dport 161 –j ACCEPT

iptables –A INPUT –s 192.168.166.0/24 –p UDP –dport 161 –j ACCEPT

7、 rsync 服务的限制策略

# rsync

iptables –A INPUT –s 192.168.132.0/24 –p tcp –m tcp –dport 873 –j ACCEPT

iptables –A INPUT –s 192.168.12.0/24 –p tcp –m tcp –dport 873 –j ACCEPT

iptables –A INPUT –s 192.168.1.0/24 –p tcp –m tcp –dport 873 –j ACCEPT

8、 nfs 服务的限制

iptables –A INPUT –s 192.168.10.0/24 –t TCP –m multiport –dport 111,892,2049 –j ACCEPT

iptables –A INPUT –s 192.168.100.0/24 –t TCP –m multiport –dport 111,892,2049 –j ACCEPT

9、ftp服务的限制

iptables –A INPUT –p tcp –dport 21 –j ACCEPT

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

提示:上面内容表示对已经建立连接的数据包、或者发出去的数据包允许通过。

说明 iptables –A INPUT –m state –state 状态 –j ACCEPT

下面几种

1) INVLID   #####不可用的。数据包不能被识别。比如说内存溢出。ICMP错误

2) ESTABLISHED  ###这个包已经建立了连接、

3) NEW  ####这个包已经开始新连接了。然后也是有双向的。但是还看不见

4) RELATED   ####这个包开始了一个新连接。并且和已经存在的连接已经有关联。
  1. icmp 协议的限制
iptables –A INPUT –p icmp –icmp-type any –j ACCEPT

iptables –A INPUT –p icmp –s 192.168.10.0/24 –m icmp –icmp-type any –j ACCEPT

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

提示:如果不想开。就不执行此行

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容