Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1662768
  • 博文数量: 631
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3920
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-06 21:58
个人简介

博客是我工作的好帮手,遇到困难就来博客找资料

文章分类

全部博文(631)

文章存档

2022年(2)

2021年(4)

2020年(40)

2019年(4)

2018年(78)

2017年(213)

2016年(41)

2015年(183)

2014年(66)

我的朋友

分类: 系统运维

2016-07-13 12:06:34

iptables由3个表filter,nat,mangle组成,主要实验了filter表,这个表是用来过滤数据包的,

有三个链INPUT,OUTPUT,FORWARD。

配置防火墙策略有固定的格式


Iptables  表名   链名    匹配条件  动作


-t 表名 (默认为filter)


-A 链名(在该链末尾append追加策略)


-I 链名 数字


-I (insert)插入链,如果不加数字,默认是将写的策略添加到表中所有策略的前面


但是我们要指定插入到相应的行,我们可以这样

Iptables –t filter –I INPUT 2 ……  这里就是插到第二个

匹配条件:

-i   网卡    数据包进入的网卡

-o  网卡   出去的

-s   ip   源ip

-d   ip    目的ip

-p   协议

--dport  端口号   目的端口号

--sport   端口号   源端口号

动作:

ACCEPT:对满足策略的数据包允许通过

DROP:丢弃数据包,且不返回任何信息

REJECT:丢弃数据包,但是会返回拒绝的信息

LOG:把通过的数据包写到日志中(相当于一个门卫对进去的人进行登记)


iptables   -t filter  -A INPUT –s 192.168.0.0/24  -p tcp  --dport 80 –j REJECT

上面这句的意思是:对filter表的INPUT链,追加一条策略,策略是,源地址在192.168.0.0/24网段内,使用tcp协议,目标端口为80的所有输入包都执行REJECT动作(拒绝)

实验室完成了filter表的INPUT链的基本操作和增加删除一条链,过程如下:



Iptables命令的管理控制选项

-A(append) 在指定链的末尾添加一条新的规则

-I (insert)在指定链中插入一条新规则,为指明插入位置

-D(delete)删除指定链里的某条规则

-R (replace)修改、替换指定链的某条规则,按序号或内容确定要替换的规则

-L (list)列出指定链中所有规则进行查看

-F (flush)清空指定链中的所有规则

-N 新建一条用户自己定义的规则链

-X(delete-chain)删除指定表中用户自己定义的规则链

-P 设置指定链的默认策略

-V 查看iptables命令工具的版本(--version)信息

-v 查看规则列表时显示详细(--verbose)的信息

-h 查看命令帮助信息(--help)

-n 使用数字形式(--number)显示输出结果,如显示主机的ip地址而不是主机名

--line-number 查看规则列表时,同时显示规则在链中的顺序号


添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。


