Linux--Firewalld基本命令操作(防火墙状态查看、重新配置、区域操作、服务操作、端口操作、阻塞协议操作以及实验配置)

前言

  • firewall-cmd是firewalld防火墙自带的字符管理工具,可以用来设置firewalld防火墙的各种规则

  • firewalld防火墙规则分为两种状态:

    • 一种是runtime,指正在运行生效的状态,在runtime状态添加新的防火墙规则,这些规则会立即生效,但是重新加载防火墙配置或者再重启系统后这些规则将会失效
    • 另一种是permanent,指永久生效的状态,在permanent状态添加新的防火墙规则,这些规则不会马上生效,需要重新加载防火墙配置或者重启系统后生效
  • 在使用firewall-cmd命令管理防火墙时,需要添加为永久生效的规则需在配置规则时添加–permanent选项(否则所有命令都是作用于runtime,运行时配置)

    如果让永久生效规则立即覆盖当前规则生效使用,还需要使用firewall-cmd --reload命令重新加载防火墙配置

一:启动、停止、查看 firewalld 服务

1.1:firewalld防火墙维护命令

  • 防火墙进程操作
[root@localhost ~]# systemctl 选项 firewalld
选项:
    stop:关闭
    start:开启
    restart:重启
    status:状态

防火墙管理操作

  • firewalld-cmd命令
    • 支持全部防火墙特性
    • 对于状态和查询模式,命令只返回状态,没有其他输出
    • –permanent(永久的)参数:携带该参数表示永久配置,否则表示运行时配置
    • [–zone=]选项:不懈怠此选项表示针对默认区域操作,否则针对指定区域操作

1.2:启动、停止、查看命令

  • 在安装 CentOS7 系统时,会自动安装 firewalld 和图形化工具 firewall-config。执行 以下命令可以启动 firewalld 并设置为开机自启动状态。
[root@localhost ~]# systemctl start firewalld		 //启动 firewalld 
[root@localhost ~]# systemctl enable firewalld           //设置 firewalld 为开机自启动 
  • 如果 firewalld 正在运行,通过 systemctl status firewalld 或 firewall-cmd 命令 可以查看其运行状态
[root@localhost ~]# systemctl status firewalld
[root@localhost ~]# firewall-cmd --state 
  • 如果想要禁用 firewalld,执行以下命令即可实现。
[root@localhost ~]# systemctl stop firewalld 		//停止 firewalld 
[root@localhost ~]# systemctl disable firewalld		//设置 firewalld 开机不自启动 

二:firewalld防火墙重载配置命令

2.1:重新加载firewalld的配置

  • firewall-cmd --reload
[root@localhost ~]# firewall-cmd --reload
success
  • firewall-cmd --complete-reload
[root@localhost ~]# firewall-cmd --complete-reload
success

状态信息将会丢失,多用于处理防火墙出现问题时

  • systemctl restart firewalld
[root@localhost ~]# systemctl restart firewalld.service 

2.2:获取预定义信息

firewall-cmd 预定义信息主要包括三种:可用的区域、可用的服务以及可用的 ICMP 阻 塞类型,具体的查看命令如下所示

[root@localhost ~]# firewall-cmd --get-zones  	//显示预定义的区域 

[root@localhost ~]# firewall-cmd --get-service  	//显示预定义的服务 

[root@localhost ~]# firewall-cmd --get-icmptypes	//显示预定义的 ICMP 类型 
firewall-cmd --get-icmptypes 命令的执行结果中各种阻塞类型的含义分别如下所示。 
Ø destination-unreachable:目的地址不可达。 
Ø echo-reply:应答回应(pong)。 
Ø parameter-problem:参数问题。 
Ø redirect:重新定向。 
Ø router-advertisement:路由器通告。 
Ø router-solicitation:路由器征寻。 
Ø source-quench:源端抑制。 
Ø time-exceeded:超时。 
Ø timestamp-reply:时间戳应答回应。 
Ø timestamp-request:时间戳请求。 

三:firewalld区域操作命令

3.1:对默认区域操作

  • 显示当前系统中的默认区域
[root@localhost ~]# firewall-cmd --get-default-zone

public 
  • 设置网络连接或接口的默认区域
[root@localhost ~]# firewall-cmd --set-default-zone=work
success
[root@localhost ~]# firewall-cmd --get-default-zone
work
  • 显示默认区域的所有规则
[root@localhost ~]# firewall-cmd --list-all
public (active) 
target: default 
icmp-block-inversion: no 
interfaces: ens33 
  • 显示网络接口 ens33 对应区域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
work
  • 为指定接口绑定区域
[root@localhost ~]# firewall-cmd --zone=internal --add-interface=ens36
The interface is under control of NetworkManager, setting zone to 'internal'.
success
  • 将网络接口 ens33 对应区域更改为 internal 区域
