firewall 防火墙服务简述
1、Centos7
默认的防火墙是 firewall
===Centos6 iptables
2、firewal
l 使用更加方便、功能也更加强大一些
3、firewalld 服务引入了一个信任级别的概念来管理与之相关联的连接与接口。它支持 ipv4
与 ipv6
,并支持网桥,采用 firewall-cmd (command)
或 firewall-config (gui)
来动态的管理 kernel netfilter
的临时或永久的接口规则,并实时生效而无需重启服务。
firewall 防火墙安装
1.需要注意的是某些系统已经自带了 firewal l
的,如果查看版本没有找到,则可以进行 yum
安装
2.安装指令:
[root@firewalld ~]# yum -y install firewalld
3.查看 firewall 版本
[root@firewalld ~]# firewall-cmd --version
0.6.3
firewalld 服务基本使用
#查看防火墙状态
[root@firewalld ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2020-05-13 15:22:56 CST; 1min 45s ago
Docs: man:firewalld(1)
Main PID: 6129 (firewalld)
CGroup: /system.slice/firewalld.service
└─6129 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
May 13 15:22:56 firewalld systemd[1]: Starting firewalld - dynamic firewall.....
May 13 15:22:56 firewalld systemd[1]: Started firewalld - dynamic firewall ...n.
May 13 15:22:56 firewalld firewalld[6129]: WARNING: AllowZoneDrifting is ena....
Hint: Some lines were ellipsized, use -l to show in full.
#关闭防火墙,停止 firewall 服务
[root@firewalld ~]# systemctl stop firewalld
#开启防火墙,启动 firewall 服务
[root@firewalld ~]# systemctl start firewalld
#重启防火墙,重启 firewall 服务
[root@firewalld ~]# systemctl restart firewalld
#查看 firewall 服务是否开机启动
[root@firewalld ~]# systemctl is-enabled firewalld
disabled
#开机时自动启动 firewall 服务
[root@firewalld ~]# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
#开机时自动禁用 firewall 服务
[root@firewalld ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
firewalld-cmd 防护墙命令使用
上面所说的 firewall 可以看成整个防火墙服务,而 firewall-cmd 可以看成是其中的一个功能,可用来管理端口
1.查看 firewall-cmd 状态
#即查看 `firewall` 防火墙程序是否正在运行: `firewall-cmd --state`
[root@firewalld ~]# firewall-cmd --state
running
2.查看已打开的所有端口
[root@firewalld ~]# firewall-cmd --zone=public --list-ports
3.开启指定端口
#开启防火墙,无法访问apache
[root@firewalld ~]# systemctl start firewalld
#开启80端口 --permanent 永久生效,没有此参数重启后失效
[root@firewalld ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
#重新加载 firewall,修改配置后,必须重新加载才能生效
[root@firewalld ~]# firewall-cmd --reload
success
#开到80端口已开启
[root@firewalld ~]# firewall-cmd --zone=public --list-ports
80/tcp
4.关闭指定端口
#查看已开启的端口
[root@firewalld ~]# firewall-cmd --zone=public --list-ports
80/tcp
#关闭80端口 --permanent 表示永久生效,没有此参数重启后失效
[root@firewalld ~]# firewall-cmd --zone=public --remove-port=80/tcp --permanent success
[root@firewalld ~]# firewall-cmd --reload
success
#查看已打开的端口
[root@firewalld ~]# firewall-cmd --zone=public --list-ports
[root@firewalld ~]#
public.xml 文件修改防火墙端口(直接修改也是可以的)
#已开启端口列表是空的时候 配置文件也没有
[root@firewalld ~]# firewall-cmd --zone=public --list-ports
[root@firewalld 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"/>
</zone>
#添加一个端口时 配置文件也有
[root@firewalld zones]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@firewalld 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"/>
</zone>
注意事项(阿里云)
1、CentOS 7.7 系统防火墙明明开启了指定的端口,tomcat服务器端口也指定正确,启动没有任何问题,最后从浏览器访问的时候,却只有80端口有效,其余的端口全部访问失败
[root@aly bin]# firewall-cmd --zone=public --list-ports
8080/tcp 80/tcp
2、最后原因居然是因为系统是阿里云服务器,而它的后台为了安全,封掉了其它端口的访问,所以即使防火墙修改了也没用,解决办法是登录阿里云服务器后台,修改它的安全组策略即可。