CentOS 上 iptables 防火牆的基本應用

來源:互聯網
上載者:User


iptables是Linux上常用的防火牆軟體之一,在安裝LNMP後。只要重啟機器後,會發現Web網站打不開,這樣很大程度是因為 iptable引起的。下面為大家列一些常的設定。

安裝 iptables 防火牆

如果沒有安裝iptables的話需要先安裝,在CentOS下執行:

yum install iptables

清除已經有的防火牆規則

iptables -F
iptables -X
iptables -Z

開放指定連接埠

#允許本地迴環介面(即運行本機訪問本機)
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
# 允許已建立的或相關連的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允許所有本機向外的訪問
iptables -A OUTPUT -j ACCEPT
# 允許訪問22連接埠
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#允許訪問80連接埠
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#允許FTP服務的21和20連接埠
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT

#如果有其他連接埠的話,規則也類似,稍微修改上述語句就行
#禁止其他未允許的規則訪問

iptables -A INPUT -j REJECT  (注意:如果22連接埠未加入允許規則,SSH連結會直接斷開。)
iptables -A FORWARD -j REJECT

查看已添加的規則

iptables -L -n

v:顯示詳細資料,包括每條規則的匹配包數量和匹配位元組數
x:在 v 的基礎上,禁止自動單位換算(K、M)
n:只顯示IP地址和連接埠號碼,不將ip解析為網域名稱

iptables 的開機啟動及規則儲存

CentOS上可能會存在安裝好iptables後,iptables並不開機自啟動,可以執行一下:

chkconfig --level 345 iptables on

將其加入開機啟動

其它配置

重啟後生效

chkconfig iptables on   # 開啟
chkconfig iptables off  # 關閉
即時生效

service iptables start # 開啟
service iptables stop  # 關閉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.