SYNOPSIS iptables [-t table] {-A|-C|-D} chain rule-specification ip6tables [-t table] {-A|-C|-D} chain rule-specification iptables [-t table] -I chain [rulenum] rule-specification iptables [-t table] -R chain rulenum rule-specification iptables [-t table] -D chain rulenumiptables 是linux下一款強大的防火牆,在不考慮效率的情況下,功能強大到足可以替代大多數硬體防火牆,但是強大的防火牆如果應用不當,可能擋住的可不光是那些潛在的攻擊,還有可能是你自己哦。這個帶來的危害對於普通的個人PC來說可能無關緊要,但是想象一下,如果這是一台伺服器,一旦發生這樣的情況,不光是影院正常的服務,還需要到現場去恢複,這會給你帶來多少損失呢?
所以我想說的是,當你敲入每一個iptables 相關命令的時候都要萬分小心。
1.應用每一個規則到DROP target時,都要仔細檢查規則,應用之前要考慮他給你帶來的影響。
2.在redhat中我們可以使用service iptables stop來關閉防火牆,但是在有些版本如ubuntu中這個命令卻不起作用,大家可能在網上搜尋到不少文章告訴你用iptables -F這個命令來關閉防火牆,但是使用這個命令前,千萬記得用iptables -L查看一下你的系統中所有鏈的預設target,iptables -F這個命令只是清除所有規則,只不會真正關閉iptables.想象一下,如果你的鏈預設target是DROP,本來你有規則來允許一些特定的連接埠,但一旦應用iptables -L ,清除了所有規則以後,預設的target就會阻止任何訪問,當然包括遠程ssh管理伺服器的你。
以我建議的關閉防火牆命令是
iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -F
總之,當你要在你的伺服器上做任何變更時,最好有一個測試環境做過充分的測試再應用到你的伺服器。除此之外,要用好iptables,那就要理解iptables的運行原理,知道對於每一個資料包iptables是怎麼樣來處理的。這樣才能準確地書寫規則,避免帶來不必要的麻煩。
iptables [-t table] -S [chain [rulenum]] iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...] iptables [-t table] -N chain iptables [-t table] -X [chain] iptables [-t table] -P chain target iptables [-t table] -E old-chain-name new-chain-name rule-specification = [matches...] [target] match = -m matchname [per-match-options] target = -j targetname [per-target-options]
屏蔽ICMP ping請求
我們可以通過允許下面的命令屏蔽ping請求:
- # iptables -A INPUT -p icmp --icmp-type echo-request -j DROP # iptables -A INPUT -i eth1 -p icmp --icmp-type echo-request -j DROP
也可以按照特定的網段和主機限制ping請求:
- # iptables -A INPUT -s 192.168.1.0/24 -p icmp --icmp-type echo-request -j ACCEPT
以下命令只接受受限制的ping請求:
- #假定預設INPUT策略為丟棄資料包 # iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
- # iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT # iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
- #所有的伺服器都對ping請求作出應答 # iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
屏蔽或開啟常見連接埠
屏蔽或開啟常用的TCP、UDP連接埠:
- #可以使用DROP替換ACCEPT,實現連接埠屏蔽。 #開啟22連接埠(SSH)
- # iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT
- #開啟TCP/UDP631連接埠(列印服務) # iptables -A INPUT -s 192.168.1.0/24 -p udp -m udp --dport 631 -j ACCEPT
- # iptables -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 631 -j ACCEPT # 開啟123連接埠,允許區域網路使用者進行NTP時間同步
- # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p udp --dport 123 -j ACCEPT #開啟25連接埠(SMTP)
- # iptables -A INPUT -m state --state NEW -p tcp --dport 25 -j ACCEPT # 開啟DNS連接埠
- # iptables -A INPUT -m state --state NEW -p udp --dport 53 -j ACCEPT # iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
- #開啟http/https連接埠 # iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
- # iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT #開啟TCP110連接埠(POP3)
- # iptables -A INPUT -m state --state NEW -p tcp --dport 110 -j ACCEPT #開啟TCP143連接埠
- # iptables -A INPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT #為區域網路使用者開啟Samba訪問
- # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT
- # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT
- #為區域網路使用者開啟Proxy 伺服器訪問 # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 3128 -j ACCEPT
- #為區域網路使用者開啟MySQL訪問 # iptables -I INPUT -p tcp --dport 3306 -j ACCEPT