[root@localhost ~]# firewall-cmd --zone=internal --change-interface=ens33
The interface is under control of NetworkManager, setting zone to 'internal'. 
success 
[root@localhost ~]# firewall-cmd --zone=internal --list-interfaces
ens33 
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
internal 
  • 为指定的区域删除绑定的网络接口
[root@localhost ~]# firewall-cmd --zone=internal --remove-interface=ens33
The interface is under control of NetworkManager, setting zone to default.
success
  • 显示所有激活区域
[root@localhost ~]# firewall-cmd --get-active-zones 
work
  interfaces: ens33
internal
  interfaces: ens36
  • 显示指定接口绑定的区域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens36
internal

3.2:区域操作命令总结

命令选项 说明
–get-default-zone 显示网络连接或接口的默认区域
–set-default-zone=区域类型 设置网络连接或接口的默认区域
–get-active-zones 显示已激活的所有区域
–get-zone-of-interface=接口(网卡) 显示指定接口绑定的区域
–zone=区域类型 --add-interface=接口(网卡) 为指定接口绑定区域
–zone=区域类型 --change-interface=接口(网卡) 为指定的区域更改绑定的网络接口
–zone=区域类型 --remove-interface=接口(网卡) 为指定的区域删除绑定的网络接口
–query-interface=接口(网卡) 查询区域中是否包含某接口
–list-all-zones 显示所有区域及其规则
[–zone=区域类型] --list-all 显示所有指定区域的所有规则

四:firewalld服务操作命令

  • 显示指定区域区域内允许访问的所有服务
[--zone=<zone>] --list-services
[root@localhost ~]# firewall-cmd --zone=work --list-services 
ssh dhcpv6-client
  • 为指定区域设置允许访问的某项服务
[--zone=<zone>] --add-service=<service> 
[root@localhost ~]# firewall-cmd --zone=work --add-service=http
success
  • 删除指定区域已设置的允许访问的某项服务
[--zone=<zone>] --remove-service=<service>
[root@localhost ~]# firewall-cmd --zone=work --remove-service=http
success
  • 查询internal区域中是否启用了SSH服务
[root@localhost ~]# firewall-cmd --zone=internal --query-service=ssh
no

4.1:操作命令总结

命令选项 说明
[–zone=区域类型] --list-services 显示指定区域内允许访问的所有服务
[–zone=区域类型] --add-service=服务 为指定区域设置允许访问的某项服务
[–zone=区域类型] --remove-service=服务 删除指定区域已设置的允许访问的某项服务
[–zone=区域类型] --query-service=服务 查询指定区域中是否启用了某项服务

五:Firewalld端口操作命令

  • 显示指定区域内允许访问的所有端口号
[root@localhost ~]# firewall-cmd --zone=internal --list-ports
  • 启用internal区域22端口的TCP协议组合
 [root@localhost ~]# firewall-cmd --zone=internal --add-port=22/tcp --timeout=5m
  success

  '//--timeout=5m:表示五分钟后删除该端口,多用于测试'
  • 禁用internal区域22端口的TCP协议组合
[root@localhost ~]# firewall-cmd --zone=internal --remove-port=22/tcp
success
  • 查询internal区域中是否启用了22端口和TCP协议组合
[root@localhost ~]# firewall-cmd --zone=internal --query-port=22/tcp
no

5.1:操作命令总结

命令选项 说明
[–zone=区域类型] --list-ports 显示指定区域内允许访问的所有端口号
[–zone=区域类型] --add-port=端口号[-端口号]/协议 [–timeout=时间] 启用区域端口和协议组合,可选配置超时时间
[–zone=区域类型] --remove-port=端口号[-端口号]/协议] 禁用区域端口和协议组合
[–zone=区域类型]==query-port=端口号[-端口号]/协议] 查询区域中是否启用了端口和协议组合

六:Firewalld阻塞ICMP操作命令

  • 显示work区域内阻塞的所有ICMP类型
[root@localhost ~]# firewall-cmd --zone=work --list-icmp-blocks
  • 为work区域设置阻塞echo-reply类型的ICMP
[root@localhost ~]# firewall-cmd --zone=work --add-icmp-block=echo-reply 
success
  • 删除work区域已阻塞的echo-reply类型的ICMP
[root@localhost ~]# firewall-cmd --zone=work --remove-icmp-block=echo-reply
success
  • 查询work区域的echo-request类型的ICMP是否阻塞
[root@localhost ~]# firewall-cmd --zone=work --query-icmp-block=echo-request 
no

6.1:操作命令总结

操作命令选项 说明
[–zone=区域类型] --list-icmp-blocks 显示指定区域内阻塞的所有ICMP类型
[–zone=区域类型] --add-icmp-block=ICMP类型 为指定区域设置阻塞的某项ICMP类型
[–zone=区域类型] --remove-icmp-block=ICMP类型 删除指定区域已阻塞的某项ICMP类型
[–zone=区域类型] --query-icmp-block=ICMP类型 查询指定区域的ICMP阻塞功能

