Centos7 firewall 基本操作

Stella981
• 阅读 461

Centos7 默认启用 firewall 替代原来的 iptables。firewall 与 iptables 一个明显的区别是: firewall 属于动态防火墙,它拥有运行时配置和永久配置选项,它支持允许服务或者应用程序直接添加防火墙规则的接口,而 iptables 属于静态防火墙,任何操作都需要重启 iptables 服务。

这里整理一些常用的 firewall 基本操作。

1. 查看 firewall 启动停止状态

#查看状态
systemctl status firewalld
#启动
systemctl start firewalld
#停止
systemctl stop firewalld

2. 查看开启端口

firewall-cmd --list-all

输出

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources: 
  services: dhcpv6-client ssh
  ports: 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules:

可以看到 ports 这个节点, 我这里只开启了80端口。

3. 增加需要开放的端口

firewall-cmd --permanent --zone=public --add-port=8080/tcp

--permanent: 永久增加

--zone=public: 增加到一个名字叫 public 的 zone 里面

--add-port: 增加tcp 8080端口

zone是什么意思? zone(网络区域)定义了网络连接的可信等级。这是一个 一对多的关系,这意味着一次连接可以仅仅是一个区域的一部分,而一个区域可以用于很多连接。

重启防火墙

[root@localhost ~]# systemctl restart firewalld
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources: 
  services: dhcpv6-client ssh
  ports: 80/tcp 8080/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

我们可以看到在 ports 节点已经添加了 8080 端口。在篇头我们已经讲到了 firewall 是动态防火墙,这里添加完端口需要重启服务是因为加了 --permanent。

这里我们加一个临时的 8088 端口

[root@localhost ~]# firewall-cmd --zone=public --add-port=8088/tcp
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources: 
  services: dhcpv6-client ssh
  ports: 80/tcp 8080/tcp 8088/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

可以只是临时增加端口不需要重启服务。

4.移除端口号

firewall-cmd --permanent --zone=public --remove-port=80/tcp

--remove-port: 移除端口号

[root@localhost ~]# firewall-cmd --permanent --zone=public --remove-port=80/tcp
success
[root@localhost ~]# systemctl restart firewalld
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources: 
  services: dhcpv6-client ssh
  ports: 8080/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules:

可以看到 80 端口已经被移除

5.firewall 配置文件

以上操作都是通过命令来实现的,其实最终都反应到它的配置文件。

打开 zone 下面的 public.xml 文件

vi /etc/firewalld/zones/public.xml

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>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.</description>
  <service name="dhcpv6-client"/>
  <service name="ssh"/>
  <port protocol="tcp" port="8080"/>
</zone>

可以看到刚才加的 8080 端口还是配置文件中。

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
Centos7中使用iptables,docker容器中实例,外网端口无法访问
因微信小程序需要https的域名访问方式,阿里云无法通过IP和端口绑定域名,只能通过IP进行绑定,故如果不想直接使用443端口,就只能进行端口映射了。这里我使用iptables。由于centos7默认是使用firewall作为防火墙,下面介绍如何将系统的防火墙设置为iptables。停止firewall systemctlstopfire
Wesley13 Wesley13
2年前
IPTABLES简介
iptables防火墙工作原理简介:iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防火墙基于内核编码实现,具有非常稳定的性能和高效率;   iptables属于“用户态”的防火墙管理体系。!(https://oscimg.oschina.net/oscnet/691dc3c1cdd75580e336
Stella981 Stella981
2年前
CentOS 7 安装 Oracle 12c 步骤
CentOS7安装Oracle12c步骤catoop2019092823:35:022170收藏4分类专栏:数据库版权本例操作系统版本:CentOS7.7、数据库版本:Oracle12c(12.2)安装步骤如下分解:1.关闭防火墙,禁止防火墙开机自启\关闭防火墙systemctlst
Stella981 Stella981
2年前
LVS+KeepAlived+Nginx高可用实现方案
文章目录概念LVSKeepAlived为什么要使用准备软件安装KeepAlived安装源码安装yum安装服务启动、重启、关闭安装ipvsadmnginx安装防火墙(iptables)防火墙配置(方式一)防火墙配置(方式二)配置nginx服务器(这里很重要!很
Stella981 Stella981
2年前
CentOS7防火墙firewalld操作
firewalldLinux上新用的防火墙软件,跟iptables差不多的工具。firewallcmd是firewalld的字符界面管理工具,firewalld是CentOS7的一大特性,最大的好处有两个:支持动态更新,不用重启服务;第二个就是加入了防火墙的zone概念。firewalld跟iptabl
Stella981 Stella981
2年前
Mycat防火墙配置:IP白名单和 SQL黑名单说明
server.xml文件    firewall标签用来定义防火墙;firewall下whitehost标签用来定义IP白名单,blacklist用来定义SQL黑名单。<firewall<whitehost<hostuser"mycat"host"127.0.0.1"</
Stella981 Stella981
2年前
CentOS 7.0如何将iptables作为防火墙(默认使用的是firewall作为防火墙)
1、直接关闭防火墙 systemctlstopfirewalld.service停止firewall systemctldisablefirewalld.service禁止firewall开机启动2、设置iptablesservice yumyinstalliptablesservices 如果要修改防
Stella981 Stella981
2年前
CentOS 6.5~7.x防火墙操作命令
网上搜集的防火墙操作命令,很实用CentOS6.5防火墙操作命令配置filter表防火墙清除预设表filter中的所有规则链的规则iptablesF清除预设表filter中使用者自定链中的规则iptablesX保存iptables配置servic
Stella981 Stella981
2年前
Linux iptables 设置
编辑/etc/sysconfig/iptables然后运行/sbin/serviceiptablesrestartnetstatanp|grep80|grepLISTEN防火墙规则只有在iptables服务运行的时候才能被激活。要手工启动服务,使用以下命令:/sbin/servic
胖大海 胖大海
1年前
Linux centos7 firewalld详细用法
iptables与firewalld的区别1.firewalld可以动态修改单条规则,动态管理规则集,允许更新规则而不破坏现有会话和连接。而iptables,在修改了规则后必须得全部刷新才可以生效;2.firewalld使用区域和服务而不是链式规则;3.firewalld默认是拒绝的,需要设置以后才能放行。而iptables默认是允许的,需要拒绝的才去限制;