查看firewall是否运行,下面两个命令都可以

systemctl status firewalld.service firewall-cmd —state

查看default zone和active zone

我们还没有做任何配置,default zone和active zone都应该是public

firewall-cmd —get-default-zone firewall-cmd —get-active-zones

查看当前开了哪些服务和端口

服务和端口是单独的

查看开放的服务

其实一个服务对应一个端口,每个服务对应/usr/lib/firewalld/services下面一个xml文件。

firewall-cmd —list-services

查看开放了哪些端口

sudo firewall-cmd --zone=public --list-ports

查看还有哪些服务可以打开

firewall-cmd —get-services

添加一个服务到firewalld

firewall-cmd —add-service=http //http换成想要开放的service

这样添加的service当前立刻生效,但系统下次启动就失效,可以测试使用。要永久开发一个service,加上 —permanent

firewall-cmd —permanent —add-service=http

Note1: Type –remove-service=http to deny the http service.
Note2: The firewall-cmd –reload command is necessary to activate the change. Contrary to the –complete-reload option, current connections are not stopped.
Note3: If you only want to temporarily add a service, don’t use the –permanent option and don’t reload the firewall configuration. If you reload the firewall configuration, you cancel all the operation.

如果要添加的端口并没有服务对应

就要新建一个服务,在/usr/lib/firewalld/services,随便拷贝一个xml文件到一个新名字,比如myservice.xml,把里面的

short改为想要名字(这个名字只是为了人来阅读,没有实际影响。重要的是修改 protocol和port。修改完保存。我的经验是这是要重启firewalld服务,systemctl restart firewalld.service,否则可能提示找不到刚才新建的service。然后把新建的service添加到firewalld

firewall-cmd —permanent —add-service=myservice

重启firewalld 生效

firewall-cmd —reload

直接开放端口

  1. [root@centos7 ~]# firewall-cmd --permanent --add-port=100/tcp
  2. success
  3. [root@centos7 ~]# firewall-cmd --reload
  4. success