Centos7 防火牆經常使用配置及說明

1、 Centos7和Centos6 防火牆的區別:html

    使用的工具不同了。Centos6 使用的是iptables ,Centos7 使用的是filewallbash

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

    firewall 可以容許哪些服務可用,那些端口可用...屬於更高一層的防火牆。網絡

 

2、經常使用命令:ssh

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

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

    systemctl enable firewalld.servicethis

    systemctl restart firewalld.servicerest

    firewall-cmd --statecode

    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

 

3、firewall 配置

    The configuration for firewalld is stored in various XML files

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

注意:如下firewalld 的操做只有重啓以後纔有效:service firewalld restart 

 

一、系統配置目錄

        /usr/lib/firewalld/services

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

二、用戶配置目錄

        /etc/firewalld/

三、如何自定義添加端口

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

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

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

 

參數介紹:

一、firwall-cmd:是Linux提供的操做firewall的一個工具;二、--permanent:表示設置爲持久;三、--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.二、修改配置文件的方式添加端口

<?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>

上述的一個配置文件能夠很好的看出:

一、添加須要的規則,開放通源ip爲122.10.70.234,端口514,協議tcp; 二、開放通源ip爲123.60.255.14,端口10050-10051,協議tcp;/三、開放通源ip爲任意,端口9527,協議tcp;

 

4、firewall經常使用命令

    一、重啓、關閉、開啓、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  關閉開機啓動

    二、查看狀態

        firewall-cmd --state

    三、查看防火牆規則

        firewall-cmd --list-all

 

 

5、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 #設置防火牆開機啓動

相關文章
相關標籤/搜索