0

我在端口8080上的Google VM實例中的CentOS7上運行Tomcat8。 我設置iptables規則以將所有外部連接映射到端口80至8080無法在Google Cloud VM實例上保存iptables規則(CentOS 7)

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 

從那以後,我保存

service iptables save 

的Tomcat通過端口80 規則被保存在/etc/sysconfig/iptables工作正常,並從外部接近的規則。

... 
-A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 
... 

但服務器重新引導後,規則不適用。 它仍然在文件/etc/sysconfig/iptables但不存在動作,當我運行

iptables-save 

看來,iptables規則是從別的地方恢復。

如何在重新啓動後正確保留規則以保留規則?

回答

2

爲了解決這個問題使用iptables,你可以做到以下幾點:

yum install iptables-services 
systemctl mask firewalld 
systemctl enable iptables 
systemctl enable ip6tables 
systemctl stop firewalld 
systemctl start iptables 
systemctl start ip6tables 

然而,Centos7使用FirewallD現在來代替。爲了應用的防火牆,你需要先檢查什麼是可用區域和區域是活躍在FirewallD通過運行以下命令:

firewall-cmd --list-all-zones 
firewall-cmd --get-active-zones 

如果公共區域是活躍的,例如,您可以運行這些命令使能(在你的案件80端口爲8080)端口轉發:

firewall-cmd --zone=public --add-masquerade --permanent 
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent 

一旦這樣做,你可以重新加載的規則,以確保一切正常,運行以下命令:

firewall-cmd --reload 

您可以檢查man firewall-cmd獲取更多信息。

+0

謝謝喬治,它現在有效! – 4ilin

+0

@ 4ilin如果我的答案有助於解決您的問題,我將不勝感激,如果您可以接受答案,以便它可以幫助其他人,如果他們遇到同樣的問題。謝謝 – George