CentOS / RHEL 7 防火墙

1 简述

CentOS 7 与 CentOS 6 对于系统功能的管理与维护方式上发生了巨大的变化,当然CentOS 7 仍兼容CentOS 6中的管理方式。

CentOS 7 中使用的是 systemctl 命令来管理与维护系统功能。而CentOS 6 及之前的版本 中使用的是service命令。

CentOS 7 中防火墙的名字是firewalld[.service] . 防火墙本身也提供了管理命令: firewall-cmd. firewalld 是 iptables的前端控制器, 是 iptables 的一个封装,可以让你更容易地管理 iptables 规则,虽然 iptables 命令仍可用于 FirewallD,但建议使用 FirewallD 时仅 使用 FirewallD 命令。

与iptables相比,区别有以下两点:

  • FirewallD 使用区域和服务而不是链式规则。
  • 它动态管理规则集,允许更新规则而不破坏现有会话和连接。

根据RHEL 官网中的描述,防火墙按照网络的可信任度,分为了几个级别,根据不同的信任级别对 数据包选择不同的处理方式.

firewalld网络分类(zone)
  • drop 丢弃所有进入的包,而不给出任何响应
  • block 拒绝所有外部发起的连接,允许内部发起的连接
  • public 允许指定的进入连接
  • external 同上,对伪装的进入连接,一般用于路由转发
  • dmz 允许受限制的进入连接
  • work/home 允许受信任的计算机被限制的进入连接,类似于工作组
  • internal 同上,范围针对所有互联网用户
  • trusted 信任所有连接

2 常用基本操作

 

2.1 查看防火墙状态

firewall-cmd --state
或者
systemctl status firewalld

示例:

[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@localhost ~]# firewall-cmd --state
not running

2.2 开启防火墙

systemctl start firewalld
或者
systemctl start firewalld.service

2.3 关闭防火墙

systemctl stop firewalld
或者
systemctl stop firewalld.service

2.4 开机自动启动防火墙

systemctl enable firewalld
或者
systemctl enable firewalld.service

2.5 开机关闭防火墙

systemctl disenable firewalld
或者
systemctl disenable firewalld.service

3 firewalld配置

关于firewalld 命令的使用非常简单,使用/firewall-cmd –help/ 查看帮助文档。 下面只是列出其中几个常用的功能。

3.1 重新加载配置

firewall-cmd --reload # 更新规则,不重启服务
firewall-cmd --complete-reload # 更新规则,重启服务

3.2 zone

zone的配置文件位于/etc/firewalld/zones中,默认只有public类的配置文件。

3.2.1 默认zone

  • 查看默认zone

    firewall-cmd --get-default-zone
    
  • 设置默认zone

    firewall-cmd --set-default-zone=<zone_name>
    

3.2.2 查看zone信息

zone信息中包含了对应的网卡.

firewall-cmd --get-active-zones

示例:

[root@localhost ~]# firewall-cmd --get-active-zones
public
interfaces: eth0 eth1

3.2.3 查看网卡对应zone

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

示例:

[root@localhost ~]# firewall-cmd --get-zone-of-interface=eth0
public

3.2.4 将网卡添加至zone中

示例:

firewall-cmd --zone=public --add-interface=eth0 --permanent

3.2.5 查看指定zone打开的端口

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

示例:

[root@localhost ~]# firewall-cmd --zone=public --list-ports

[root@localhost ~]#

3.3 服务管理

