Centos7防火墙基本操作
开关
- 查看防火墙状态:
systemctl status firewalld
- 开启:
systemctl start firewalld
- 关闭:
systemctl stop firewalld
- 重启:
systemctl restart firewalld
开放端口
- 查询80端口是否开放:
firewall-cmd --query-port=80/tcp
- 开放80端口:
firewall-cmd --permanent --add-port=80/tcp
- 移除端口:
firewall-cmd --permanent --remove-port=80/tcp
开机自启
- 查看是否开机启动:
systemctl list-unit-files | grep fire
- 开启开机自启:
systemctl enable firewalld
- 关闭开机自启:
systemctl disable firewalld
关于Centos 7的firewalld
- 官网给的结构图 https://firewalld.org/documentation/concepts.html
- firewalld的配置文件以xml格式为主(主配置文件firewalld.conf例外),他们有两个存储位置:
/etc/firewalld/*
. 用户配置文件,比如自己添加的端口规则就在这里面zones/public.xml
中可以找到./usr/lib/firewalld/*
. 系统预定义的配置文件.
- 所以如果
firewall-cmd
用不了的话,可以自己更改/etc/firewalld/zones/public.xml
配置文件也可以,比如添加端口,添加服务。下面是我的配置:
[root@iZh86z4fy5fpm3Z zones]# pwd
/etc/firewalld/zones
[root@iZh86z4fy5fpm3Z zones]# ls -l
total 8
-rw-r--r-- 1 root root 413 May 27 18:55 public.xml
-rw-r--r--. 1 root root 350 May 18 09:48 public.xml.old
[root@iZh86z4fy5fpm3Z zones]# cat 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"/>
<port protocol="tcp" port="80"/>
<service name="https"/>
<port protocol="tcp" port="8888"/>
</zone>
[root@iZh86z4fy5fpm3Z zones]#
提一下这个<service name="https"/>
其实对应的是/usr/lib/firewalld/services/https.xml
这个配置文件,最后改了记得重启防火墙。