返回列表 发帖

CentOS8防火墙(firewalld)

CentOS5/6的防火墙叫netfilter,CentOS7/8的防火墙叫firewalld


查看操作系统的版本:
图片1.png


查看防火墙firewalld的版本:
[root@centos8 ~]# firewall-cmd --version
0.8.0


查看区域信息:  (默认zone就是 public
[root@centos8 ~]# firewall-cmd --get-active-zones
public
  interfaces: ens160


查看指定网卡接口所属区域:
[root@centos8 ~]# firewall-cmd --get-zone-of-interface=ens160
public



查看firewalld所有打开的服务:
[root@centos8 ~]# firewall-cmd --zone=public --list-services
cockpit dhcpv6-client ssh
注释: cockpit、dhcpv6-client、ssh 是默认就有的,这些服务的默认端口是可以被访问。



查看firewalld所有打开的端口:
firewall-cmd --zone=public --list-ports
图片1.png



查看firewalld的当前配置信息:(firewalld的初始状态)
firewall-cmd --list-all
图片2.png


查看firewalld的配置文件:(firewalld的初始状态)
[root@centos8 ~]# cat /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="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="cockpit"/>
</zone>



打开一个TCP端口:(TCP 25)
firewall-cmd --zone=public --add-port=25/tcp --permanent
注释: --permanent 永久生效,没有此参数服务器重启后失效

重新加载firewalld的配置:
firewall-cmd --reload
图片4.png


查看firewalld所有打开的端口:
firewall-cmd --zone=public --list-ports
图片5.png





一次性打开两个TCP端口:(TCP 110、143)
[root@centos8 ~]# firewall-cmd --zone=public --add-port={110/tcp,143/tcp} --permanent
success
[root@centos8 ~]# firewall-cmd --reload
success


查看firewalld所有打开的端口:
firewall-cmd --zone=public --list-ports
图片6.png



查看单个TCP端口是否开启:
[root@centos8 ~]# firewall-cmd --zone=public --query-port=25/tcp
yes

[root@centos8 ~]# firewall-cmd --zone=public --query-port=80/tcp
no



删除一个TCP端口:
[root@centos8 ~]# firewall-cmd --zone=public --remove-port=25/tcp --permanent
success
[root@centos8 ~]# firewall-cmd --reload
success


查看防火墙所有打开的端口:
firewall-cmd --zone=public --list-ports
图片7.png





打开连续的UDP端口、TCP端口:
firewall-cmd --zone=public --add-port=800-880/udp --permanent
firewall-cmd --zone=public --add-port=900-990/tcp --permanent
firewall-cmd --reload


查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
110/tcp 143/tcp 800-880/udp 900-990/tcp



删除连续的UDP端口、TCP端口:
firewall-cmd --zone=public --remove-port=800-880/udp --permanent
firewall-cmd --zone=public --remove-port=900-990/tcp --permanent
firewall-cmd --reload


查看firewalld所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
110/tcp 143/tcp





firewalld打开服务(HTTP):(相当于打开TCP 80端口)
[root@centos8 ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@centos8 ~]# firewall-cmd --reload
success


查看firewalld所有打开的服务:
[root@centos8 ~]# firewall-cmd --zone=public --list-services
cockpit dhcpv6-client http ssh

笺注:假如HTTP的端口不是默认的TCP 80,客户端则不能访问了;需要另外在firewalld打开与HTTP对应的TCP端口。


此时直接检测TCP 80端口是否开启的话,会如下显示,但不会影响客户端访问HTTP:
[root@centos8 ~]# firewall-cmd --zone=public --query-port=80/tcp
no





删除一个服务:
[root@centos8 ~]# firewall-cmd --zone=public --remove-service=cockpit --permanent
success
[root@centos8 ~]# firewall-cmd --reload
success


查看firewalld所有打开的服务:
[root@centos8 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client http ssh


查看firewalld的配置文件:
[root@centos8 ~]# cat /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="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="http"/>
  <port port="110" protocol="tcp"/>
  <port port="143" protocol="tcp"/>
</zone>

可以在配置文件里修改,然后重新加载firewalld的配置:
[root@centos8 ~]# firewall-cmd --reload
success





相关文章:
CentOS8_NFS共享存储服务
CentOS8使用Cockpit管理服务器

CentOS8_安装与配置邮件服务器(Postfix+dovecot)
使用Navicat远程管理MySQL8.0

CentOS8_vsftpd匿名用户
CentOS8_PureFTPd虚拟用户验证
CentOS8_lnmp1.7_单独安装数据库(MariaDB)

CentOS8防火墙(netfilter)
CentOS8基本命令
最小化安装CentOS8
Zabbix使用SNMP监控CentOS8/Redhat8

CentOS8_firewalld+Nginx
CentOS7.8_firewalld+SSH

返回列表