RHEL 7中防火墙的配置和使用方法
RHEL7中使用了firewalld代替了原来的iptables,操作设置和原来有点不同:
查看防火墙状态:systemctlstatusfirewalld
启动防火墙:systemctlstartfirewalld
停止防火墙:systemctlstopfirewalld
防火墙中的一切都与一个或者多个区域相关联,下面对各个区进行说明:
ZoneDescription ----------------------------------------------------- drop(immutable)Denyallincomingconnections,outgoingonesareaccepted. block(immutable)Denyallincomingconnections,withICMPhostprohibitedmessagesissued. trusted(immutable)Allowallnetworkconnections public Publicareas,donottrustothercomputers external Forcomputerswithmasqueradingenabled,protectingalocalnetwork dmz Forcomputerspubliclyaccessiblewithrestrictedaccess. work Fortrustedworkareas home Fortrustedhomenetworkconnections internal Forinternalnetwork,restrictincomingconnections
drop(丢弃)
任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。
block(限制)
任何接收的网络连接都被IPv4的icmp-host-prohibited信息和IPv6的icmp6-adm-prohibited信息所拒绝。
public(公共)
在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接。
external(外部)
特别是为路由器启用了伪装功能的外部网。您不能信任来自网络的其他计算,不能相信它们不会对您的计算机造成危害,只能接收经过选择的连接。
dmz(非军事区)
用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的内部网络,仅仅接收经过选择的连接。
work(工作)
用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅接收经过选择的连接。
home(家庭)
用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接。
internal(内部)
用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接。
trusted(信任)
可接受所有的网络连接。
操作防火墙的一些常用命令:
--显示防火墙状态
[root@localhostzones]#firewall-cmd--state
running
--列出当前有几个zone
[root@localhostzones]#firewall-cmd--get-zones
blockdmzdropexternalhomeinternalpublictrustedwork
--取得当前活动的zones
[root@localhostzones]#firewall-cmd--get-active-zones
public
interfaces:ens32veth4103622
--取得默认的zone
[root@localhostzones]#firewall-cmd--get-default-zone
public
--取得当前支持service
[root@localhostzones]#firewall-cmd--get-service
RH-Satellite-6amanda-clientbaculabacula-clientdhcpdhcpv6dhcpv6-clientdnsftphigh-availabilityhttphttpsimapsippipp-clientipseckerberoskpasswdldapldapslibvirtlibvirt-tlsmdnsmountdms-wbtMySQLnfsntpopenvpnpmcdpmproxypmwebapipmwebapispop3spostgresqlproxy-dhcpradiusrpc-bindsambasamba-clientsmtpsshtelnettftptftp-clienttransmission-clientvnc-serverwbem-https
--检查下一次重载后将激活的服务。
[root@localhostzones]#firewall-cmd--get-service--permanent
RH-Satellite-6amanda-clientbaculabacula-clientdhcpdhcpv6dhcpv6-clientdnsftphigh-availabilityhttphttpsimapsippipp-clientipseckerberoskpasswdldapldapslibvirtlibvirt-tlsmdnsmountdms-wbtmysqlnfsntpopenvpnpmcdpmproxypmwebapipmwebapispop3spostgresqlproxy-dhcpradiusrpc-bindsambasamba-clientsmtpsshtelnettftptftp-clienttransmission-clientvnc-serverwbem-https
--列出zonepublic端口
[root@localhostzones]#firewall-cmd--zone=public--list-ports
--列出zonepublic当前设置
[root@localhostzones]#firewall-cmd--zone=public--list-all
public(default,active)
interfaces:eno16777736
sources:
services:dhcpv6-clientssh
ports:
masquerade:no
forward-ports:
icmp-blocks:
richrules:
--增加zonepublic开放httpservice
[root@localhostzones]#firewall-cmd--zone=public--add-service=http
success
[root@localhostzones]#firewall-cmd--permanent--zone=internal--add-service=http
success
--重新加载配置
[root@localhostzones]#firewall-cmd--reload
success
--增加zoneinternal开放443/tcp协议端口
[root@localhostzones]#firewall-cmd--zone=internal--add-port=443/tcp
success
--列出zoneinternal的所有service
[root@localhostzones]#firewall-cmd--zone=internal--list-services
dhcpv6-clientipp-clientmdnssamba-clientssh
设置黑/白名单
--增加172.28.129.0/24网段到zonetrusted(信任)
[root@localhostzones]#firewall-cmd--permanent--zone=trusted--add-source=172.28.129.0/24
success
--列出zonetruste的白名单
[root@localhostzones]#firewall-cmd--permanent--zone=trusted--list-sources
172.28.129.0/24
--活动的zone
[root@localhostzones]#firewall-cmd--get-active-zones
public
interfaces:eno16777736
--添加zonetruste后重新加载,然后查看--get-active-zones
[root@localhostzones]#firewall-cmd--reload
success
[root@localhostzones]#firewall-cmd--get-active-zones
public
interfaces:ens32veth4103622
trusted
sources:172.28.129.0/24
--列出zonedrop所有规则
[root@localhostzones]#firewall-cmd--zone=drop--list-all
drop
interfaces:
sources:
services:
ports:
masquerade:no
forward-ports:
icmp-blocks:
richrules:
--添加172.28.13.0/24到zonedrop
[root@localhostzones]#firewall-cmd--permanent--zone=drop--add-source=172.28.13.0/24
success
--添加后需要重新加载
[root@localhostzones]#firewall-cmd--reload
success
[root@localhostzones]#firewall-cmd--zone=drop--list-all
drop
interfaces:
sources:172.28.13.0/24
services:
ports:
masquerade:no
forward-ports:
icmp-blocks:
richrules:
[root@localhostzones]#firewall-cmd--reload
success
--从zonedrop中删除172.28.13.0/24
[root@localhostzones]#firewall-cmd--permanent--zone=drop--remove-source=172.28.13.0/24
success
--查看所有的zones规则
[root@localhost~]#firewall-cmd--list-all-zones
最后再提几点:
1、很多时候我们需要开放端口或开放某IP访问权限,我们需要先查看我们当前默认的zone是哪个,然后在对应的zone里面添加port和source,这样对外才会有作用。
比如我当前的默认zone是public,我需要开放80端口对外访问,则执行如下命令:
[root@localhostzones]#firewall-cmd--zone=public--permanent--add-port=80/tcp
success
[root@localhostzones]#firewall-cmd--reload
success
2、使用命令的时候加上--permanent是永久生效的意思,在重启防火墙服务后依然生效。否则,只对重启服务之前有效。
3、我们执行的命令,结果其实都体现在具体的配置文件中,其实我们可以直接修改对应的配置文件即可。
以publiczone为例,对应的配置文件是/etc/firewalld/zones/public.xml,像我们刚刚添加80端口后,体现在public.xml中的内容为:
[root@localhostzones]#catpublic.xml <?xmlversion="1.0"encoding="utf-8"?> <zone> <short>Public</short> <description>Foruseinpublicareas.Youdonottrusttheothercomputersonnetworkstonotharmyourcomputer.Onlyselectedincomingconnectionsareaccepted.</description> <servicename="dhcpv6-client"/> <servicename="ssh"/> <portprotocol="tcp"port="80"/> </zone>
这个大家可自己再进一步了解下配置文件的结构后,进行自行配置,不过记得要在配置后--reload或重启firewall服务。
以上就是小编为大家带来的RHEL7中防火墙的配置和使用方法全部内容了,希望大家多多支持毛票票~