iptables是linux/unix自帶的一款開源基於包過濾的防火牆工具,使用很是靈活,對硬件資源需求不是很高,是在內核中集成的服務,主要工做在OSI的2、3、四層。linux
Netfilter:是表的容器 表:鏈的容器 鏈:規則的容器 規則:iptables一系列過濾信息的規範和具體方法web
工做流程:vim
客戶端請求數據------》iptables Filter-------》獲取主機的服務(直接拒絕Drop)centos
數據包————過濾規則 1————過濾規則 2————默認規則 拒絕就 Drop--------------------------------後面的規則不起做用 沒有匹配————拒絕就 Drop--------------後面的規則不起做用 前兩個規則都沒有匹配—————默認規則去過濾
防火牆是層層過濾,數據包的匹配規則是自上而下順序匹配,若是前面都沒有匹配上規則(這個匹配規則是不管經過仍是拒絕都是匹配上規則),明確經過或阻止,最後交給防火牆默認規則去處理bash
經常使用的表有:filter、nat、mangle網絡
完整過程:app
一、數據包進入----通過NAT PREROUTING----通過 FORWARD----FILTER INPUT----NAT OUTPUT----FILTER OUTPUT----NAT POSTROUTING 主要用於NAT或端口映射tcp
二、數據包通過----通過 FORWARD----FILTER FORWARD----NAT POSTROUTING 主要用於過濾工具
[root@VM_0_7_centos ~]# iptables -h iptables v1.4.7 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) Commands: Either long or short options are allowed. --append -A chain Append to chain 把規則添加到鏈的結尾 --check -C chain Check for the existence of a rule --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' 指定端口類型,如tcp,udp [!] --source -s address[/mask][...] source specification 原規則(-s 後面跟IP地址) [!] --destination -d address[/mask][...] destination specification [!] --in-interface -i input name[+] network interface name ([+] for wildcard) 網絡接口名稱(後面跟網絡接口,如eth0) --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) 網絡接口名稱(是output的網絡接口名稱,和-i區別開來) --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@VM_0_7_centos ~]# /etc/init.d/iptables status 查看iptables的運行狀態 Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination [root@VM_0_7_centos ~]# iptables -L -n 查看錶列表和端口地址 Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
[root@VM_0_7_centos ~]# iptables -F 清除iptables規則 [root@VM_0_7_centos ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
[root@VM_0_7_centos ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
由於默認的是filter表,因此和iptables -F同樣網站
配置防火牆前最好寫一個定時任務,每多長時間就關閉防火牆,這樣就能夠防止沒法遠程登陸
-A 把規則添加到鏈的結尾
-I 把規則添加到鏈的開頭
[root@VM_0_7_centos ~]# iptables -t filter -A INPUT -p tcp --dport 9001 -j DROP [root@VM_0_7_centos ~]# iptables -t filter -A INPUT -p tcp --dport 9002 -j DROP [root@VM_0_7_centos ~]# /etc/init.d/iptables status Table: filter 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:9001 2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination [root@VM_0_7_centos ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9001 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
在實際的企業應用中,若是默認的規則是容許的,可是我又得禁止某一個應用或服務時,就會用到-I參數,將規則放在最前面,好比你發現一個網站被一個IP頻繁訪問,就能夠用它來禁止
[root@VM_0_7_centos ~]# iptables -t filter -I INPUT -p tcp -s 10.0.100.1 --dport 9003 -j ACCEPT [root@VM_0_7_centos ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0/0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9001 3 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination
刪除某一行規則
[root@VM_0_7_centos ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0/0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9001 3 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9002 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination [root@VM_0_7_centos ~]# iptables -D INPUT 3 [root@VM_0_7_centos ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0/0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9001 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination
禁止某網段的流量
[root@VM_0_7_centos ~]# iptables -A INPUT -i eth0 -s 10.0.0.0/24 -j DROP
[root@VM_0_7_centos ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 10.0.100.1 0.0.0.0/0 tcp dpt:9003 2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9001 3 DROP all -- 10.0.0.0/24 0.0.0.0/0 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination
清除規則
[root@VM_0_7_centos ~]# iptables -F [root@VM_0_7_centos ~]# iptables -X [root@VM_0_7_centos ~]# iptables -Z [root@VM_0_7_centos ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination
配置規則使得內網用戶可用
[root@VM_0_7_centos ~]# iptables -A INPUT -s 100.1.3.9/24 -j ACCEPT [root@VM_0_7_centos ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT [root@VM_0_7_centos ~]# iptables -A INPUT -i lo -j ACCEPT [root@VM_0_7_centos ~]# iptables -A OUTPUT -o lo -j ACCEPT
設置規則(默認規則)
[root@VM_0_7_centos ~]# iptables -P INPUT DROP [root@VM_0_7_centos ~]# iptables -P FORWARD DROP [root@VM_0_7_centos ~]# iptables -P OUTPUT DROP
配置用戶訪問web頁面(這個不能禁止,由於網站是開放給全球用戶訪問)
[root@VM_0_7_centos ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT [root@VM_0_7_centos ~]# /etc/init.d/iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination
由於全部iptables命令都是保存在內存中的,重啓計算機就會失效
[root@VM_0_7_centos ~]# cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak [root@VM_0_7_centos ~]# /etc/init.d/iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] 成功保存iptables規則
維護iptables防火牆
[root@VM_0_7_centos ~]# vim /etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Fri Mar 23 11:13:52 2018 *filter :INPUT ACCEPT [938:61166] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [872:82030] -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT COMMIT # Completed on Fri Mar 23 11:13:52 2018