设置zone的服务时,所做的修改最终会保存到/etc/firewalld/zones/<zone_name>.xml 所以,我们可以直接修改zone的配置文件,然后再执行 firewall-cmd –reload 重新加载

  • 查看支持的服务

    firewall-cmd --get-service
    

    结果中,不同的服务之间使用空格分隔。实际上,这些服务的名字来源于/usr/lib/firewalld/services/ 和/etc/firewalld/services,firewalld 先在后者中查找服务,然后再在前者中查找服务。 服务的配置文件保存为xml格式。如下:

    [root@localhost ~]# ls /usr/lib/firewalld/services/
    amanda-client.xml dhcpv6-client.xml ganglia-master.xml jenkins.xml minidlna.xml ovirt-imageio.xml puppetmaster.xml smtps.xml tftp.xml
    amanda-k5-client.xml dhcpv6.xml git.xml kadmin.xml mongodb.xml ovirt-storageconsole.xml quassel.xml smtp.xml tinc.xml
    bacula-client.xml dhcp.xml gre.xml kerberos.xml mosh.xml ovirt-vmconsole.xml radius.xml snmptrap.xml tor-socks.xml
    bacula.xml dns.xml high-availability.xml kibana.xml mountd.xml pmcd.xml redis.xml snmp.xml transmission-client.xml
    bgp.xml docker-registry.xml https.xml klogin.xml mssql.xml pmproxy.xml RH-Satellite-6.xml spideroak-lansync.xml upnp-client.xml
    bitcoin-rpc.xml docker-swarm.xml http.xml kpasswd.xml ms-wbt.xml pmwebapis.xml rpc-bind.xml squid.xml vdsm.xml
    bitcoin-testnet-rpc.xml dropbox-lansync.xml imaps.xml kprop.xml murmur.xml pmwebapi.xml rsh.xml ssh.xml vnc-server.xml
    bitcoin-testnet.xml elasticsearch.xml imap.xml kshell.xml mysql.xml pop3s.xml rsyncd.xml syncthing-gui.xml wbem-https.xml
    bitcoin.xml freeipa-ldaps.xml ipp-client.xml ldaps.xml nfs3.xml pop3.xml samba-client.xml syncthing.xml xmpp-bosh.xml
    ceph-mon.xml freeipa-ldap.xml ipp.xml ldap.xml nfs.xml postgresql.xml samba.xml synergy.xml xmpp-client.xml
    ceph.xml freeipa-replication.xml ipsec.xml libvirt-tls.xml nmea-0183.xml privoxy.xml sane.xml syslog-tls.xml xmpp-local.xml
    cfengine.xml freeipa-trust.xml ircs.xml libvirt.xml nrpe.xml proxy-dhcp.xml sips.xml syslog.xml xmpp-server.xml
    condor-collector.xml ftp.xml irc.xml managesieve.xml ntp.xml ptp.xml sip.xml telnet.xml zabbix-agent.xml
    ctdb.xml ganglia-client.xml iscsi-target.xml mdns.xml openvpn.xml pulseaudio.xml smtp-submission.xml tftp-client.xml zabbix-server.xml

    此时,如果我们在这个路径里添加一个xml文件,再执行/firewall-cmd –get-service/看看,

    [root@localhost services]# cd /usr/lib/firewalld/services/
    [root@localhost services]# cp ftp.xml ftp1.xml
    [root@localhost services]# systemctl stop firewalld
    [root@localhost services]# systemctl start firewalld
    [root@localhost services]# firewall-cmd --get-service
    RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ftp1 ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server

    从结果中看, 我们添加了一个ftp1.xml文件,重启firewalld服务后,该文件被识为一个服务。 因此, 如果有需要,我们可以在这个路径中添加firewalld支持的服务。

    修改服务配置后,使用 firewall-cmd –reload 重新加载。

  • 启用某个服务

    firewall-cmd --zone=public --add-service=ftp //临时
    firewall-cmd --permanent --zone=public --add-service=ftp //永久
  • 查看开启的服务

    firewall-cmd --permanent --zone=public --list-services
    
  • 设置IP访问服务

    firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="10.235.0.10/24" service name="http" accept"
    

3.4 端口

  • 添加端口

    firewall-cmd --permanent --zone=public --add-port=8080-8081/tcp //永久
    firewall-cmd --zone=public --add-port=8080-8081/tcp //临时
  • 删除端口

    firewall-cmd --zone=public --remove-port=80/tcp --permanent
    
  • 查看开启的端口

    firewall-cmd --permanent --zone=public --list-ports
    
  • IP伪装 端口转发需要先开启masquerade,可以理解成ip伪装,一般情况下,也就是用于端口转发. 下面是该功能的的基本用法:

    # 查看当前是否开启IP伪装功能
    firewall-cmd --zone=public --query-masquerade
    #打开伪装
    firewall-cmd --zone=public --add-masquerade
    #关闭伪装
    firewall-cmd --zone=public --remove-masquerade

    示例如下:

    [root@localhost zones]# firewall-cmd --get-default-zone
    public
    [root@localhost zones]# firewall-cmd --zone=public --query-masquerade
    no
    [root@localhost zones]# firewall-cmd --zone=public --add-masquerade
    success
    [root@localhost zones]# firewall-cmd --zone=public --query-masquerade
    yes
    [root@localhost zones]# firewall-cmd --zone=public --remove-masquerade
    success
  • 端口转发 在进行端口转发前,需要先开启IP伪装功能。然后再配置转发。

    # 开启端口转发
    firewall-cmd --zone=external --add-masquerade # 转发 tcp 22 端口至 8181端口 firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=8181
    # 转发 22 端口数据至另一个ip的相同端口上
    firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toaddr=10.10.100.10 # 转发 22 端口数据至另一ip的1919端口上
    firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=1919:toaddr=10.10.100.10