七:实验

7.1:环境

  • VMware软件

  • centos7虚拟机,作为服务器,IP地址为20.0.0.47

    需要安装SSH和Apache服务

  • centos7虚拟机,作为客户机,IP地址为20.0.0.48

7.2:实验目标

  • 禁止主机ping服务器
  • 只允许20.0.0.48主机访问Apache服务
  • 只允许20.0.0.49主机访问TCP/22端口

7.3:实验步骤

7.3.1:绑定区域

[root@localhost ~]# firewall-cmd --get-active-zones
public
  interfaces: ens33
[root@localhost ~]# firewall-cmd --permanent --zone=work --add-source=20.0.0.48
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --add-source=20.0.0.49
success
[root@localhost ~]# firewall-cmd --reload 
success
[root@localhost ~]# firewall-cmd --get-active-zones 
work
  sources: 20.0.0.48
internal
  sources: 20.0.0.49
public
  interfaces: ens33

7.3.2:配置服务

配置work区域服务
[root@localhost ~]# firewall-cmd --permanent --zone=work --remove-service=ssh
success
[root@localhost ~]# firewall-cmd --permanent --zone=work --remove-service=dhcpv6-client 
success
[root@localhost ~]# firewall-cmd --permanent --zone=work --add-service=http
success
[root@localhost ~]# firewall-cmd --permanent --zone=work --list-services 
http
配置internal区域服务
[root@localhost ~]# firewall-cmd --zone=internal --list-services
ssh mdns samba-client dhcpv6-client
[root@localhost ~]# firewall-cmd --permanent --zone=internal --remove-service=mdns
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --remove-service=ssh
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --remove-service=samba-client 
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --remove-service=dhcpv6-client 
success
[root@localhost ~]#  firewall-cmd --permanent --zone=internal --list-service
配置public区域服务
[root@localhost ~]# firewall-cmd --permanent --zone=public --list-services
ssh dhcpv6-client
[root@localhost ~]# firewall-cmd --permanent --zone=public --remove-service=dhcpv6-client
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --remove-service=ssh
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --list-services

7.3.3:配置端口

[root@localhost ~]# firewall-cmd --permanent --zone=internal --list-ports
[root@localhost ~]# firewall-cmd --permanent --zone=internal --add-port=22/tcp
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --list-ports
22/tcp
[root@localhost ~]# 

7.3.4:配置ICMP阻塞

work区域ICMP阻塞
[root@localhost ~]# firewall-cmd --permanent --zone=work --list-icmp-blocks
[root@localhost ~]# firewall-cmd --permanent --zone=work --add-icmp-block=echo-request
success
[root@localhost ~]# firewall-cmd --permanent --zone=work --list-icmp-blocks
echo-request
internal区域ICMP阻塞
[root@localhost ~]# firewall-cmd --permanent --zone=internal --list-icmp-blocks
[root@localhost ~]#  firewall-cmd --permanent --zone=internal --add-icmp-block=echo-request
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --list-icmp-blocks
echo-request
public区域ICMP阻塞
[root@localhost ~]# firewall-cmd --permanent --zone=public --list-icmp-blocks
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-icmp-block=echo-request
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --list-icmp-blocks
echo-request
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# 

7.3.5:重载配置

[root@localhost ~]# firewall-cmd --reload
success

7.3.6:安装Apache服务

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# netstat -ntap | grep 80

7.4:实验结果

20.0.0.48ping20.0.0.47
在这里插入图片描述

20.0.0.49ping20.0.0.47
在这里插入图片描述

7.4.1:访问服务器Apache服务

  • 只有20.0.0.48主机可以访问
    在这里插入图片描述

在这里插入图片描述

//IP地址为20.0.0.48的主机
[root@localhost ~]# ssh 20.0.0.47
ssh: connect to host 20.0.0.47 port 22: No route to host
//IP地址为20.0.0.49的主机
[root@localhost ~]# ssh 20.0.0.47
The authenticity of host '20.0.0.47 (20.0.0.47)' can't be established.
ECDSA key fingerprint is SHA256:UhiQeQeIoKaH1ogewTdbaZIldXmr3dxKoD0/RN0jhcU.
ECDSA key fingerprint is MD5:95:ac:b8:fe:9e:01:50:3c:56:c9:e3:aa:28:ee:1c:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '20.0.0.47' (ECDSA) to the list of known hosts.
root@20.0.0.47's password: 
Permission denied, please try again.
root@20.0.0.47's password: 
Permission denied, please try again.
root@20.0.0.47's password: 
Last failed login: Mon Dec  7 19:39:50 CST 2020 from 20.0.0.49 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Dec  7 19:39:50 2019
[root@localhost ~]# exit
logout
Connection to 20.0.0.47 closed

.

猜你喜欢

转载自blog.csdn.net/m0_47219942/article/details/107718070