顯示廣告
隱藏 ✕
看板 Knuckles_note
作者 Knuckles (站長 那克斯')
標題 [CentOS] iptables 防火牆設定
時間 2012年12月10日 Mon. PM 04:53:39


參考:
 


新增 iptables 目錄與設定檔
$ sudo mkdir /etc/iptables
$ sudo vim /etc/iptables/iptables.rule


CentOS 7 改成使用 firewalld 了,要用 iptables 要先關閉 firewalld,
然後安裝 iptables-service
$ sudo systemctl disable firewalld
$ sudo yum install iptables-services
$ sudo systemctl start iptables


===========以下是 iptables.rule 的內容================
#!/bin/bash

# 請先輸入您的相關參數,不要輸入錯誤了!
  EXTIF="eth0"  # 這個是可以連上 Public IP 的網路介面
  INIF=""       # 內部 LAN 的連接介面;若無請填 ""
  INNET=""      # 內部 LAN 的網域,若沒有內部 LAN 請設定為 ""
  export EXTIF INIF INNET

# 第一部份,針對本機的防火牆設定!###########################
# 1. 先設定好核心的網路功能:
  echo "1" > /proc/sys/net/ipv4/tcp_syncookies
  echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}; do
        echo "1" > $i
  done
  for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,send_redirects}; do
        echo "0" > $i
  done

# 2. 清除規則、設定預設政策及開放 lo 與相關的設定值
  PATH=/sbin:/usr/sbin:/bin:/usr/bin; export PATH
  iptables -F
  iptables -X
  iptables -Z
  iptables -P INPUT   DROP
  iptables -P OUTPUT  ACCEPT
  iptables -P FORWARD ACCEPT
  iptables -A INPUT -i lo -j ACCEPT
  iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# 3. 例外的主機清單
  # 一律允許進入的主機位址
  #iptables -A INPUT -i $EXTIF -s 192.168.0.1 -j ACCEPT

  # 一律阻擋進入的主機位址
  #iptables -A INPUT -i $EXTIF -s xxx.xxx.xxx.xxx -j DROP

# 4. 允許某些類型的 ICMP 封包進入
  AICMP="0 3 3/4 4 8 11 12 14 16 18"
  for tyicmp in $AICMP
  do
     iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
  done

# 5. 允許某些服務的進入,請依照您自己的環境開啟
iptables -A INPUT -p TCP -i $EXTIF --dport  22  -j ACCEPT   # SSH
#iptables -A INPUT -p TCP -i $EXTIF --dport  21  -j ACCEPT   # FTP
#iptables -A INPUT -p TCP -i $EXTIF --dport  25  -j ACCEPT   # SMTP
# iptables -A INPUT -p UDP -i $EXTIF --sport  53  -j ACCEPT   # DNS
# iptables -A INPUT -p TCP -i $EXTIF --sport  53  -j ACCEPT   # DNS
iptables -A INPUT -p TCP -i $EXTIF --dport  80  -j ACCEPT   # HTTP
#iptables -A INPUT -p TCP -i $EXTIF --dport 443  -j ACCEPT   # HTTPS
# iptables -A INPUT -p TCP -i $EXTIF --dport 110  -j ACCEPT   # POP3
#iptables -A INPUT -p TCP -i $EXTIF --dport 3128 -j ACCEPT # proxy
#iptables -A INPUT -p TCP -i $EXTIF --dport 1723 -j ACCEPT # vpn
#iptables -A INPUT -p gre -i $EXTIF -j ACCEPT
#iptables -A INPUT -p TCP -i $EXTIF --dport  3306  -j ACCEPT   # MySQL

# 第二部份,針對後端主機的防火牆設定!##############################
# 1. 先載入一些有用的模組 (好像都不能用...先註解掉)
#  modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc"
  for mod in $modules
  do
      testmod=lsmod | grep "^${mod} " | awk '{print $1}'
      if [ "$testmod" == "" ]; then
            modprobe $mod
      fi
  done

# 2. 清除 NAT table 的規則吧!
  iptables -F -t nat
  iptables -X -t nat
  iptables -Z -t nat
  iptables -t nat -P PREROUTING  ACCEPT
  iptables -t nat -P POSTROUTING ACCEPT
  iptables -t nat -P OUTPUT      ACCEPT

# 3. 若有雙網卡開放成為路由器,且為 IP 分享器
  if [ "$INIF" != "" ]; then
    iptables -A INPUT -i $INIF -j ACCEPT
    echo "1" > /proc/sys/net/ipv4/ip_forward
    if [ "$INNET" != "" ]; then
      for innet in $INNET
      do
        iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE
      done
    fi
  fi
  # 如果你的 MSN 一直無法連線,或者是某些網站 OK 某些網站不 OK,
  # 可能是 MTU 的問題,那你可以將底下這一行給他取消註解來啟動 MTU 限制範圍
  # iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \
  #          --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu


# 4. NAT 伺服器後端的 LAN 內對外之伺服器設定
# iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80  \
#          -j DNAT --to 192.168.1.210:80

# 有 VPN 要設定這個
#iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
#iptables -A FORWARD -s 192.168.0.0/24 -p tcp -m tcp --tcp-flags SYN,RST SYN \
#         -j TCPMSS --set-mss 1200


# 5. 特殊的功能,包括 Windows 遠端桌面所產生的規則,假設桌面主機為 1.2.3.4
# iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4  --dport 6000 \
#          -j DNAT --to-destination 192.168.100.10
# iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4  --sport 3389 \
#          -j DNAT --to-destination 192.168.100.20


# 將阻檔封包的訊息記錄在 /var/log/message
iptables -N LOGGING
iptables -A INPUT -j LOGGING
iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
iptables -A LOGGING -j DROP



# 6. 將設定存在 /etc/sysconfig/iptables
  service iptables save

# 列出設定結果
# iptables -L -v

echo 'iptables has changed'
============================================

# 執行讓設定生效
$ sudo sh /etc/iptables/iptables.rule


(CentOS 6)
# 開機自動啟動iptables
$ sudo chkconfig iptables on
# 重啟 iptables
$ sudo service iptables restart

(CentOS 7)
# 開機自動啟動iptables
$ sudo systemctl enable iptables
# 重啟 iptables
$ sudo systemctl restart iptables


□ 問題解決記錄

如果有設定 /etc/hosts 的話
上面的 IP 位址也可以改用 hostname

不過如果使用 hostname 卻沒有在 /etc/hosts 設定的話
執行 iptables 時會卡住




--
※ 作者: Knuckles 時間: 2012-12-10 16:53:39
※ 編輯: Knuckles 時間: 2017-02-24 23:29:44
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 2438 
※ 文章分類: Linode CentOS 架站筆記
分享網址: 複製 已複製
1樓 時間: 2012-12-18 20:49:18 (台灣)
  12-18 20:49 TW
SSH使用預設的22 port會有奇怪的軟體三天兩頭來掃....
2樓 時間: 2012-12-18 20:50:02 (台灣)
  12-18 20:50 TW
我把它改成五位數的port....清靜許多
有道理 我也來改個port好了
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