• ADADADADAD

    centos7云服务器如何使用firewalld防火墙[ 建站问答 ]

    建站问答 时间:2024-11-29 09:38:35

    作者:文/会员上传

    简介:

    centos7云服务器使用firewalld防火墙的方法:1、打开centos7云服务器终端控制台;2、输入“systemctl status firewalld”命令查看firewalld状态来检查firewalld是否已成功安装;3

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    centos7云服务器使用firewalld防火墙的方法:1、打开centos7云服务器终端控制台;2、输入“systemctl status firewalld”命令查看firewalld状态来检查firewalld是否已成功安装;3、firewalld防火墙安装成功后,能够用来进行区域管理、服务管理、端口管理等等操作。

    具体内容如下:

    首先我们需要确认firewalld是否已成功安装。用下面的命令来查看firewalld的状态:

    $systemctlstatusfirewalld

    firewalld.service-firewalld-dynamicfirewalldaemon

    Loaded:loaded(/usr/lib/systemd/system/firewalld.service;enabled;vendorpreset:enabled)

    Active:active(running)sinceThu2016-03-1015:07:00UTC;1min30sago

    可以看出,在这台云服务器上,firewalld已成功安装并加载。

    我们还可以用firewall-cmd工具来查看:

    $firewall-cmd--state

    running

    1、区域管理

    和iptables不同,firewalld引入了一个叫做区域的概念,用于定义安全等级。区域的使用很灵活,我们可以把不同的网络接口添加到不同的区域中,也可以把所有的网络接口都添加到一个区域中。通过这样的划分,只要在区域上设置了防火墙规则,就可以应用于该区域下的所有网络接口。为了便于用户上手,firewalld建立了一个初始的默认区域。和区域相关的常用命令如下。

    查看当前区域:

    $firewall-cmd--get-zones

    blockdmzdropexternalhomeinternalpublictrustedwork

    查看默认区域:

    $firewall-cmd--get-default-zone

    public

    更改默认区域:

    $firewall-cmd--set-default-zone=home

    值得注意的是,firewalld的设置文件是/etc/firewalld/firewalld.conf。虽然可以通过手工修改这个文件来设置firewalld,但是仍然推荐使用firewall-cmd命令,否则容易出现错误。

    获取活跃区域:

    $firewall-cmd--get-active-zones

    获取某个网络接口对应的区域(以eth0为例):

    $firewall-cmd--get-zone-of-interface=eth0

    如果默认区域不能满足更精细化的需求,还可以创建新区域(以zone1为例):

    $firewall-cmd--permanent--new-zone=zone1

    创建之后,需要重新加载:

    $firewall-cmd--reload

    把某个网络接口(以eth1为例)添加到某个区域(以internal为例)下:

    $firewall-cmd--permanent--zone=internal--change-interface=eth1

    列出某个区域下的全部信息,包括网络接口、允许的服务、端口设置等:

    $firewall-cmd--permanent--zone=public--list-all

    public(default)

    interfaces:

    sources:

    services:dhcpv6-clientssh

    ports:

    masquerade:no

    forward-ports:

    icmp-blocks:

    richrules:

    2、服务管理

    区域设置完毕之后,就可以把服务添加到区域中了。这里的服务指的是协议和端口的组合。

    查看可用服务:

    $firewall-cmd--get-services

    RH-Satellite-6amanda-clientbaculabacula-clientdhcpdhcpv6dhcpv6-clientdnsfreeipa-ldapfreeipa-ldapsfreeipa-replicationftphigh-availabilityhttphttpsimapsippipp-clientipseciscsi-targetkerberoskpasswdldapldapslibvirtlibvirt-tlsmdnsmountdms-wbtmysqlnfsntpopenvpnpmcdpmproxypmwebapipmwebapispop3spostgresqlproxy-dhcpradiusrpc-bindrsyncdsambasamba-clientsmtpsshtelnettftptftp-clienttransmission-clientvdsmvnc-serverwbem-https

    查看默认区域下的服务:

    $firewall-cmd--list-services

    dhcpv6-clientssh

    把某个服务(以http为例)添加到某个区域(以public为例)下:

    $firewall-cmd--permanent--zone=public--add-service=http

    重新加载firewalld:

    $firewall-cmd--reload

    验证是否添加成功:

    $firewall-cmd--zone=public--list-services

    dhcpv6-clienthttpssh

    把某个服务(以http为例)从某个区域(以public为例)中删除:

    $firewall-cmd--permanent--zone=public--remove-service=http

    如果我们要做一些自定义设置,比如修改了默认的SSH端口,那么就需要添加自定义服务。服务是以XML文件的格式来定义的,默认服务存放在/usr/lib/firewalld/services目录下。我们用tree命令查看该目录下的文件:

    $tree/usr/lib/firewalld/services

    /usr/lib/firewalld/services

    ├──amanda-client.xml

    ├──bacula-client.xml

    ├──bacula.xml

    ├──dhcpv6-client.xml

    ├──dhcpv6.xml

    ├──dhcp.xml

    ├──dns.xml

    ├──freeipa-ldaps.xml

    ├──freeipa-ldap.xml

    ├──freeipa-replication.xml

    ├──ftp.xml

    ├──high-availability.xml

    ├──https.xml

    ├──http.xml

    创建自定义服务最简单的方法就是复制一份默认的服务XML文件,在此基础上做修改。自定义服务存放在/etc/firewalld/services目录下。例如,我们要创建自定义SSH服务,先复制一份SSH服务对应的XML文件。

    $cp/usr/lib/firewalld/services/ssh.xml/etc/firewalld/services/ssh-custom.xml

    XML文件的内容如下:

    $cat/etc/firewalld/services/ssh-custom.xml

    <?xmlversion="1.0"encoding="utf-8"?>

    <service>

    <short>SSH</short>

    <description>SecureShell(SSH)isaprotocolforloggingintoandexecutingcommandsonremotemachines.Itprovidessecureencryptedcommunications.IfyouplanonaccessingyourmachineremotelyviaSSHoverafirewalledinterface,enablethisoption.Youneedtheopenssh-serverpackageinstalledforthisoptiontobeuseful.</description>

    <portprotocol="tcp"port="22"/>

    </service>

    假如我们要把端口号从22改成1234,那么要修改short标签的内容和port标签的属性。修改后的文件如下:

    $nano/etc/firewalld/services/ssh-custom.xml

    <?xmlversion="1.0"encoding="utf-8"?>

    <service>

    <short>SSH-Custom</short>

    <description>SecureShell(SSH)isaprotocolforloggingintoandexecutingcommandsonremotemachines.Itprovidessecureencryptedcommunications.IfyouplanonaccessingyourmachineremotelyviaSSHoverafirewalledinterface,enablethisoption.Youneedtheopenssh-serverpackageinstalledforthisoptiontobeuseful.</description>

    <portprotocol="tcp"port="1234"/>

    </service>

    重新加载firewalld:

    $firewall-cmd--reload

    把新创建的服务添加到默认区域下:

    $firewall-cmd--permanent--zone=public--add-service=ssh-custom

    3、端口管理

    除了通过服务来定义协议和端口规则之外,还可以单独设置协议和端口的防火墙规则。比如我们要运行TCP协议7777端口,就可以这样设置:

    $firewall-cmd--permanent--zone=public--add-port=7777/tcp

    设置端口范围也是没有问题的:

    $firewall-cmd--permanent--zone=public--add-port=7000-8000/tcp

    删除刚才设置的协议和端口:

    $firewall-cmd--permanent--zone=public--remove-port=7777/tcp

    查看某个区域下允许的协议和端口:

    $firewall-cmd--zone=public--list-ports

    7000-8000/tcp

    在所有设置完成之后,需要启用firewalld,使其能跟随系统自动启动。

    $systemctlenablefirewalld

    centos7云服务器如何使用firewalld防火墙.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载