firewall-cmd是firewalld防火墙自带的字符管理工具,可以用来设置firewalld防火墙的各种规则
firewalld防火墙规则分为两种状态:
在使用firewall-cmd命令管理防火墙时,需要添加为永久生效的规则需在配置规则时添加–permanent选项(否则所有命令都是作用于runtime,运行时配置)
如果让永久生效规则立即覆盖当前规则生效使用,还需要使用firewall-cmd --reload命令重新加载防火墙配置
防火墙进程操作
[root@localhost ~]# systemctl 选项 firewalld
选项:
stop:关闭
start:开启
restart:重启
status:状态
防火墙管理操作
查询firewalld状态
[root@localhost ~]# systemctl status firewalld.service '//查看防火墙状态'
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-12-09 11:45:18 CST; 2h 46min ago
...省略内容
[root@localhost ~]# firewall-cmd --stat '//查看防火墙状态'
running
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
获取预定义信息
查看预定义的区域
[root@localhost ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work '//默认网络区域'
查看预定义的服务
[root@localhost ~]# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc ...省略内容
查看预定义的ICMP类型
[root@localhost ~]# firewall-cmd --get-icmptypes
address-unreachable bad-header communication-prohibited destination-unreachable echo-...省略内容
显示网络连接或接口的默认区域
[root@localhost ~]# firewall-cmd --get-default-zone
public
设置网络连接或接口的默认区域为internal
[root@localhost ~]# firewall-cmd --set-default-zone=internal
success
显示已激活的所有区域
[root@localhost ~]# firewall-cmd --get-active-zones
internal
interfaces: ens33
激活的条件:区域至少关联一个接口或一个源地址/网段
显示ens33接口绑定的区域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
internal
为ens33接口绑定work区域
[root@localhost ~]# firewall-cmd --zone=work --add-interface=ens33
The interface is under control of NetworkManager, setting zone to 'work'.
success
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
work
为work区域更改绑定的网络接口ens33
[root@localhost ~]# firewall-cmd --zone=work --change-interface=ens33
The interface is under control of NetworkManager and already bound to 'work'
The interface is under control of NetworkManager, setting zone to 'work'.
success
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
work
为work区域删除绑定的网络接口ens33
[root@localhost ~]# firewall-cmd --zone=work --remove-interface=ens33
The interface is under control of NetworkManager, setting zone to default.
success
查询work区域中是否包含接口ens33
[root@localhost ~]# firewall-cmd --zone=work --query-interface=ens33
no
显示区域及其规则
显示左右区域及其规则
[root@localhost ~]# firewall-cmd --list-all-zones
...省略内容
显示internal区域的所有规则
[root@localhost ~]# firewall-cmd --zone=internal --list-all
...省略内容
显示默认区域的所有规则
[root@localhost ~]# firewall-cmd --list-all
...省略内容
命令选项 | 说明 |
---|---|
–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 | 显示所有指定区域的所有规则 |
显示internal区域内允许访问的所有服务
[root@localhost ~]# firewall-cmd --zone=internal --list-services
ssh mdns samba-client dhcpv6-client
为public区域设置允许访问SMTP服务☆☆☆☆☆☆☆
[root@localhost ~]# firewall-cmd --zone=public --add-service=smtp
success
删除internal区域中的SSH服务
[root@localhost ~]# firewall-cmd --zone=internal --remove-service=ssh
success
查询internal区域中是否启用了SSH服务
[root@localhost ~]# firewall-cmd --zone=internal --query-service=ssh
no
命令选项 | 说明 |
---|---|
[–zone=区域类型] --list-services | 显示指定区域内允许访问的所有服务 |
[–zone=区域类型] --add-service=服务 | 为指定区域设置允许访问的某项服务 |
[–zone=区域类型] --remove-service=服务 | 删除指定区域已设置的允许访问的某项服务 |
[–zone=区域类型] --query-service=服务 | 查询指定区域中是否启用了某项服务 |
显示internal区域内允许访问的所有端口号
[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
命令选项 | 说明 |
---|---|
[–zone=区域类型] --list-ports | 显示指定区域内允许访问的所有端口号 |
[–zone=区域类型] --add-port=端口号[-端口号]/协议 [–timeout=时间] | 启用区域端口和协议组合,可选配置超时时间 |
[–zone=区域类型] --remove-port=端口号[-端口号]/协议] | 禁用区域端口和协议组合 |
[–zone=区域类型]==query-port=端口号[-端口号]/协议] | 查询区域中是否启用了端口和协议组合 |
显示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
操作命令选项 | 说明 |
---|---|
[–zone=区域类型] --list-icmp-blocks | 显示指定区域内阻塞的所有ICMP类型 |
[–zone=区域类型] --add-icmp-block=ICMP类型 | 为指定区域设置阻塞的某项ICMP类型 |
[–zone=区域类型] --remove-icmp-block=ICMP类型 | 删除指定区域已阻塞的某项ICMP类型 |
[–zone=区域类型] --query-icmp-block=ICMP类型 | 查询指定区域的ICMP阻塞功能 |
VMware软件
centos7虚拟机,作为服务器,IP地址为192.168.197.139
需要安装SSH和Apache服务
centos7虚拟机,作为客户机,IP地址为192.168.197.140
[root@localhost ~]# firewall-cmd --get-active-zones
public
interfaces: ens33
[root@localhost ~]# firewall-cmd --permanent --zone=work --add-source=192.168.197.140
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --add-source=192.168.197.141
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --get-active-zone
work
sources: 192.168.197.140
internal
sources: 192.168.197.141
public
interfaces: ens33
'//配置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=dhcpv6-client
success
[root@localhost ~]# firewall-cmd --permanent --zone=internal --remove-service=samba-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
[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 ~]#
'//配置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 ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# netstat -ntap | grep 80
tcp 0 36 192.168.197.139:22 192.168.197.1:49466 ESTABLISHED 1580/sshd: root@pts
tcp6 0 0 :::80 :::* LISTEN 3896/httpd
'//IP地址192.168.197.140的客户机'
[root@localhost ~]# ping 192.168.197.139
PING 192.168.197.139 (192.168.197.139) 56(84) bytes of data.
From 192.168.197.139 icmp_seq=1 Destination Host Prohibited
From 192.168.197.139 icmp_seq=2 Destination Host Prohibited
From 192.168.197.139 icmp_seq=3 Destination Host Prohibited
^C
--- 192.168.197.139 ping statistics ---
3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2001ms
'//IP地址192.168.197.141的客户机'
[root@localhost ~]# ping 192.168.197.139
PING 192.168.197.139 (192.168.197.139) 56(84) bytes of data.
From 192.168.197.139 icmp_seq=1 Destination Host Prohibited
From 192.168.197.139 icmp_seq=2 Destination Host Prohibited
From 192.168.197.139 icmp_seq=3 Destination Host Prohibited
From 192.168.197.139 icmp_seq=4 Destination Host Prohibited
^C
--- 192.168.197.139 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3001ms
'//IP地址为192.168.197.140的主机'
[root@localhost ~]# ssh 192.168.197.139
ssh: connect to host 192.168.197.139 port 22: No route to host
'//IP地址为192.168.197.141的主机'
[root@localhost ~]# ssh 192.168.197.139
The authenticity of host '192.168.197.139 (192.168.197.139)' 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 '192.168.197.139' (ECDSA) to the list of known hosts.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Last failed login: Mon Dec 9 17:40:09 CST 2019 from 192.168.197.141 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Dec 9 16:39:50 2019
[root@localhost ~]# exit
logout
Connection to 192.168.197.139 closed.