firewall 简述
Centos7默认的防火墙是 firewall,替代了以前的 iptables
firewall使用更加方便、功能也更加强大一些
firewalld 服务引入了一个信任级别的概念来管理与之相关联的连接与接口。它支持 ipv4 与 ipv6,并支持网桥,采用 firewall-cmd (command) 或 firewall-config (gui) 来动态的管理 kernel netfilter 的临时或永久的接口规则,并实时生效而无需重启服务。
firewall 版本
查看版本:firewall-cmd --version
firewall 安装
- 像使用iptables一样,firewall同样需要安装
- 需要注意的是某些系统已经自带了firewall的,如果查看版本没有找到,则可以进行yun安装
- 安装指令: yum install firewalld
firewalld 基本使用
- firewall与iptables一样都是服务,所以可以使用systemctl服务管理工具来操作
查看防火墙状态
- 1、查看firewall服务状态: systemctl status firewalld
关闭防火墙
- 2、停止firewall服务:systemctl stop firewalld
开启防火墙
- 3、启动firewall服务:systemctl start firewalld
重启防火墙
- 重启firewall服务:systemctl restart firewalld
查看防火墙是否开机自启
- 4、查看firewall服务是否开机启动:systemctl is-enabled firewalld
开机自启防火墙
- 5、开机时启动firewall服务:systemctl enable firewalld.service
开机禁用防火墙
- 6、开机时禁用firewall服务:systemctl disable firewalld.service
firewalld-cmd 基本使用
1、上面所说的 firewall 可以看成整个防火墙服务,而firewall-cmd可以看成是其中的一个功能,可用来管理端口
查看 firewall-cmd 状态
- 查看firewall状态,即查看 firewall 防火墙程序是否正在运行: firewall-cmd --state
查看已打开端口
- 3、查看所有已经打开的端口: firewall-cmd --zone=public --list-ports
开启指定端口
4、开启一个端口:firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent 永久生效,没有此参数重启后失效)
5、重新加载firewall,修改配置后,必须重新加载才能生效:firewall-cmd --reload
[root@localhost ~]# firewall-cmd --zone=public --list-port
9876/tcp 8090/tcp 80/tcp 8080/tcp
[root@localhost ~]# firewall-cmd --zone=public --add-port=3307/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --zone=public --list-port
9876/tcp 8090/tcp 80/tcp 8080/tcp 3307/tcp
[root@localhost ~]#
关闭指定端口
关闭9876端口:firewall-cmd --zone=public --remove-port=9898/tcp --permanent(--permanent 表示永久生效,没有此参数重启后失效)
重新加载 firewall,修改配置后,必须重新加载才能生效:firewall-cmd --reload
[root@localhost ~]# firewall-cmd --zone=public --list-ports
9876/tcp 8090/tcp 80/tcp 8080/tcp
[root@localhost ~]# firewall-cmd --zone=public --remove-port=9876/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports
8090/tcp 80/tcp 8080/tcp
[root@localhost ~]#
修改文件操作端口
- firewall-cmd对端口的操作,如开放端口等信息,都放在在"/etc/firewall/zones/public.xml"中记录
- 所以直接修改此文件也是可以的
注意事项
如下所示,CentOS 7.2 Linux系统防火墙明明开启了指定的端口,tomcat服务器端口也指定正确,启动没有任何问题,最后从windows浏览器访问的时候,却只有80端口有效,其余的端口全部访问失败
最后原因居然是因为系统是阿里云服务器,而它的后台为了安全,封掉了其它端口的访问,所以即使防火墙修改了也没用,解决办法是登录阿里云服务器后台,修改它的安全组策略即可