Ubuntu 14.04 配置iptables防火牆
----------------------- 配置防火牆,開啟80端口、3306端口 ---- start -----------------------
# 說明:Ubuntu默認安裝是沒有開啟任何防火牆的,為了服務器的安全,建議大家安裝啟用防火牆設置,這里推薦使用iptables防火牆.如果mysql本地使用,可以不用打開3306端口.
whereis iptables #查看系統是否安裝防火牆
# 顯示:iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz
# 表示已經安裝 iptables
# apt-get install iptables #如果默認沒有安裝,請運行此命令安裝防火牆
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
######################################
可以看到Linux中都有的3個常用默認鏈(INPUT、OUTPUT和FORWARD),同時也可以看到每個鏈的缺省策略(每個鏈對默認策略都是接受),在此我們可以看到Ubuntu中並沒有添加任何默認規則集。
# 1. 一鍵批處理設置
vi lanmps_iptables.sh 保存一下內容(如果還有其他端口規則請一起在上面配置,執行時清空規則):
##################################################################################################################
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin:~/bin
export PATH
# Check if user is root
if [ $UID != 0 ]; then echo "Error: You must be root to run the install script, please use root to install lanmps";exit;fi
iptables-save >> _.iptables.up.rules #保存防火牆設置,以便沒保存時使用
iptables -L -n 2>&1 | tee -a "_.iptables.log"
iptables -F #清除預設表filter中的所有規則鏈的規則
iptables -X #清除預設表filter中使用者自定鏈中的規則
iptables -Z #計數器清零
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
#雙向
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允許本機
iptables -A INPUT -i lo -j ACCEPT
#FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#www 80
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#13306 映射轉發到 mysql數據庫 3306
iptables -A PREROUTING -p tcp --dport 13306 -j REDIRECT --to-ports 3306 -t nat
#3306 mysql數據庫
#iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
#memache
#iptables -A INPUT -p tcp --dport 11211 -j ACCEPT
#對於OUTPUT規則,因為預設的是ACCEPT,所以要添加DROP規則,減少不安全的端口鏈接。
iptables -A OUTPUT -p tcp --sport 31337 -j DROP
iptables -A OUTPUT -p tcp --dport 31337 -j DROP
#丟棄壞的TCP包
iptables -A FORWARD -p tcp ! --syn -m state --state NEW -j DROP
#處理IP碎片數量,防止攻擊,允許每秒100個
#iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT
#設置ICMP包過濾,允許每秒1個包,限制觸發條件是10個包
#iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
#防止外部的ping和SYN洪水攻擊
iptables -A INPUT -p tcp --syn -m limit --limit 100/s --limit-burst 100 -j ACCEPT
#ping洪水攻擊,限制每秒的ping包不超過10個
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s –limit-burst 10 -j ACCEPT
#防止各種端口掃描,將SYN及ACK SYN限制為每秒鍾不超過200個
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
#最后規則拒絕所有不符合以上所有的
iptables -A INPUT -j DROP
if [ -z "`grep "iptables-save" /etc/network/interfaces`" ]
then
echo "#以下有防火牆需要的可以使用
pre-up iptables-restore < /etc/iptables.up.rules #啟動時應用防火牆
post-down iptables-save > /etc/iptables.up.rules #關閉時保存防火牆設置,以便下次啟動時使用 " >> /etc/network/interfaces
else
echo "iptables-save find "
fi
clear
echo "iptables ok ";
echo ""
iptables -L -n
cat /etc/network/interfaces
##################################################################################################################
chmod 777 lanmps_iptables.sh
./lanmps_iptables.sh #那么 防火牆就設置完成了
# 2. ubuntu iptables 防火牆 啟動
modprobe ip_tables
# 3. ubuntu iptables 防火牆 關閉
# ubuntu 並沒有關閉命令,所以要通過變通方法解決防火牆
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
modprobe -r ip_tables
依次執行以上命令即可關閉iptables,否則在執行modproble -r ip_tables時將會提示 FATAL: Module ip_tables is in use.
# 4. Iptables的保存和調用
防止每次開機或重啟后都需要去調用一次,把它設置自動執行
第一步 更改網卡配置文件
sudo vi /etc/network/interfaces
第二部 在最后增加配置
#以下有防火牆需要的可以使用
pre-up iptables-restore < /etc/iptables.up.rules #啟動時應用防火牆
post-down iptables-save > /etc/iptables.up.rules #關閉時保存防火牆設置,以便下次啟動時使用
----------------------- 配置防火牆,開啟80端口、3306端口 ---- end -----------------------