-n 新建链 -x 删除链(只能删除自定义链,而且必须先清除所有规则) -e 重命名 -p 设置链的默认策略,accept、drop
示例
iptables -n input_test 新建链input_test iptables -e input_test output_test 将自定义链input_test改名为output_test iptables -x output_test 删除链input_test iptables -p output drop 更改内置链的默认规则为drop,自定义链不能有默认规则
-a 追加为链中的最后一条规则 -i 插入,如不指定rule_number则默认插入到第一条 -r 替换,如不指定rule_number则默认替换第一条 -d 删除,可以指定删除某个rule_number,或指定删除满足某个匹配条件的rule -f 清空规则表,可清空某个表上的所有链的规则,清空所有表上的所有链的规则,也可清空某个链上的所有表的规则 -z 将规则的计数置0;iptables的每条规则,都有两个计数器:1、有本规则匹配到的所有的packets,2、由本规则匹配到的所有packets大小之和 -s 列出规则selected,以iptables-save命令的格式显示链上的规则;可重定向保存规则;相当于iptables-save命令;
示例
iptables -a output_test -s 192.168.1.0/24 -d 192.168.1.9 -p tcp -j accept ##追加规则到output_test链的最后 iptables -i output_test 1 -s 192.168.2.0/24 -d 192.168.1.9 -p tcp -j accept ##插入规则到output_test的最前,忽略rule_num默认插入到最前 iptables -r output_test 2 -s 192.168.3.0/24 -d 192.168.1.9 -p tcp -j drop ##将第二条规则替换 iptables -d output_test 2 ##删除output_test链中的第二条规则 iptables -z output_test 1 ##清空output_test链中的第一条规则的计数 iptables -z output_test ##清空output_test链中所有规则的计数 iptables -f ##清空所有规则 iptables -f -t filter ##清空filter表的所有规则 iptables -f output_test ##清空output_test链中的所有规则 iptables -s -t nat ##显示nat表的所有规则,格式和iptables-save一样,如果不指定表,则为默认的filter表
-l list,列出规则 -n 以数字形式显示ip地址和端口 -v 查看详细信息,支持-vv,-vvv -x 显示计数器的精确值而非单位换算后的结果; --line-numbers 显示每个rule的rule number
示例
iptables -vxnl ##不指定的-t的话,就默认查看所有filter表的规则,所有-l要放到是最后,因为-l是命令,-n和-v是选项,用来修饰命令的; iptables -vxnl input --line-numbers ##显示input链的过滤规则,并且显示规则id; 每一个自定义链都有引用计数,在内置链上引用一次,引用计数就加1,之后引用计数为0时才可以删除;
iptables是由规则组成的;而规则就是根据匹配条件来尝试匹配报文,一旦匹配成功,就由定义的处理动作作出处理;
匹配条件
处理动作
允许192.168.1.0/24网段访问192.168.2.0/24网段的非udp协议通过 iptables -a output_test -s 192.168.1.0/24 -d 192.168.2.0/24 ! -p udp -j accept 允许192.168.1.0/24网段访问192.168.2.0/24网段的tcp syn报文(只有syn置位)通过 iptables -a output_test -s 192.168.1.0/24 -d 192.168.2.0/24 -p tcp --syn -j accept 允许192.168.1.0/24网段访问192.168.2.0/24网段的icmp echo request报文通过 iptables -a output_test -s 192.168.1.0/24 -d 192.168.2.0/24 -p icmp --icmp-type 8/0 -j accept 允许192.168.1.0/24网段访问192.168.2.1主机的tcp21、22、23端口 iptables -a output_test -s 192.168.1.0/24 -d 192.168.2.1 -p tcp --dport 21:23 -j accept 允许192.168.2.1主机使用udp67口端访问所有主机 iptables -a output_test -s 192.168.2.1 -p udp --sport 67 -j accept
multiport示例
多端口一条规则指明:开放22和80端口: iptables -i input -s 0/0 -d 172.18.11.114 -p tcp -m multiport --dports 22,80 -j accept iptables -i output -d 0/0 -s 172.18.11.114 -p tcp -m multiport --sports 22,80 -j accept 如果想同时指定源端口范围和目的端口范围则需要指定两次-m选项 iptables -a output_test -s 172.16.11.114 -p udp -m multiport --sports 67,68 -m multiport --dports 67,68 -j accept
iptange示例
允许172.16.11.114服务区使用udp67、68端口访问192.168.1.0-192.168.1.10这些ip的udp67、68端口 iptables -a output_test -s 172.16.11.114 -p udp -m iprange --dst-range 192.168.1.0-192.168.1.10 -m multiport --sports 67,68 -m multiport --dports 67,68 -j accept
string示例
例如:禁止响应页面内容有test字样 iptables -i output -s 172.18.11.114 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "test" -j reject
time示例
在工作工作日的9:00-16:00,放行访问telnet服务 iptables -r input 4 -d 172.18.11.114 -p tcp --dport 23 -m iprange --src-range 172.18.11.1-172.18.11.111 -m time --timestart 09:00:00 --timestop 16:00:00 --kerneltz --weekdays 1,2,3,4,5 -j accept
connlimit示例
限制每台主机访问telnet最大并发数为2个 iptables -a input -s 0/0 -d 172.18.11.114 -p tcp --dport 23 -m connlimit --connlimit-upto 2 -j accept
limit示例
允许别人ping,设置一分钟20包(表示一次ping需要3秒),突发值(桶大小)为3,; 除了最开始的3个包,其余报文均按照一次ping需要3秒的设定 iptables -r input 5 -d 172.18.11.114 -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 3 -j accept
state
使用state使iptables支持ftp的被动模式 加载模块之后就支持ftp的连接追踪 [root@ftp ~]# modprobe nf_conntrack_ftp 下列配置可以支持ftp被动模式 [root@ftp ~]# iptables -vnl input chain input (policy accept 73 packets, 7631 bytes) pkts bytes target prot opt in out source destination 3 176 accept tcp -- * * 0.0.0.0/0 192.168.1.131 tcp dpts:21:22 state new 398 29161 accept all -- * * 0.0.0.0/0 192.168.1.131 state established 1 60 accept tcp -- * * 0.0.0.0/0 192.168.1.131 state related 8 472 drop all -- eth0 * 0.0.0.0/0 192.168.1.131 [root@ftp ~]# iptables -vnl output chain output (policy accept 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 313 38663 accept all -- * * 192.168.1.131 0.0.0.0/0 state new,established 0 0 drop all -- * eth0 192.168.1.131 0.0.0.0/0
一个linux主机上两块网卡,分别是a、b,a后面接着的终端访问b网卡自身的ip时,即便不开启linux主机上的net.ipv4.ip_forward也可以通信。因为这两个ip地址虽然配置在网卡上了,但却是它们属于同一个内核的,内核内部的通信不算转发;
转发的意思是,从这个内核转出去,这种情况下才需要开启net.ipv4.ip_forward;
配置forward链的filter表和配置input/output链的filter表是一样的,注意一下数据的流向即可;
临时开启转发
sysctl -w net.ipv4.ip_forward=1
永久开启转发
需要编辑/etc/sysctl.conf
配置转发规则是要注意的问题
三台设备上没有任何策略; 三台设备的路由表如下 [root@a-server ~]# route -n kernel ip routing table destination gateway genmask flags metric ref use iface 0.0.0.0 172.16.1.1 0.0.0.0 ug 100 0 0 ens33 172.16.1.0 0.0.0.0 255.255.255.0 u 100 0 0 ens33 [root@b-server ~]# route -n kernel ip routing table destination gateway genmask flags metric ref use iface 10.207.51.0 0.0.0.0 255.255.255.0 u 100 0 0 ens37 172.16.1.0 0.0.0.0 255.255.255.0 u 100 0 0 ens33 [root@c-server ~]# route -n kernel ip routing table destination gateway genmask flags metric ref use iface 10.207.51.0 0.0.0.0 255.255.255.0 u 100 0 0 ens33 已在b-server上开启了转发功能 sysctl -w net.ipv4.ip_forward=1 ##此方法临时生效
snat仅可以在postrouting和input链下nat表中使用;或者将snat定义在自定义链中,然后在postrouting和input链中的nat表下调用;
snat可以更改数据包的源地址和源端口
根据现在看到的效果,只有首包才会让rules的计数加1,例如ping -c 10 x.x.x.x 只会让规则的计数+1;ssh x.x.x.x后不管执行了多少操作,只会让规则的计数+1;
示例
让a可以访问c,就需要在b上做snat,将a访问c的数据包的源地址改为与c一个网段的ip地址; iptables -t nat -a postrouting -s 172.16.1.10 -d 10.207.51.92 -j snat --to-source 10.207.51.91 下面这种配置可以实现对10.207.51.90和10.207.51.91的循环使用,172.16.1.10使用10.207.51.90,172.16.1.11使用10.207.51.91; 已经提前在a和b上配置了子网卡 iptables -t nat -a postrouting -m iprange --src-range 172.16.1.10-172.16.1.11 -d 10.207.51.92 -j snat --to-source 10.207.51.90-10.207.51.91
masquerade只能在postrouting和input链下nat表中使用;
masquerade只应该用在动态分配ip地址(拨号上网)的时候,如果有静态ip地址,就不应该使用snat,masquerade是将数据包的源地址映射为接口的ip,可以理解为这就是pat;
根据现在看到的效果,只有首包才会让rules的计数加1,例如ping -c 10 x.x.x.x 只会让规则的计数+1;ssh x.x.x.x后不管执行了多少操作,只会让规则的计数+1;
--to-ports port[-port] ##指定源端口范围,但只在制定了协议后有效(tcp, udp, dccp or sctp.)
--random ##随机映射端口(kernel >= 2.6.21)
示例
这将使用b的接口地址进行nat转换 iptables -t nat -r postrouting 1 -m iprange --src-range 172.16.1.10-172.16.1.13 -d 10.207.51.92 -j masquerade
dnat只能配置在prerouting和output链下;或者在自定义链中定义,然后在prerouting和output链下调用。
dnat可以更改数据包的目的地址和目的端口;
根据现在看到的效果,只有首包才会让rules的计数加1,例如ping -c 10 x.x.x.x 只会让规则的计数+1;ssh x.x.x.x后不管执行了多少操作,只会让规则的计数+1;
为服务器配置了dnat之后,服务器自己向往访问并不会只用dnat映射的地址,还是需要为其配置snat;
--to-destination [ipaddr[-ipaddr]][:port[-port]] ##如果指定了这些协议(tcp, udp, dccp or sctp),则可以指定端口或端口范围;如果没有指定端口范围,则目的端口不会更改;如果没有指定ip地址,则只修改目标端口;
--random ##将随机映射端口(kernel >= 2.6.22)
--persistent ##为每个连接提供相同的源目地址;固定的;support for persistent mappings is available from 2.6.29-rc2.
示例
让c可以访问a,方法用dnat是将10.207.51.89, 映射为172.16.1.10 已经提前在a和b上配置了子网卡 iptables -t nat -a prerouting -d 10.207.51.89 -j dnat --to-destination 172.16.1.10 c ssh 10.207.51.89:19970时,可以登录到172.16.1.10:22 iptables -t nat -a prerouting -p tcp -d 10.207.51.89 --dport 19970 -j dnat --to-destination 172.16.1.10:22
redirect不能更改目的ip地址,只能更改目的端口,所以它可以将访问自己某个端口的数据包,重定向到自己的另一个端口上;
示例
目的端口为19970的数据包重定向到22端口; iptables -t nat -r prerouting 1 -p tcp -d 10.207.51.89 --dport 19970 -j redirect --to-port 22 使用dnat也可以实现一样的效果 iptables -t nat -r prerouting 1 -p tcp -d 10.207.51.89 --dport 19970 -j dnat --to-destination 10.207.51.89:22
保存:iptables-save > /path/from/some_rule_file
重载:iptables-restore < /path/from/some_rule_file
centos 6专用方式
保存:service iptables save ##保存在/etc/sysconf/iptables文件中,覆盖保存;
重载:service iptables restart ##重新载入/etc/sysconf/iptables文件中的内容‘;
centos7方式
使用iptables-save命令将规则导出到指定文件
在rc.local中定义脚本,使用iptables-restore回复规则
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论