CentOS 6 - iptables 阻止透過連接埠 80 進行 Web 訪問

CentOS 6 - iptables 阻止透過連接埠 80 進行 Web 訪問

我正在使用 CentOS 6.2 設定新的 Web 伺服器,但無法透過網路進行連線。一切看起來都設定正確httpd.conf且 Apache 正在運行,所以我假設這是一個 iptables 問題。

以下內容是否存在可能導致該問題的原因?

編輯:如果我停止 iptables,我可以正常連接,所以下面肯定需要調整一些東西。我已經運行iptables -A INPUT -p tcp --dport 80 -j ACCEPT並保存並重新啟動 iptables 但沒有什麼區別

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

遵循以下答案中的建議:

[root@staging ~]# iptables -N TCP
[root@staging ~]# iptables -A TCP -p tcp --dport 80 -j ACCEPT
[root@staging ~]# iptables-save > /etc/iptables/iptables.rules
-bash: /etc/iptables/iptables.rules: No such file or directory
[root@staging ~]# iptables-save
# Generated by iptables-save v1.4.7 on Thu Nov  8 14:09:09 2012
*filter
:INPUT ACCEPT [91:7480]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [70:6556]
:TCP - [0:0]
-A TCP -p tcp -m tcp --dport 80 -j ACCEPT
COMMIT
# Completed on Thu Nov  8 14:09:09 2012
[root@staging ~]# iptables-restore
^C
[root@staging ~]# service iptables start
iptables: Applying firewall rules:                         [  OK  ]

進一步編輯: iptables-save 在我停止 iptables 後運行它時沒有顯示任何內容!所以這是輸出:

# iptables-save
# Generated by iptables-save v1.4.7 on Thu Nov  8 14:39:10 2012
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [28:3344]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Thu Nov  8 14:39:10 2012

答案1

輸出iptables-save顯示了規則 3 中未顯示的附加資訊iptables -L

iptables -L輸出來看,人們會認為全部接受流量:

ACCEPT     all  --  anywhere             anywhere

iptables-save顯示:

-A INPUT -i lo -j ACCEPT

這意味著iptables確實接受所有流量...但僅來自環回介面 ( lo)。

這就是 HTTP 流量永遠不會到達您的 Web 伺服器的原因:唯一允許的流量是規則 1 中建立的連線、ping規則 2: 中的 ICMP(例如 )-A INPUT -p icmp -j ACCEPT和規則 4: 中的 SSH -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

那麼一切都在規則 5​​ 中被拒絕:-A INPUT -j REJECT --reject-with icmp-host-prohibited

也就是說,所有 HTTP 流量在到達規則 6 之前都會被拒絕:-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

修正它刪除規則 6 ( -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT):

iptables -D INPUT 6

並將其插入(選項-I)作為規則 5:

iptables -I INPUT 5 -p tcp -m tcp --dport 80 -j ACCEPT

或導入這個:

# Generated by iptables-save v1.4.6 on Thu Nov  8 16:46:28 2012
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [40:5423]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Thu Nov  8 16:46:28 2012

透過將其保存到文件中並執行iptables-restore < file.

編輯:OP注意到新規則在重新啟動後遺失iptables

正如這裡所解釋的:http://wiki.centos.org/HowTos/Network/IPTables,任何時候我們更新規則時都需要保存它們,因此執行以下命令:

# /sbin/service iptables save

使改變永久化。現在修改後的規則將會載入iptables

答案2

我不是 iptables 大師,但我的設定方式是使用單獨的自訂鏈來開啟連接埠:

iptables -N TCP
iptables -A TCP -p tcp --dport 80 -j ACCEPT

確保保存

iptables-save > /etc/iptables/iptables.rules

載入

iptables-restore < /etc/iptables/iptables.rules

^^ 如果守護程式沒有為您載入它。我看到你說你已經運行了上面描述的方法(只是對於我認為不重要的輸入鏈),所以這讓我相信你可能沒有正確保存和重新加載。你的 iptables 守護程式運作正常嗎?確保您執行上述命令並使用 iptables -L 後您可以看到新新增的鍊和規則。


回應您的進一步麻煩。 iptables-save > location_of_rules 我不確定它在 CentOS 上的位置。但使用 iptables -L 檢查新鍊是否存在

我還建議檢查以確保您的網站無法訪問。要執行此操作,請轉到您的網絡瀏覽器(在電腦上)並輸入網址http://127.0.0.1/ <- : 和// 之間沒有空格如果網站可以訪問,則問題出在您的路由器上。您需要進行連接埠轉送才能在防火牆中開啟連接埠。http://en.wikipedia.org/wiki/Port_forwarding

相關內容