CentOS7中關閉firewall,並使用iptables管理防火牆


背景描述

在使用Docker時,啟用centos7默認的firewall,啟動端口映射時,防火牆規則不生效。docker默認使用了iptables防火牆機制。所以需要關閉firewall使用iptables解決。

 

1.關閉默認的firewall防火牆

systemctl stop firewalld.service 關閉防火牆
systemctl disable firewalld.service 關閉開機啟動

  

2.開啟iptables

yum install iptables (根據centOS7的版本和內核,有些版本已經裝過,可以跳過此命令)
yum install iptables-services
service iptables restart
chkconfig iptables on或者systemctl enable iptables.service開機自啟

  

3.編輯防火牆文件(開啟了21,22,80,3306端口)

vim /etc/sysconfig/iptables

# sampleconfiguration for iptables service  
# # you can edit thismanually or use system-config-firewall  
# # please do not askus to add additional ports/services to this default configuration  
*filter  
:INPUT ACCEPT [0:0]  
:FORWARD ACCEPT [0:0]  
:OUTPUT ACCEPT [0:0]  
-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 21 -j ACCEPT  
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT  
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT  
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT  
-A INPUT -j REJECT --reject-with icmp-host-prohibited  
-A FORWARD -j REJECT --reject-with icmp-host-prohibited  
COMMIT  

  

4.添加防火牆命令

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

 

5.查看本機關於IPTABLES的設置情況

iptables -L -n

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM