Firewalld管理防火墙相关命令
参数 | 作用 |
–get-default-zone | 查询默认的区域名称。 |
–set-default-zone=<区域名称> | 设置默认的区域,永久生效。 |
–get-zones | 显示可用的区域。 |
–get-services | 显示预先定义的服务。 |
–get-active-zones | 显示当前正在使用的区域与网卡名称。 |
–add-source= | 将来源于此IP或子网的流量导向指定的区域。 |
–remove-source= | 不再将此IP或子网的流量导向某个指定区域。 |
–add-interface=<网卡名称> | 将来自于该网卡的所有流量都导向某个指定区域。 |
–change-interface=<网卡名称> | 将某个网卡与区域做关联。 |
–list-all | 显示当前区域的网卡配置参数,资源,端口以及服务等信息。 |
–list-all-zones | 显示所有区域的网卡配置参数,资源,端口以及服务等信息。 |
–add-service=<服务名> | 设置默认区域允许该服务的流量。 |
–add-port=<端口号/协议> | 允许默认区域允许该端口的流量。 |
–remove-service=<服务名> | 设置默认区域不再允许该服务的流量。 |
–remove-port=<端口号/协议> | 允许默认区域不再允许该端口的流量。 |
–reload | 让“永久生效”的配置规则立即生效,覆盖当前的。 |
1.查看防火墙的状态
[root@localhost HMK]# firewall-cmd --state 查看防火墙的运行状态
not running
[root@localhost HMK]# systemctl status firewalld.service 查看防火墙服务是否开启
● 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 HMK]#
2.开启防火墙
开启防火墙(重启服务器后又会重新关闭)
[root@localhost HMK]# systemctl start firewalld.service
设置防火墙开机自动启动
[root@localhost HMK]# systemctl enable firewalld.service
重启防火墙
[root@localhost HMK]# systemctl restart firewalld.service
3.查看 firewalld 服务当前所使用的区域
[root@localhost HMK]# firewall-cmd --get-default-zone
public
[root@localhost HMK]#
4.为默认区域开启端口(允许该端口的流量)
[root@localhost HMK]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost HMK]#
命令含义:
–zone #指定区域
–add-port=80/tcp #添加端口,格式为:&lt;端口号/协议&gt;
–permanent #永久生效,没有此参数重启后失效
非永久生效的端口
[root@localhost HMK]# firewall-cmd --zone=public --add-port=3002/tcp
success
查看开放的端口时,需要区分是否为永久生效
[root@localhost HMK]# firewall-cmd --permanent --list-port 加上 -- permanent ,可以列出永久开放的端口
80/tcp
[root@localhost HMK]# firewall-cmd --list-port 未加 --permanent , 列出非永久开放的端口
3002/tcp
[root@localhost HMK]# firewall-cmd --permanent --zone=public --list-ports 查看开放端口时,也可以指定区域 --zone
80/tcp
[root@localhost HMK]#
需要注意的是,添加或删除端口后,需要重启防火墙!!!
5.关闭防火墙
关闭防火墙
[root@localhost HMK]# systemctl stop firewalld.service
设置防火墙关闭自启动
[root@localhost HMK]# systemctl disable firewalld.service
6.限制ip及端口访问
#限制某个ip访问
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="10.6.1.2" drop'
#查看当前zones
firewall-cmd --get-active-zones
#显示当前开放端口
firewall-cmd --zone=public --list-ports
#显示当前开放的服务
firewall-cmd --zone=public --list-service
#添加端口到永久区
firewall-cmd --permanent --zone=public --add-port=61080/tcp --add-port=61446/tcp
#添加服务到永久区
firewall-cmd --permanent --zone=public --add-service=samba
#限制IP端口访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="6379" accept"
#删除规则
firewall-cmd--permanent--remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept"
#删除打开的端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
#查询网络接口所属区域
firewall-cmd --get-zone-of-interface=eth0
#刷新配置,添加到永久区后必须刷新配置才能生效
firewall-cmd --reload

