3.5 拒绝所有包

紧急情况下才会使用。

  • 查看

    firewall-cmd --query-panic
    
  • 开启

    firewall-cmd --panic-on
    
  • 关闭

    firewall-cmd --panic-off
    
  • firewalld 3.0 之前,使用的是–enable-panic 和 –disable-panic。

Author: halberd.lee

Created: 2019-08-22 Thu 22:33

Validate

Linux(CentOS / RHEL 7) 防火墙的更多相关文章

  1. Linux & CentOS & RHEL

    1.修改centos7的系统编码:https://blog.csdn.net/violet_echo_0908/article/details/58063555 2.windows 环境下使用ultr ...

  2. Linux CentOS 下关闭防火墙

    永久关闭(重启后生效) 开启: chkconfig iptables on 关闭: chkconfig iptables off 临时关闭(重启后失效) 开启: service iptables st ...

  3. 在CentOS/RHEL/Scientific Linux 6下安装 LAMP

    LAMP 是服务器系统中开源软件的一个完美组合.它是 Linux .Apache HTTP 服务器.MySQL 数据库.PHP(或者 Perl.Python)的第一个字母的缩写代码.对于很多系统管理员 ...

  4. redhat linux/CentOS 6/7 如何关闭防火墙?

    redhat linux/CentOS 6/7 如何关闭防火墙?关闭防火墙iptables的具体命令如下: 临时性的完全关闭防火墙,可以不重启机器(但是重启服务器后iptables防火墙服务会自动随系 ...

  5. Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7

    Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7 By SK  - August 12, 201 ...

  6. 【Linux笔记】CentOS&RHEL YUM基础知识

    以下内容收集自网络,以作参考. 一.YUM是什么 YUM = Yellow dog Updater, Modified. 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它 ...

  7. 【Linux】YUM Repositories for CentOS, RHEL & Fedora Systems

    这里是官方wiki:https://wiki.centos.org/AdditionalResources/Repositories 一.简介 YUM(Yellowdog Updater Modifi ...

  8. linux 安装禅道 和 CentOS 7 开放防火墙端口 命令

    linux 安装禅道链接:  https://www.cnblogs.com/maohuidong/p/9750202.html CentOS 7 开放防火墙端口 命令 链接:https://www. ...

  9. 在新安装的Linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所有不存在/etc/sysconfig/iptables文件。

    原因:在新安装的Linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所有不存在/etc/sysconfig/iptables文件. 解决办法: .随便写一条iptables命令配 ...

随机推荐

  1. jQuery EasyUI简介

    jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件. jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助we ...

  2. Spring Cloud(一)服务的注册与发现(Eureka)

    Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集 ...

  3. Jmeter服务器压力测试使用说明

    Jmeter服务器压力测试使用说明 Apache JMeter是Apache组织开发的基于Java的压力测试工具. 官方地址:http://jmeter.apache.org/download_jme ...

  4. OSI网络通信工作流程的标准化 ----- 理论

    OSI 七层模型 1 应用层 [提供用户服务,具体功能由特定的程序而定] 2 表示层 [数据的压缩优化,加密] 3 会话层 [建立应用级的连接,选择传输服务] 4 传输层 [提供不同的传输服务,流量控 ...

  5. maskrcnn-benchmark错误:ImportError: cannot import name rnn_compat

    错误: from apex import amp File "build/bdist.linux-x86_64/egg/apex/__init__.py", line 5, in ...

  6. linux在配置菜单中添加选项

  7. XXX_initcall()函数分析

    1. 先看这些宏的定义(定义在文件include/linux/init.h中) #define pure_initcall(fn) __define_initcall("0",fn ...

  8. 不创建父窗体的情况下他其他窗体的信息显示在第一个打开的窗体!(winfrom)

    公司使用vs2008做的东西,用vs2017都打不开了(编译错误) 叫我更新一下,我看了一下,08的项目 和 winform 差不多  如何就用winfrom来做了 (winform  很久没碰了,, ...

  9. 构建之法第二次作业【使用git和Vs实现四则运算】

    [相关信息] Q A GIT地址 git地址 GIT用户名 Lin-000 学号后五位 62501 博客地址 博客地址 作业链接 此次作业链接 1.项目需求 程序接收一个命令行参数 n,然后随机产生 ...

  10. Kotlin环境搭建---Hello World

    对于亲爹都已经宣布它的重要性了,那做为搞安卓的自己有理由去拒绝它么?当然目前国内真正在商业中去采用Kotlin去开发项目的应该不多,但是!这肯定会被普及的,所以为了走在时代的前言,接下来会从基础语法开 ...