Centos7 防火牆常用配置及說明


一、 Centos7和Centos6 防火牆的區別:

    使用的工具不一樣了。Centos6 使用的是iptables ,Centos7 使用的是filewall

    iptables 用於過濾數據包,屬於網絡層防火牆。

    firewall 能夠允許哪些服務可用,那些端口可用...屬於更高一層的防火牆。

 

二、常用命令:

    vi /usr/lib/firewalld/services/ssh.xml

    vi /usr/lib/firewalld/services/html.xml

    systemctl enable firewalld.service

    systemctl restart firewalld.service

    firewall-cmd --state

    firewall-cmd --list-all

    firewall-cmd --zone=public --permanent --add-port=8502/tcp           

    vi /etc/firewalld/zones/public.xml

    <port protocol="tcp" port="8502"/>

    systemctl restart firewalld.service

 

三、firewall 配置

    The configuration for firewalld is stored in various XML files

    in /usr/lib/firewalld and /etc/firewalld

注意:以下firewalld 的操作只有重啟之后才有效:service firewalld restart 

 

1、系統配置目錄

        /usr/lib/firewalld/services

    目錄中存放定義好的網絡服務和端口參數,系統參數,不能修改。

2、用戶配置目錄

        /etc/firewalld/

3、如何自定義添加端口

    用戶可以通過修改配置文件的方式添加端口,也可以通過命令的方式添加端口,注意:修改的內容會在/etc/firewalld/目錄下的配置文件中體現。

    3.1 、命令的方式添加端口:

        firwall-cmd --permanent --add-port=9527/tcp

 

參數介紹:

1、firwall-cmd:是Linux提供的操作firewall的一個工具;2、--permanent:表示設置為持久;3、--add-port:標識添加的端口;

另外,firewall中有Zone的概念,可以將具體的端口制定到具體的zone配置文件中。

例如:添加8010端口

firewall-cmd --zone=public --permanent --add-port=8010/tcp

--zone=public:指定的zone為public;

如果–zone=dmz 這樣設置的話,會在dmz.xml文件中新增一條。

 

l  3.2、修改配置文件的方式添加端口

<?xml version="1.0" encoding="utf-8"?>
<zone>   
<short>Public</short>   
<description>For use in public areas.</description> 
<rule family="ipv4">    
  <source address="122.10.70.234"/>     
  <port protocol="udp" port="514"/>     
  <accept/>   
</rule> <rule family="ipv4">   <source address="123.60.255.14"/>   <port protocol="tcp" port="10050-10051"/>   <accept/> </rule> <rule family="ipv4">   <source address="192.249.87.114"/> 放通指定ip,指定端口、協議   <port protocol="tcp" port="80"/>   <accept/> </rule> <rule family="ipv4"> 放通任意ip訪問服務器的9527端口   <port protocol="tcp" port="9527"/>   <accept/> </rule> </zone>

上述的一個配置文件可以很好的看出:

1、添加需要的規則,開放通源ip為122.10.70.234,端口514,協議tcp; 2、開放通源ip為123.60.255.14,端口10050-10051,協議tcp;/3、開放通源ip為任意,端口9527,協議tcp;

 

四、firewall常用命令

    1、重啟、關閉、開啟、firewalld.serverice 服務

        Service firewalld restart 重啟

        Service firewalld start  開啟

        Service firewalld stop  關閉

        systemctl status firewalld

        systemctl stop firewalld  關閉

        systemctl start firewalld 開啟

        systemctl  restart firewalld 重啟

        systemctl  disable firewalld  關閉開機啟動

    2、查看狀態

        firewall-cmd --state

    3、查看防火牆規則

        firewall-cmd --list-all

 

 

五、Centos 切換為iptables防火牆

切換到iptables首先應該關掉默認的firewalld,然后安裝iptables服務。

1、關閉firewall:

service firewalld stop systemctl disable firewalld.service #禁止firewall開機啟動

2、安裝iptables防火牆

yum install iptables-services #安裝

3、編輯iptables防火牆配置

vi /etc/sysconfig/iptables #編輯防火牆配置文件

下邊是一個完整的配置文件:

Firewall configuration written by system-config-firewall  Manual customization of this file is not recommended.  
*filter  :INPUT ACCEPT [0:0]  :FORWARD ACCEPT [0:0]  :OUTPUT ACCEPT [0:0]  
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
-A INPUT -p icmp -j ACCEPT  -A INPUT -i lo -j ACCEPT  
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT  
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  
-A INPUT -j REJECT --reject-with icmp-host-prohibited  
-A FORWARD -j REJECT --reject-with icmp-host-prohibited  
COMMIT

  

:wq! #保存退出

service iptables start #開啟systemctl enable iptables.service #設置防火牆開機啟動

 


免責聲明!

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



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