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. java oop 基础

    1.如果将一个类打包,使用该类的时候,必须使用该类的全名,java编译器才能找到. 2.也可以使用import 导入这个包. 3.可以不需要import语句 直接使用 java.lang包中的类. 4 ...

  2. 我理解的epoll(一)——实现分析

    epoll项目中用了几次,但是对于其原理只是一知半解.我希望通过几篇blog能加深对她的理解. 我认为epoll是同步IO,因为他在调用epoll_wait时,内核在有I/O就绪前是阻塞的,虽然可以将 ...

  3. Linux目录结构以及一些常见操作

    本章内容: Linux 目录结构 远程服务器关机及重启时的注意事项 不要在服务器访问高峰运行高负载命令 远程配置防火墙时不要把自己踢出服务器 指定合理的密码规范并定期更新 合理分配权限 定期备份重要数 ...

  4. Logstash工作原理

    Logstash事件处理有三个阶段:inputs → filters → outputs.是一个接收,处理,转发日志的工具.支持系统日志,webserver日志,错误日志,应用日志,总之包括所有可以抛 ...

  5. ELK架构图示例

    这是最简单的一种ELK架构方式.优点是搭建简单,易于上手.缺点是Logstash耗资源较大,运行占用CPU和内存高.另外没有消息队列缓存,存在数据丢失隐患. 此架构由Logstash分布于各个节点上搜 ...

  6. LNMP原理

    在linux系统中,LNMP是目前网站主流架构之一,LNMP中L指linux系统平台,N是nginx指网站的前端程序,发布静态页面和调用外部程序解析动态页面,M是mysql指网站的后台数据库,P是PH ...

  7. springboot2.0入门(四)----mock模拟测试+单元测试

    一.本节主要记录模拟测试.单元测试: 二.mock 测试 1.1什么是Mock? 在面向对象程序设计中,模拟对象(英语:mock object,也译作模仿对象)是以可控的方式模拟真实对象行为的假的对象 ...

  8. 面试必会之ArrayList源码分析&手写ArrayList

    简介 ArrayList是我们开发中非常常用的数据存储容器之一,其底层是数组实现的,我们可以在集合中存储任意类型的数据,ArrayList是线程不安全的,非常适合用于对元素进行查找,效率非常高. 线程 ...

  9. IntelliJ 如何显示代码的代码 docs

    希望能够在 IntelliJ 代码上面显示方法的 docs. 如何进行显示? 你可以使用 Ctrl + Q 这个快捷键来查看方法的 Docs. https://blog.ossez.com/archi ...

  10. TTTTTTTTTTTT Gym 100818B Tree of Almost Clean Money 树连剖分+BIT 模板题

    Problem B Tree of Almost Clean Money Input File: B.in Output File: standard output Time Limit: 4 sec ...