學習如何在Linux中使用Iptables管理系統防火牆
Iptables 和 ip6tables 用於在 Linux 核心中建立、維護和檢查 IPv4 和 IPv6 資料包過濾規則的表。可以定義多個不同的表。每個表包含一定數量的內建鏈,也可以包含使用者定義的鏈。讓我們來了解一下如何在 Linux 中使用 Iptables 管理系統防火牆。
安裝 IP tables
要安裝 IP tables,請使用以下命令:
$sudo apt-get install iptables
示例輸出應如下所示:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libecap3 squid-common squid-langpack Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: iptables 0 upgraded, 1 newly installed, 0 to remove and 261 not upgraded. Need to get 266 kB of archives. After this operation, 1,663 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 iptables amd64 1.6.0-2ubuntu3 [266 kB] ...........................................................................
IP tables 幫助
要獲取有關 IP tables 的更多選項,請使用以下命令:
$ iptables --help
示例輸出應如下所示:
iptables v1.6.0 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 chainAppend to chain --check -C chainCheck for the existence of a rule --delete -D chainDelete 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]] ..............................................................................
顯示防火牆狀態
要顯示防火牆狀態,請使用以下命令:
$ sudo iptables -L -n -v
示例輸出應如下所示:
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 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
啟動防火牆
要啟動防火牆,請使用以下命令:
$ sudo service iptables start
停止防火牆
要停止防火牆,請使用以下命令:
$ sudo service iptables stop
重啟防火牆
要重啟防火牆,請使用以下命令:
$sudo service iptables restart
阻止所有連線
要阻止來自某個 IP 地址的所有連線,請使用以下命令:
$ sudo iptables -A INPUT -s 100.100.100.100 -j DROP
在上面的命令中,100.100.100.100 是 IP 地址示例。
阻止特定網站
要阻止特定網站,請使用以下命令:
$ sudo iptables -A OUTPUT -p tcp -d 100.100.100.100/20 -j DROP
在上面的命令中,100.100.100.100/20 是特定 IP 地址的埠。
示例
例如,要阻止名為 www.orkut.com 的網站,請使用以下命令列:
$ sudo whois 104.198.199.158
在上面的命令中,104.198.199.158 表示 Orkut.com 的 IP 地址,結果應如下所示:
...........................
NetRange: 104.196.0.0 - 104.199.255.255
CIDR: 104.196.0.0/14
NetName: GOOGLE-CLOUD
NetHandle: NET-104-196-0-0-1
Parent: NET104 (NET-104-0-0-0-0)
NetType: Direct Allocation
OriginAS: AS15169
Organization: Google Inc. (GOOGL-2)
RegDate: 2014-08-27
Updated: 2015-09-21
.................................................
複製 CIDR 並使用以下命令阻止 orkut.com,如下所示:
$sudo iptables -A OUTPUT -p tcp -d 104.196.0.0/14 -j DROP
儲存規則
要儲存上述規則,請使用以下命令:
$ sudo /sbin/iptables-save
示例輸出應如下所示:
# Generated by iptables-save v1.6.0 on Tue Jan 24 11:14:39 2017 *filter :INPUT ACCEPT [258:45283] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [233:43953] -A INPUT -s 10.10.10.10/32 -j DROP -A OUTPUT -d 104.196.0.0/14 -p tcp -j DROP -A OUTPUT -d 104.196.0.0/14 -p tcp -j DROP COMMIT # Completed on Tue Jan 24 11:14:39 2017
阻止傳入 ping 請求
要阻止傳入 ping 請求,請使用以下命令:
$ sudo iptables -A INPUT -p icmp -i eth0 -j DROP
丟棄未使用的資料包
要在 IP tables 中丟棄未使用的資料包,請使用以下命令:
$sudo iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
阻止公共網路上的 IP 欺騙
要記錄和阻止公共網路上的 IP 欺騙,請使用以下命令:
$sudo iptables -A INPUT -i eth1 -s 100.100.100.100/24 -j LOG --log-prefix "IP_SPOOF A:" $sudo iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
接受來自 MAC 地址的流量
要接受來自 MAC 地址的流量,請使用以下命令:
$sudo iptables -A INPUT -p tcp --destination-port 22 -m mac --mac-source 00:0F:EA:91:04:07 -j ACCEPT
阻止來自 MAC 地址的流量
要阻止來自 MAC 地址的流量,請使用以下命令:
$sudo iptables -A INPUT -m mac --mac-source 00:0F:EA:91:04:08 -j DROP
接受開放的埠範圍
要接受開放的埠範圍,請使用以下命令:
$sudo iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 7000:7010 -j ACCEPT
將 HTTP 請求設定到 iptables
要將 HTTP 請求設定到 iptables,請使用以下命令:
$ sudo iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 50 --connlimit-mask 24 -j DROP
在上面的命令中,我們給出了 50 個請求許可權。
設定每個客戶端主機的 SSH 連線數
要設定每個客戶端主機的 SSH 連線數,請使用以下命令:
$sudo iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 10 -j REJECT
在上面的命令中,我們為每個客戶端主機設定了 10 個 SSH 連線。
重新整理 IP 表規則
要重新整理所有 IP 表規則,請使用以下命令:
$ sudo iptables --flush
在本文中,我們學習瞭如何在 Linux 中使用 iptables 管理系統防火牆。在我們的下一篇文章中,我們將介紹更多基於 Linux 的技巧和提示。敬請關注。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP