Centos7 firewall防火墻常用配置

一、 Centos7和Centos6 防火墻的區(qū)別:

使用的工具不一樣了。Centos6 使用的是iptables,Centos7 使用的是filewall
iptables 用于過濾數(shù)據(jù)包,屬于網(wǎng)絡(luò)層防火墻。
firewall 能夠允許哪些服務(wù)可用,那些端口可用...屬于更高一層的防火墻。

二、常用命令:

vim  /usr/lib/firewalld/services/ssh.xml
vim  /usr/lib/firewalld/services/http.xml
systemctl enable firewalld.service
systemctl restart firewalld.service
firewall-cmd --state    查看狀態(tài)
firewall-cmd --list-all   查看過濾的列表信息
firewall-cmd --zone=public --permanent --add-port=8502/tcp     添加一個協(xié)議為tcp的8502端口過濾
vim /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
This allows a great deal of flexibility as the files can be edited, written to, backed up, used as templates for other installations and so on.
firewalld的配置存儲在各種XML文件中
在/usr/lib/firewalld和/etc/firewalld中
這允許極大的靈活性,因為文件可以被編輯、寫入、備份、用作其他安裝的模板等等。

注意:以下firewalld 的操作只有重啟之后才有效:service firewalld restart
1、系統(tǒng)配置目錄(/usr/lib/firewalld/services)
目錄中存放定義好的網(wǎng)絡(luò)服務(wù)和端口參數(shù),系統(tǒng)參數(shù),不能修改。

image.png

2、用戶配置目錄(/etc/firewalld/)

image.png

3、如何自定義添加端口
用戶可以通過修改配置文件的方式添加端口,也可以通過命令的方式添加端口,注意:修改的內(nèi)容會在/etc/firewalld/目錄下的配置文件中體現(xiàn)。
3.1 、命令的方式添加端口:

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

參數(shù)介紹:
1、firwall-cmd:是Linux提供的操作firewall的一個工具;
2、--permanent:表示設(shè)置為持久;
3、--add-port:標(biāo)識添加的端口;
另外,firewall中有Zone的概念,可以將具體的端口制定到具體的zone配置文件中。

例如:添加8010端口
firewall-cmd --zone=public --permanent --add-port=8010/tcp
--zone=public:指定的zone為public;
如果--zone=dmz 這樣設(shè)置的話,會在dmz.xml文件中新增一條。

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,指定端口、協(xié)議     
<port protocol="tcp" port="80"/>     
<accept/>   
</rule>
<rule family="ipv4"> 放通任意ip訪問服務(wù)器的9527端口     
<port protocol="tcp" port="9527"/>     
<accept/>   
</rule>
</zone>

上述的一個配置文件可以很好的看出:
1、添加需要的規(guī)則,開放通源ip為122.10.70.234,端口514,協(xié)議tcp;
2、開放通源ip為123.60.255.14,端口10050-10051,協(xié)議tcp;
3、開放通源ip為任意,端口9527,協(xié)議tcp;

四、firewall常用命令
1、重啟、關(guān)閉、開啟、firewalld.serverice 服務(wù)

Service firewalld restart 重啟
Service firewalld start  開啟
Service firewalld stop  關(guān)閉
systemctl status firewalld  查看狀態(tài)
systemctl stop firewalld  關(guān)閉
systemctl start firewalld 開啟
systemctl  restart firewalld 重啟
systemctl  disable firewalld  關(guān)閉開機(jī)啟動

2、查看狀態(tài)

firewall-cmd --state

3、查看防火墻規(guī)則

firewall-cmd --list-all

五、Centos 切換為iptables防火墻
提示:切換到iptables首先應(yīng)該關(guān)掉默認(rèn)的firewalld,然后安裝iptables服務(wù)。
1、關(guān)閉firewall:

systemctl stop firewalld.service
systemctl disable firewalld.service #禁止firewall開機(jī)啟動

2、安裝iptables防火墻:

yum install iptables-services #安裝

3、編輯iptables防火墻配置:

vim  /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 start iptables.service #開啟
systemctl enable iptables.service #設(shè)置防火墻開機(jī)啟動
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容