linux中IPTABLES常用的例子

來源:互聯網
上載者:User

1.安裝iptables

很多Linux已經預設安裝iptables,可使用後文的查看命令測試是否安裝
CentOS/RedHat下執行:

yum install iptablesDebian/Ubuntu下執行:

apt-get install iptables

iptables –F

例子

#刪除已經存在的規則

iptables -P INPUT DROP
#配置預設的拒絕規則。基本規則是:先拒絕所有的服務,然後根據需要再添加新的規則。

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#開啟WEB服務連接埠的tcp協議

iptables -A INPUT -p tcp --dport 110 -j ACCEPT
#開啟POP3服務連接埠的tcp協議

iptables -A INPUT -p tcp --dport 25 -j ACCEPT
#開啟SMTP服務連接埠的tcp協議

iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#開啟FTP服務連接埠的tcp協議

iptables -A INPUT -p tcp -s 202.106.12.130 --dport 22 -j ACCEPT
#允許IP地址為202.106.12.130這台主機串連本地的SSH服務連接埠

iptables -A INPUT -p tcp --dport 53 -j ACCEPT
#允許DNS服務連接埠的tcp資料包流入

iptables -A INPUT -p udp --dport 53 -j ACCEPT
#允許DNS服務連接埠的udp資料包流入

iptables -A INPUT -p icmp -icmp-type echo-request -i eth1 -j DROP
#防止死亡之ping,從介面eth1進入的icmp協議的請求全部丟棄。

iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT
#防止SYN Flood (拒絕服務的攻擊)

iptables -t nat -A POSTROUTING -o eth1 -s 192.168.0.226 -j MASQUERADE
#允許 192.168.0.226通過eth1 IP偽裝出外網

iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.4 -p tcp --dport 25 -j MASQUERADE
#允許 192.168.0.4通過eth0 偽裝訪問外網的 25連接埠

6.設定開機啟動

一般在安裝iptables完成後,開機啟動會自動化佈建成功,但在個別CentOS系統上,貌似還有些問題,可以使用如下命令手動設定
chkconfig --level 345 iptables on

7.儲存iptables規則

service iptables save

8.iptables在手動防CC攻擊中的簡單應用

關於擷取攻擊者ip的方法,可以通過很多方法擷取,如查看網站日誌等,本文不再贅述。
a).建立要屏蔽的ip/ip段檔案,名為ip.txt

#屏蔽的ip
123.4.5.6
#屏蔽的ip段(編寫方法,同前文)
123.4.5.6/24b).建立block_ip.sh指令檔

複製代碼代碼如下:

#!/bin/sh
# Filename: block_ip.sh
# Purpose: blocks all IP address/network found in a text file
# The text file must have one IP address or network per line
#################################################################
# Change the following path/filename to match yours
IP_LIST_FILE=/path/to/ip.txt
#################################################################
# Don't change anything below unless you are a smarty pant!
#################################################################
IPTABLES_BIN=/sbin/iptables
# Get the IP address/network from the file and ignore any line starting with # (comments)
BAD_IP_ADDR_LIST=$(grep -Ev "^#" $IP_LIST_FILE)
# Now loop through the IP address/network list and ban them using iptabels
for i in $BAD_IP_ADDR_LIST
do
echo -n "Blocking $i ...";
$IPTABLES_BIN -A INPUT -s $i -j DROP
$IPTABLES_BIN -A OUTPUT -d $i -j DROP
echo "DONE.";
done
##################################################################
# END OF SCRIPT - NOTHING TO SEE HERE - THAT'S ALL FOLKS!
##################################################################

c).運行指令碼

sh /path/to/block_ip.sh

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.