iptables令不少小伙伴脑阔疼,下面咱们来讲说如何使用iptables。算法
经过iptables --help
查看一下iptables用法shell
[root@note1 ~]# iptables --help iptables v1.4.21 Usage: iptables -[ACD] 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] iptables -h (print this help information)
iptables [-t table] COMMAND chain [-m matchname [per-match-options]] -j targetname [per-target-options]
bash
iptables命令由 表 + 命令 + 链 + 匹配条件 + 处理动做
组成服务器
iptables由四表五链组成。每一个表分别实现不一样的功能,每一个表拥有不一样的链,链表明规则实现的位置。网络
四表分别为:app
五链分别为:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING。ssh
不一样表支持的链:curl
添加规则时的考量点:tcp
链:链上的规则次序,即为检查的次序;所以,隐含必定的应用法则:ide
使用iptables命令时若不使用-t
指明操做哪张表,默认操做filter表。
iptables命令有三大类,查看,链管理,规则管理
-t : 查看的表
-n :不进行 IP 与 HOSTNAME 的反解
-v :列出更多的信息,包括经过该规则的封包总位数、相关的网络接口等.
-L :列出目前的 table 的规则.
-S :查看规则定义,
--line-number用于查看规则号.
#使用iptables查看规则 [root@note1 ~]# iptables -vnL --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 467 29128 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 41 packets, 4276 bytes) num pkts bytes target prot opt in out source destination [root@note1 ~]# [root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 502 31476 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]# #使用-S选项查看iptables的规则定义 [root@note1 ~]# iptables -S -P INPUT ACCEPT -P FORWARD ACCEPT -P OUTPUT ACCEPT -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-N:new, 自定义一条新的规则链;
iptables -N test
-X:delete,删除自定义的规则链;
注意:仅能删除用户自定义的引用计数为0的空的链;
iptables -X test
-E:重命名自定义链;引用计数不为0的自定义链不可以被重命名,也不能被删除;
iptables -N testrn iptables -E testrn testrename
-P:Policy,设置默认策略;对filter表中的链而言,其默认策略有:
使用需谨慎,因为我测试时,没有先增长一条放行ssh的规则,因此在我将filter的INPUT链默认策略改成DROP后,我已经没法经过Xshell连接虚拟机了,须要进入VMware放行ssh。
iptables -P INPUT DROP
增长了放行规则后,咱们已经成功使用Xshell从新连上了主机
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
使用命令添加默认策略
#先放行ssh,INPUT链及OUTPUT链都要放行。 iptables -A INPUT -d 176.16.128.1 -p tcp --dport 22 -j ACCEPT iptables -A OUTPUT -s 176.16.128.1 -p tcp --sport 22 -j ACCEPT #添加新规则的时候要插入在默认拒绝规则前,除这些规则外的都将拒绝。 iptables -A INPUT -d 176.16.128.1 -j REJECT iptables -A OUTPUT -s 176.16.128.1 -j REJECT #设置链上的默认策略为容许。 iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT
-A:append,在已有规则后追加规则;
# 在note1节点增长一条拒绝80端口的规则 [root@note1 local]# iptables -A INPUT -p tcp --dport 80 -j REJECT # 咱们能够看到因为是使用追加命令追加的规则,这条规则的位置为2 [root@note1 local]# iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 4208 225K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 2 120 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable # 在主机点访问note1节点的80端口 [root@master ~]# curl note1:80 curl: (7) Failed connect to note1:80; 拒绝链接 [root@master ~]#
-R:replace,替换指定链上的指定规则;
# 使用-R命令修改拒绝80端口的规则为接受访问 [root@note1 local]# iptables -R INPUT 2 -p tcp --dport 80 -j ACCEPT # 查看iptables [root@note1 local]# iptables -vnL --line-numbers Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 4881 271K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 # 在master节点访问80端口,能够看到网页的内容了。 [root@master ~]# curl note1:80 <h1>I'm Note1</h1> [root@master ~]#
-I:insert, 插入,要指明位置,省略时表示第一条;
# 使用iptables -I不指定位置插入规则。 [root@note1 ~]# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT # 查看iptables,显示新增长的规则为第一条。 [root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 2 616 38140 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]# #使用iptables -I指定在第二条插入规则。 [root@note1 ~]# iptables -I INPUT 2 -p tcp --dport 443 -j ACCEPT [root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 3 810 50540 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]#
-D:delete,删除规则按照规则序号或规则自己
[root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 3 835 52340 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]# iptables -D INPUT 2 [root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 2 882 55100 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]#
[root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 2 882 55100 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]# iptables -D INPUT -p tcp --dport 3306 -j ACCEPT [root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1016 62940 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]#
iptables的每条规则都有两个计数器:
[root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 11 packets, 774 bytes) num pkts bytes target prot opt in out source destination 1 1028 63752 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]# iptables -Z INPUT [root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 6 364 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]#
[root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 5 packets, 180 bytes) num pkts bytes target prot opt in out source destination 1 46 2728 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [root@note1 ~]# iptables -F INPUT [root@note1 ~]# iptables -vnL INPUT --line-number Chain INPUT (policy ACCEPT 6 packets, 364 bytes) num pkts bytes target prot opt in out source destination [root@note1 ~]#
无需加载任何模块,由iptables/netfilter自行提供;
[!] -s, --source address[/mask][,...]:检查报文中的源IP地址是否符合此处指定的地址或范围; [!] -d, --destination address[/mask][,...]:检查报文中的目标IP地址是否符合此处指定的地址或范围;全部地址:0.0.0.0/0 [!] -p, --protocol protocol: tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or "all" 最经常使用的协议tcp、udp、icmp; [!] -i, --in-interface 数据报文流入的接口;只能应用于数据报文流入的环节,只能应用于PREROUTING,INPUT和FORWARD链; [!] -o, --out-interface 数据报文流出的接口;只能应用于数据报文流出的环节,只能应用于FORWARD、OUTPUT和POSTROUTING链;
[!]中的叹号表示取反的意思
隐式扩展:不须要手动加载扩展模块;由于它们是对协议的扩展,因此在使用-p选项指明了特定的协议时,就表示已经指明了要扩展的模块,无需再同时使用-m选项指明扩展模块的扩展机制。
[!] --source-port, --sport port[:port]: 匹配报文的源端口;能够是端口范围; [!] --destination-port, --dport port[:port]: 匹配报文的目标端口;能够是端口范围; [!] --tcp-flags mask comp mask是咱们应该检查的标志,以逗号分隔,例如 SYN,ACK,FIN,RST comp是必须设置的标志,例如SYN 例如:“--tcp-flags SYN,ACK,FIN,RST SYN”表示,要检查的标志位为SYN,ACK,FIN,RST四个,其中SYN必须为1,余下的必须为0; [!] --syn:用于匹配第一次握手,至关于”--tcp-flags SYN,ACK,FIN,RST SYN“;
[!]叹号表示取反的意思
[!] --source-port, --sport port[:port]: 匹配报文的源端口;能够是端口范围; [!] --destination-port, --dport port[:port]: 匹配报文的目标端口;能够是端口范围;
[!]叹号表示取反的意思
[!] --icmp-type {type[/code]|typename}
[!]叹号表示取反的意思
类型为8:请求回送echo-request(Ping 请求)
类型为0:回送应答echo-reply(Ping 应答)
咱们设置INPUT放行icmp-type类型为0的报文,OUTPUT放行icmp-type类型为8的报文,默认规则设置为拒绝,这样就能够只容许咱们ping其余主机,不容许其余主机ping咱们。
#由于要增长默认拒绝规则,因此先放行ssh [root@note1 ~]# iptables -A INPUT -d 176.16.128.1 -p tcp --dport 22 -j ACCEPT [root@note1 ~]# iptables -A OUTPUT -s 176.16.128.1 -p tcp --sport 22 -j ACCEPT #增长默认拒绝规则 [root@note1 ~]# iptables -A INPUT -d 176.16.128.1 -j REJECT [root@note1 ~]# iptables -A OUTPUT -s 176.16.128.1 -j REJECT [root@note1 ~]# iptables -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 299 19206 ACCEPT tcp -- * * 0.0.0.0/0 176.16.128.1 tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0 176.16.128.1 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 165 15559 ACCEPT tcp -- * * 176.16.128.1 0.0.0.0/0 tcp spt:22 0 0 REJECT all -- * * 176.16.128.1 0.0.0.0/0 reject-with icmp-port-unreachable #如今咱们尝试ping,因为ping未在iptables中设置因此ping请求没法发送。 [root@note1 ~]# ping 176.16.128.8 PING 176.16.128.8 (176.16.128.8) 56(84) bytes of data. ping: sendmsg: 不容许的操做 ping: sendmsg: 不容许的操做 ping: sendmsg: 不容许的操做 ^C --- 176.16.128.8 ping statistics --- 3 packets transmitted, 0 received, 100% packet loss, time 1999ms #如今咱们在OUTPUT链上增长一条容许发送ping请求的规则 [root@note1 ~]# iptables -I OUTPUT 2 -s 176.16.128.1 -p icmp --icmp-type 8 -j ACCEPT #尝试ping,发现请求能够发送了,可是未有响应回来 [root@note1 ~]# ping 176.16.128.8 PING 176.16.128.8 (176.16.128.8) 56(84) bytes of data. #咱们使用tcpdump抓包,发现ping请求是有响应回来的。是INPUT链没有放行。 [root@note1 ~]# tcpdump -i eno16777736 icmp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eno16777736, link-type EN10MB (Ethernet), capture size 262144 bytes 20:45:00.605683 IP note1 > master: ICMP echo request, id 4276, seq 64, length 64 20:45:00.605962 IP master > note1: ICMP echo reply, id 4276, seq 64, length 64 20:45:01.606935 IP note1 > master: ICMP echo request, id 4276, seq 65, length 64 20:45:01.607533 IP master > note1: ICMP echo reply, id 4276, seq 65, length 64 ^C 8 packets captured 8 packets received by filter 0 packets dropped by kernel [root@note1 ~]# #咱们在iptables的INPUT链放行ping请求的响应。 [root@note1 ~]# iptables -I INPUT 2 -d 176.16.128.1 -p icmp --icmp-type 0 -j ACCEPT #至此咱们已经ping通了其余主机。 [root@note1 ~]# ping 176.16.128.8 PING 176.16.128.8 (176.16.128.8) 56(84) bytes of data. 64 bytes from 176.16.128.8: icmp_seq=228 ttl=64 time=0.687 ms 64 bytes from 176.16.128.8: icmp_seq=229 ttl=64 time=0.432 ms ^C --- 176.16.128.8 ping statistics --- 231 packets transmitted, 4 received, 98% packet loss, time 230101ms rtt min/avg/max/mdev = 0.432/0.804/1.443/0.382 ms [root@note1 ~]#
若要容许其余主机也能ping咱们。在INPUT链中追加一条放行icmp-type类型为8的报文,OUTPUT放行icmp-type类型为0的报文,这样就均可以ping通了。
显式扩展:必须使用-m选项指明要调用的扩展模块的扩展机制;
使用
man iptables-extensions
来查看显示扩展的用法。
以离散或连续的方式定义多端口匹配条件,最多15个;
[!]--source-ports, --sports port[,port|,port:port]...:指定多个源端口; [!]--destination-ports, --dports port[,port|,port:port]...:指定多个目标端口;
[!]叹号表示取反的意思
咱们说过iptables要尽可能将那些可由一条规则描述的多个规则合并起来,不但能够更简洁,这样也能够提升报文经过的效率。
#使用iptables放行21,22,23,80,139,443,445,3306等端口。 iptables -A INPUT -p tcp -m multiport --dports 21:23,80,139,443,445,3306 -j ACCEPT
以连续地址块的方式来指明多IP地址匹配条件;
[!] --src-range from[-to] #源地址区间 [!] --dst-range from[-to] #目标地址区间
[!]叹号表示取反的意思
设置放行176.16.128.5-176.16.128.10区间的IP能够访问主机
[root@note1 init.d]#iptables -I INPUT 2 -p icmp --icmp-type 8 -m iprange --src-range 176.16.128.5-176.16.128.10 -j ACCEPT [root@note1 init.d]#iptables -I OUTPUT 2 -p icmp --icmp-type 0 -s 176.16.128.1 -j ACCEPT #使用176.16.128.2 Ping主机,是没有回应的。 [root@note2 ~]# ping 176.16.128.1 PING 176.16.128.1 (176.16.128.1) 56(84) bytes of data. ^C --- 176.16.128.1 ping statistics --- 11 packets transmitted, 0 received, 100% packet loss, time 10076ms [root@note2 ~]# #使用176.16.128.8 Ping主机,在ip区间内是能够收到回复的。 [root@master ~]# ping 176.16.128.1 PING 176.16.128.1 (176.16.128.1) 56(84) bytes of data. 64 bytes from 176.16.128.1: icmp_seq=1 ttl=64 time=0.539 ms 64 bytes from 176.16.128.1: icmp_seq=2 ttl=64 time=0.922 ms ^C --- 176.16.128.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1020ms rtt min/avg/max/mdev = 0.539/0.730/0.922/0.193 ms [root@master ~]#
指定数据包到达时间/日期范围的匹配条件。
--timestart hh:mm[:ss] --timestop hh:mm[:ss] [!] --weekdays day[,day...] [!] --monthdays day[,day...] --datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]] --kerneltz:使用内核配置的时区而非默认的UTC;
[!]叹号表示取反的意思
通常时间或与周几联用,或时间与每个月几号联用。日期通常不经常使用。
#INPUT链放行工做区域176.16.128.5-176.16.128.10的主机在周一至周五的早9点到晚5点能够访问telnet服务。 iptables -I INPUT 2 -d 176.16.128.1 -p tcp --dport 23 -m iprange --src-range 176.16.128.5-176.16.128.10 -m time --timestart 9:00:00 --timestop 17:00:00 --weekdays 1,2,3,4,5 --kerneltz -j ACCEPT #OUTPUT链放行telnet服务。 iptables -I OUTPUT 2 -s 176.16.128.1 -p tcp --sport 23 -j ACCEPT
该模块使用某种模式匹配策略来匹配给定的字符串。
--algo {bm|kmp} #匹配算法 [!] --string pattern #要过滤的字符串 [!] --hex-string pattern #要检查的字符串的十六进制编码 --from offset #从报文的哪一个位置开始检查 --to offset #从报文的哪一个位置结束检查
[!]叹号表示取反的意思。
只对明文编码的协议生效。
# 出栈报文中包含字符串gay拒绝访问。 iptables -I OUTPUT -m string --algo bm --string "gay" -j REJECT
容许您限制每一个客户端地址与服务器的并行链接数。
--connlimit-upto n #上限 小于等于 --connlimit-above n #下限 大于等于
取决于默认规则是什么,默认规则是拒绝,使用upto,设置低于就容许,不低于就被默认规则所匹配。
# 设置每一个客户端ssh的链接不大于2个。 iptables -I INPUT -p tcp -d 176.16.128.1 --dport 22 -m connlimit --connlimit-upto 2 -j ACCEPT
此模块使用令牌桶限制请求的速率。
--limit rate[/second|/minute|/hour|/day] #每秒、每分、每小时、天天多少个。 --limit-burst number #一批最多个数、峰值(桶大小)
限制本机某tcp服务接收新请求的速率:--syn, -m limit
# 限制主机Ping请求每分钟20次,峰值发三次。 [root@note1 sysconfig]# iptables -I INPUT 2 -p icmp --icmp-type 8 -m limit --limit 20/minute --limit-burst 3 -j ACCEPT [root@note1 sysconfig]# iptables -I OUTPUT 2 -p icmp --icmp-type 0 -j ACCEPT [root@note1 sysconfig]# iptables -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 893 57538 ACCEPT tcp -- * * 0.0.0.0/0 176.16.128.1 tcp dpt:22 #conn src/32 <= 2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 limit: avg 20/min burst 3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 176.16.128.1 multiport dports 80,443,3306 2 104 REJECT all -- * * 0.0.0.0/0 176.16.128.1 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 547 39791 ACCEPT tcp -- * * 176.16.128.1 0.0.0.0/0 multiport sports 22,80,443,3306 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0 10 848 REJECT all -- * * 176.16.128.1 0.0.0.0/0 reject-with icmp-port-unreachable [root@note1 sysconfig]# #Ping主机,观察应答的时间,得出已经成功限制Ping请求速率。 [root@master ~]# ping 176.16.128.1 PING 176.16.128.1 (176.16.128.1) 56(84) bytes of data. 64 bytes from 176.16.128.1: icmp_seq=1 ttl=64 time=0.692 ms 64 bytes from 176.16.128.1: icmp_seq=2 ttl=64 time=0.684 ms 64 bytes from 176.16.128.1: icmp_seq=3 ttl=64 time=0.722 ms 64 bytes from 176.16.128.1: icmp_seq=4 ttl=64 time=0.706 ms 64 bytes from 176.16.128.1: icmp_seq=7 ttl=64 time=1.10 ms 64 bytes from 176.16.128.1: icmp_seq=10 ttl=64 time=1.89 ms 64 bytes from 176.16.128.1: icmp_seq=13 ttl=64 time=0.983 ms ^C --- 176.16.128.1 ping statistics --- 14 packets transmitted, 7 received, 50% packet loss, time 13093ms rtt min/avg/max/mdev = 0.684/0.969/1.893/0.409 ms [root@master ~]#
state模块容许访问此数据包的链接跟踪状态。
#仅放行哪些链接的状态。 [!] --state state INVALID, ESTABLISHED, NEW, RELATED or UNTRACKED.
NEW: 新链接请求;
ESTABLISHED:已创建的链接;
INVALID:没法识别的链接;
RELATED:相关联的链接,当前链接是一个新请求,但附属于某个已存在的链接;
UNTRACKED:未追踪的链接;
state扩展:
内核模块装载:
nf_conntrack
nf_conntrack_ipv4
手动装载:
nf_conntrack_ftp
追踪到的链接:
/proc/net/nf_conntrack
调整可记录的链接数量最大值:
/proc/sys/net/nf_conntrack_max
超时时长:
/proc/sys/net/netfilter/timeout
-j targetname [per-target-options]
ACCEPT 容许
DROP 丢弃
--reject-with type
--log-level
--log-prefix
默认日志保存于/var/log/messages
返回调用者;
自定义链作为target:
保存:iptables-save > /PATH/TO/SOME_RULE_FILE
重载:iptabls-restore < /PATH/FROM/SOME_RULE_FILE
-n, --noflush
:不清除原有规则-t, --test
:仅分析生成规则集,但不提交
保存规则:service iptables save
保存规则于/etc/sysconfig/iptables
文件,覆盖保存;
重载规则:service iptables restart
默认重载/etc/sysconfig/iptables
文件中的规则
配置文件:/etc/sysconfig/iptables-config
(1) 自定义Unit File,进行iptables-restore
(2) firewalld服务;
(3) 自定义脚本;