<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[CentOS7中firewall防火墙详解和配置，.xml服务配置详解]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Mon, 03 Jul 2017 02:58:47 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	官方文档地址：<br/><a href="https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html#sec-Introduction_to_firewalld1" target="_blank">https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html#sec-Introduction_to_firewalld1</a><br/><br/>修改防火墙配置文件之前，需要对之前防火墙做好备份<br/>重启防火墙后，需要确认防火墙状态和防火墙规则是否加载，若重启失败或规则加载失败，则所有请求都会被防火墙拦截<br/><div class="code"><br/>systemctl status firewall&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;#查看firewall服务状态<br/>firewall-cmd --state&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;#查看firewall的状态<br/>firewall-cmd --list-all <br/>&nbsp;&nbsp;&nbsp;&nbsp;#查看防火墙规则（只显示/etc/firewalld/zones/public.xml中防火墙策略）<br/>firewall-cmd --list-all-zones <br/>&nbsp;&nbsp;&nbsp;&nbsp;#查看所有的防火墙策略（即显示/etc/firewalld/zones/下的所有策略）<br/>firewall-cmd --reload<br/>&nbsp;&nbsp;&nbsp;&nbsp;#重新加载配置文件<br/></div><br/>方法1、修改配置文件/etc/firewalld/zones/public.xml，重启或重新加载配置生效<br/><div class="code"><br/>&#91;root@nginx01 zones&#93;# cat public.xml<br/>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br/>&lt;zone&gt;<br/>&nbsp;&nbsp;&lt;short&gt;Public&lt;/short&gt;<br/>&nbsp;&nbsp;&lt;description&gt;For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.&lt;/description&gt;<br/>&nbsp;&nbsp;&lt;rule family=&quot;ipv4&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;source address=&quot;122.x.x.234&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;port protocol=&quot;udp&quot; port=&quot;514&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;accept/&gt;<br/>&nbsp;&nbsp;&lt;/rule&gt;<br/>&nbsp;&nbsp;&lt;rule family=&quot;ipv4&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;source address=&quot;123.x.x.14&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;port protocol=&quot;tcp&quot; port=&quot;10050-10051&quot;/&gt; ##可以开放端口地址范围&quot;10050-10051&quot;，不单只限定一个端口<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;accept/&gt;<br/>&nbsp;&nbsp;&lt;/rule&gt;<br/> &lt;rule family=&quot;ipv4&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;source address=&quot;192.x.x.114&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;##放通指定ip，指定端口、协议<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;port protocol=&quot;tcp&quot; port=&quot;80&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;accept/&gt;<br/>&nbsp;&nbsp;&lt;/rule&gt;<br/>&lt;rule family=&quot;ipv4&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;##放通任意ip访问服务器的9527端口<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;port protocol=&quot;tcp&quot; port=&quot;9527&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;accept/&gt;<br/>&nbsp;&nbsp;&lt;/rule&gt;<br/>&lt;/zone&gt;<br/> <br/> <br/>firewall-cmd --reload<br/>service firewalld restart&nbsp;&nbsp;&nbsp;&nbsp;#使配置文件重新加载<br/></div><br/>方法2、命令行修改防火墙策略，仍需重启firewalld.service或重新加载防火墙配置文件<br/><div class="code"><br/>firwall-cmd --permanent --add-port=9527/tcp&nbsp;&nbsp;&nbsp;&nbsp;插入防火墙规则，放通9527端口。<br/>success <br/></div><br/>#命令执行成功同时，在/etc/firewall/zones/public.xml中自动生成该规则。<br/><div class="code"><br/>&lt;zone&gt;<br/>&nbsp;&nbsp;&lt;short&gt;xx.&lt;/short&gt;<br/>&nbsp;&nbsp;&lt;description&gt;xxx.&lt;/description&gt;<br/>&nbsp;&nbsp;&lt;port protocol=&quot;tcp&quot; port=&quot;9527&quot;/&gt;<br/>&lt;/zone&gt;<br/></div><br/><div class="code"> <br/>service firewalld restart<br/>firewall-cmd --reload&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #重启或重新加载配置文件，使配置生效&nbsp;&nbsp; <br/>firewall-cmd --list-all<br/>firewall-cmd --permanent --query-port=9527/tcp&nbsp;&nbsp;&nbsp;&nbsp;#查询刚插入的规则是否生效<br/><br/>firewall-cmd --zone=public --add-port=80/tcp --permanent&nbsp;&nbsp;&nbsp;&nbsp;添加防火墙规则；<br/>firewall-cmd --reload&nbsp;&nbsp;&nbsp;&nbsp;重新加载防火墙；<br/>firewall-cmd --permanent --zone=public --add-masquerade&nbsp;&nbsp;&nbsp;&nbsp;允许内网上网；<br/></div><br/>/etc/firewalld/zones/public.xml添加策略标准规则：<br/><div class="code"><br/>firewall-cmd --permanent --add-rich-rule &#039;rule family=ipv4 source address=122.x.x.234/24 port port=5423 protocol=tcp drop&#039;&nbsp;&nbsp;&nbsp;&nbsp;<br/>firewall-cmd --permanent --add-rich-rule &#039;rule family=ipv4 source address=122.x.x.234 port port=80 protocol=tcp accept&#039;&nbsp;&nbsp;&nbsp;&nbsp;<br/>firewall-cmd --reload<br/>&#91;root@nginx02 ~&#93;# firewall-cmd --list-all<br/>public (default, active)<br/>&nbsp;&nbsp;interfaces: em1<br/>&nbsp;&nbsp;sources: <br/>&nbsp;&nbsp;services: <br/>&nbsp;&nbsp;ports: <br/>&nbsp;&nbsp;masquerade: no<br/>&nbsp;&nbsp;forward-ports: <br/>&nbsp;&nbsp;icmp-blocks: <br/>&nbsp;&nbsp;rich rules: <br/>&nbsp;&nbsp;&nbsp;&nbsp;rule family=&quot;ipv4&quot; source address=&quot;122.x.x.234&quot; port port=&quot;5234&quot; protocol=&quot;tcp&quot; drop<br/>&nbsp;&nbsp;&nbsp;&nbsp;rule family=&quot;ipv4&quot; source address=&quot;122.x.x.234&quot; port port=&quot;80&quot; protocol=&quot;tcp&quot; accept<br/>&nbsp;&nbsp;&nbsp;&nbsp;rule family=&quot;ipv4&quot; source address=&quot;123.x.x.14&quot; port port=&quot;10050-10051&quot; protocol=&quot;tcp&quot; accept<br/></div><br/>二、以服务的形式（例如：ssh.xml/http.xml）添加新的防火墙策略<br/><div class="code"><br/>cat /etc/firewalld/zones/ssh.xml<br/>&lt;?xml version=&#039;1.0&#039; encoding=&#039;utf-8&#039;?&gt;<br/>&lt;zone&gt;<br/>&nbsp;&nbsp;&lt;short&gt;ssh&lt;/short&gt;<br/>&nbsp;&nbsp;&lt;description&gt;ssh.&lt;/description&gt;<br/>#fortress-new<br/>&nbsp;&nbsp;&lt;source address=&#039;122.x.x.2/29&#039;/&gt;<br/>&nbsp;&nbsp;&lt;service name=&#039;ssh&#039;/&gt;<br/>&lt;/zone&gt;<br/> <br/>firewall-cmd --list-all-zones<br/>...<br/>ssh<br/>&nbsp;&nbsp;interfaces: <br/>&nbsp;&nbsp;sources: 122.x.x.2/29 <br/>&nbsp;&nbsp;services: ssh<br/>&nbsp;&nbsp;ports: <br/>&nbsp;&nbsp;masquerade: no<br/>&nbsp;&nbsp;forward-ports: <br/>&nbsp;&nbsp;icmp-blocks: <br/>&nbsp;&nbsp;rich rules:<br/>...<br/></div><br/>因为在/usr/lib/firewalld/services/中事先定义了ssh.xml的相应的规则<br/><div class="code"><br/>cat /usr/lib/firewalld/services/ssh.xml <br/> <br/>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br/>&lt;service&gt;<br/>&nbsp;&nbsp;&lt;short&gt;SSH&lt;/short&gt;<br/>&nbsp;&nbsp;&lt;description&gt;Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.&lt;/description&gt;<br/>&nbsp;&nbsp;&lt;port protocol=&quot;tcp&quot; port=&quot;22&quot;/&gt;<br/>&lt;/service&gt;<br/></div><br/>##定义ssh.xml服务使用的协议，和通信的端口信息。<br/><div class="code"><br/>自定义服务(mongo.xml)模块<br/>cat /usr/lib/firewalld/services/mongo.xml<br/>&lt;service&gt;<br/>&nbsp;&nbsp;&lt;short&gt;mongo&lt;/short&gt;<br/>&nbsp;&nbsp;&lt;description&gt;The service of mongo.&lt;/description&gt;<br/>&nbsp;&nbsp;&lt;port protocol=&quot;tcp&quot; port=&quot;27017&quot;/&gt;<br/>&lt;/service&gt;<br/> <br/>防火墙应用服务器模块<br/>cat /etc/firewalld/zones/mongo.xml<br/>&lt;zone&gt;<br/>&nbsp;&nbsp;&lt;short&gt;mongo&lt;/short&gt;<br/>&nbsp;&nbsp;&lt;description&gt;mongo service&lt;/description&gt;<br/>&nbsp;&nbsp;&lt;source address=&quot;2.2.2.2/24&quot;/&gt;<br/>&nbsp;&nbsp;&lt;service name=&quot;mongo&quot;/&gt;<br/>&lt;/zone&gt;<br/> <br/>查看mongo.xml服务的防火墙生效情况<br/>firewall-cmd --list-all-zones<br/>...<br/>mongo<br/>&nbsp;&nbsp;interfaces: <br/>&nbsp;&nbsp;sources: 2.2.2.2/24 <br/>&nbsp;&nbsp;services: mongo<br/>&nbsp;&nbsp;ports: <br/>&nbsp;&nbsp;masquerade: no<br/>&nbsp;&nbsp;forward-ports: <br/>&nbsp;&nbsp;icmp-blocks: <br/>&nbsp;&nbsp;rich rules:<br/>...<br/></div><br/>PS：如果一个IP同时应用在多个.xml服务，则只会在最先匹配的服务生效，之后的服务则不匹配该IP。若需要将该IP应用在多个服务，则需要另开服务，将该IP应用的服务都绑定在该服务下。<br/><br/>例如：10.10.86.44同时需要放通ssh、http、mysql等服务<br/><div class="code"><br/>cat multi.xml <br/> <br/>&lt;zone&gt;<br/>&nbsp;&nbsp;&lt;short&gt; multi services&lt;/short&gt;<br/>&nbsp;&nbsp;&lt;description&gt;IP of 10.10.86.44 apply in multi srevices.&lt;/description&gt;<br/>&nbsp;&nbsp;&lt;source address=&quot;10.10.86.44&quot;/&gt;<br/>&nbsp;&nbsp;&lt;service name=&quot;ssh&quot;/&gt;<br/>&nbsp;&nbsp;&lt;service name=&quot;mysql&quot;/&gt;<br/>&nbsp;&nbsp;&lt;service name=&quot;http&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;##同时添加多个服务<br/>&lt;/zone&gt;<br/> <br/>firewall-cmd --list-all-zones<br/>...<br/>multi<br/>&nbsp;&nbsp;interfaces: <br/>&nbsp;&nbsp;sources: 10.10.86.44<br/>&nbsp;&nbsp;services: http mysql ssh<br/>&nbsp;&nbsp;ports: <br/>&nbsp;&nbsp;masquerade: no<br/>&nbsp;&nbsp;forward-ports: <br/>&nbsp;&nbsp;icmp-blocks: <br/>&nbsp;&nbsp;rich rules:<br/>...<br/></div><br/>总结：<br/>（1）修改配置文件的方法和命令行添加防火墙策略的方法，都不能立即生效，需要重启或重新加载防火墙配置文件，是新的策略生效。<br/><div class="code"><br/>service firewalld restart<br/>firewall-cmd --reload<br/></div><br/>（2）修改完防火墙后，一定要检查防火墙状态和策略加载状态，若失败则可能拦截所有请求。<br/>（3）以服务（ssh.xml）的方式添加防火墙，可以方便管理。前提需要先查看/usr/lib/firewalld/services中是否定义相应的服务。<br/>（4）若一个IP同时应用多个了服务，则会最先匹配第一个应用了该ip的服务，之后的服务中则不匹配。若需要同时应用到多个服务，则需要另开服务，在该服务(multi.xml)下同时应用多个服务(ssh/http/mysql等)<br/><br/>扩展文档：<br/>1.CentOS7下Firewall防火墙配置用法详解<br/><a href="http://www.centoscn.com/CentOS/Intermediate/2015/0313/4879.html" target="_blank">http://www.centoscn.com/CentOS/Intermediate/2015/0313/4879.html</a><br/>2.在CentOS7.0 中默认的防火墙 “firewall” 使用方法<br/><a href="http://f.dataguru.cn/thread-473492-1-1.html" target="_blank">http://f.dataguru.cn/thread-473492-1-1.html</a><br/>3.CentOS 7 巨大变动之 firewalld 取代 iptables<br/><a href="http://blog.csdn.net/smstong/article/details/39317277" target="_blank">http://blog.csdn.net/smstong/article/details/39317277</a>（外文官方文档）<br/>4.CentOS 7 中firewall-cmd命令<br/><a href="http://blog.sina.com.cn/s/blog_43b39e250102v4zt.html" target="_blank">http://blog.sina.com.cn/s/blog_43b39e250102v4zt.html</a><br/>5.CentOS7 Firewall防火墙配置用法详解<br/><a href="http://www.111cn.net/sys/linux/75503.htm" target="_blank">http://www.111cn.net/sys/linux/75503.htm</a><br/>Tags - <a href="http://www.dzhope.com/tags/centos/" rel="tag">centos</a> , <a href="http://www.dzhope.com/tags/firewall/" rel="tag">firewall</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] CentOS7中firewall防火墙详解和配置，.xml服务配置详解]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>