[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP


再插入一条规则到第三行,将行数直接写到规则链的后面:


[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

查看:

[root@test ~]# iptables -nLv --line-number

Iptables命令的匹配条件选项

Ip地址:

来源地址:—s (source)。

目标地址:—d (destination)。

端口(port):

来源端口:——sport。

目标端口:——dport。

协议:—p。

个服务的端口和协议:


这里只列出比较常用的参数,详细的请查看man iptables

1、查看

iptables -nvL –line-number

-L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数

-n 不对ip地址进行反查,加上这个参数显示速度会快很多

-v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口

–line-number 显示规则的序列号,这个参数在删除或修改规则时会用到


[plain] view plain copy

  1. [root@mail ~]# iptables -L  #查看当前内存中iptables策略,默认是filter表  

  2. Chain INPUT (policy ACCEPT)  

  3. target     prot opt source               destination           

  4.   

  5. Chain FORWARD (policy ACCEPT)  

  6. target     prot opt source               destination           

  7.   

  8. Chain OUTPUT (policy ACCEPT)  

  9. target     prot opt source               destination           

  10. [root@mail ~]# iptables -t filter -vnL  #加vn参数,有更多选项  

  11. Chain INPUT (policy ACCEPT 31471 packets, 4322K bytes)  

  12.  pkts bytes target     prot opt in     out     source               destination           

  13.   

  14. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)  

  15.  pkts bytes target     prot opt in     out     source               destination           

  16.   

  17. Chain OUTPUT (policy ACCEPT 37490 packets, 3056K bytes)  

  18.  pkts bytes target     prot opt in     out     source               destination         

  19.    

  20. #策略格式:iptables 表名 链名 匹配条件 动作  


  21. #下面这句话的意思是


  22. 对于filter表的INPUT链,源地址为192.169.1.0/24网段内的使用tcp  #协议80端口的输入包,都执行REJECT(拒绝)动作  


  23. [root@mail ~]# iptables -t filter -A INPUT -s 192.169.1.0/24 -p tcp --dport 80 -j REJECT  


  24. [root@mail ~]# iptables -L  #需要注意的是,该策略目前只在内存中,/etc/sysconfig/iptables配置文件中是没有的  


  25. Chain INPUT (policy ACCEPT)  

  26. target     prot opt source               destination           

  27.   

  28.   

  29.   

  30. REJECT     tcp  --  192.169.1.0/24       anywhere            tcp dpt:http reject-with icmp-port-unreachable   

  31.   

  32. Chain FORWARD (policy ACCEPT)  

  33. target     prot opt source               destination           

  34.   

  35. Chain OUTPUT (policy ACCEPT)  

  36. target     prot opt source               destination           

  37. [root@mail ~]# iptables -vnL    #看的更详细点  

  38. Chain INPUT (policy ACCEPT 88 packets, 8188 bytes)  

  39.  pkts bytes target     prot opt in     out     source               destination           

  40.     4   240 REJECT     tcp  --  *      *       192.169.1.0/24       0.0.0.0/0           tcp dpt:80 reject-with icmp-port-unreachable   

  41.   

  42. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)  

  43.  pkts bytes target     prot opt in     out     source               destination           

  44.   

  45. Chain OUTPUT (policy ACCEPT 131 packets, 11625 bytes)  

  46.  pkts bytes target     prot opt in     out     source               destination           

  47. [root@mail ~]# vim /etc/sysconfig/iptables  #iptables中没有  

  48.   

  49.   

  50. # Firewall configuration written by system-config-firewall  

  51. # Manual customization of this file is not recommended.  

  52. *filter  

  53. :INPUT ACCEPT [0:0]  

  54. :FORWARD ACCEPT [0:0]  

  55. :OUTPUT ACCEPT [0:0]  

  56. COMMIT  

  57.   

  58.   

  59. #执行save后会将这条策略写入/etc/sysconfig/iptables,在保存的时候,是执行覆盖式的保存

  60.   

  61. #内存中有的保存下来,内存中没有的,这个文件中有的将会被删除。 

  62.  

  63. [root@mail ~]# service iptables save      

  64. iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]  

  65. [root@mail ~]# vim /etc/sysconfig/iptables  

  66.   

  67.   

  68. # Generated by iptables-save v1.4.7 on Wed Aug 15 17:28:53 2012  

  69. *filter  

  70. :INPUT ACCEPT [4:352]  

  71. :FORWARD ACCEPT [0:0]  

  72. :OUTPUT ACCEPT [4:298]  

  73. -A INPUT -s 192.169.1.0/24 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable   

  74. COMMIT  

  75. # Completed on Wed Aug 15 17:28:53 2012  

  76.   

  77.   

  78. #表的每条链后面都有一个默认动作,Chain INPUT (policy ACCEPT),默认动作意思是  


  79. #没有匹配所以策略的匹配条件时(按序匹配),就执行的动作,可以修改链的默认动作  


  80. [root@mail ~]# iptables -t filter -P INPUT DROP


  81.              #修改filter表的INPUT链的默认动作  


  1. [root@mail ~]# iptables -L  


  2. Chain INPUT (policy DROP) 

  3.  

  4. target     prot opt source               destination           

  5. REJECT     tcp  --  192.169.1.0/24       anywhere            tcp dpt:http reject-with icmp-port-unreachable   

  6.   

  7. Chain FORWARD (policy ACCEPT)  

  8. target     prot opt source               destination           

  9.   

  10. Chain OUTPUT (policy ACCEPT)  

  11. target     prot opt source               destination    

  12.   

  13. [root@mail ~]# iptables -t filter -P INPUT ACCEPT   #暂时改回来  

  14.   

  15. #可以删除一条策略,策略是有序的,从1开始,要删除一条策略,需要知道它的序号  


  16. [root@mail ~]# iptables -L --line-numbers   #查看策略的序号  


  17. Chain INPUT (policy ACCEPT)  

  18. num  target     prot opt source               destination           

  19. 1    REJECT     tcp  --  192.169.1.0/24       anywhere            tcp dpt:http reject-with icmp-port-unreachable   

  20.   

  21. Chain FORWARD (policy ACCEPT)  

  22. num  target     prot opt source               destination           

  23.   

  24. Chain OUTPUT (policy ACCEPT)  

  25. num  target     prot opt source               destination    

  26.        

  27. [root@mail ~]# iptables -D INPUT 1  #删除INPUT链的序号为1的策略  


  28. [root@mail ~]# vim /etc/sysconfig/iptables  #和前面一样,这只是删除内存中

  29. 的,/etc/sysconfig/iptables中仍然存在  

  30.   

  31. # Generated by iptables-save v1.4.7 on Wed Aug 15 17:28:53 2012  

  32. *filter  

  33. :INPUT ACCEPT [4:352]  

  34. :FORWARD ACCEPT [0:0]  

  35. :OUTPUT ACCEPT [4:298]  

  36. -A INPUT -s 192.169.1.0/24 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable  

  37. COMMIT  

  38. # Completed on Wed Aug 15 17:28:53 2012  

  39.   

  40.   

  41. [root@mail ~]# service iptables save    #执行保存,配置文件中也被删除了  

  42. iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]  


  43. [root@mail ~]# vim /etc/sysconfig/iptables  

  44.   

  45. # Generated by iptables-save v1.4.7 on Wed Aug 15 17:45:03 2012  

  46. *filter  

  47. :INPUT ACCEPT [0:0]  

  48. :FORWARD ACCEPT [0:0]  

  49. :OUTPUT ACCEPT [0:0]  

  50. COMMIT  

  51. # Completed on Wed Aug 15 17:45:03 2012  

  52.   

  53.   

  54. #除了INPUT,FORWARD,OUTPUT链,可以定义自己的链 

  55.  

  56. [root@mail ~]# iptables -N chen     #定义一个chen链,相当于多了一扇门  

  57.   

  58. #拒绝通过chen链,地址为192.169.1.99,协议为tcp端口为80的数据包  


  59. [root@mail ~]# iptables -t filter -A chen -s 192.169.1.99 -p tcp --dport 80 -j REJECT     

  60. [root@mail ~]# iptables -t filter -A INPUT -j chen  #把经过INPUT链的数据引入到chen这个链上  


  61. [root@mail ~]# iptables -L --line-numbers    

  62.  

  63. Chain INPUT (policy ACCEPT)  

  64. num  target     prot opt source               destination           

  65. 1    chen       all  --  anywhere             anywhere    #INPUT链的target变为chen了          

  66.   

  67. Chain FORWARD (policy ACCEPT)  

  68. num  target     prot opt source               destination           

  69.   

  70. Chain OUTPUT (policy ACCEPT)  

  71. num  target     prot opt source               destination           

  72.   

  73. Chain chen (1 references)   #可以看到多了一个链,下面的策略也存在  

  74. num  target     prot opt source               destination           

  75.   

  76. 1    REJECT     tcp  --  192.169.1.99         anywhere            tcp dpt:http reject-with icmp-port-unreachable   

  77. [root@mail ~]#   

  78. [root@mail ~]# service iptables save  


  79. iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]  


  80. [root@mail ~]# vim /etc/sysconfig/iptables  

  81.   

  82.   

  83. # Generated by iptables-save v1.4.7 on Wed Aug 15 18:11:28 2012  

  84. *filter  

  85. :INPUT ACCEPT [1:125]  

  86. :FORWARD ACCEPT [0:0]  

  87. :OUTPUT ACCEPT [1:71]  

  88. :chen - [0:0]  

  89. -A INPUT -j chen   

  90. -A chen -s 192.169.1.99/32 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable   

  91. COMMIT  

  92. # Completed on Wed Aug 15 18:11:28 2012  

  93.   

  94.   

  95. [root@mail ~]# iptables -X chen     #删除chen这条链  


  96. iptables: Too many links.  


  97. [root@mail ~]# iptables -F      #删除前需要清空策略,否则删除不掉  


  98. [root@mail ~]# iptables -X chen  

  99. [root@mail ~]# iptables -L  

  100. Chain INPUT (policy ACCEPT)  

  101. target     prot opt source               destination           

  102.   

  103. Chain FORWARD (policy ACCEPT)  

  104. target     prot opt source               destination           

  105.   

  106. Chain OUTPUT (policy ACCEPT)  

  107. target     prot opt source               destination           

  108. [root@mail ~]#   


阅读(1299) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~