CentOS 7 防火墙相关设置

89 阅读1分钟

CentOS 7 防火墙相关设置

1、开启、重启、关闭、firewalld.service服务

# 开启
service start firewalld
# 重启
service restart firewalld
# 关闭
service stop firewalld
# 设置开机启动
systemctl enable firewalld
# 停止并禁用开机启动
systemctl disable firewalld

2、查看firewalld的服务状态

[root@root]# 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 2021-08-11 19:49:50 CST; 28s ago

3、查看firewall的状态

[root@root]# firewall-cmd --state
running

4、查看防火墙的规则

[root@root]# firewall-cmd --list-all 
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

5、查询、开放、关闭端口

# 查询端口是否开放
[root@root]# firewall-cmd --query-port=8080/tcp
no
# 开放80端口	firewall-cmd --zone=public --add-port=80/tcp
[root@root]# firewall-cmd --permanent --add-port=8080/tcp
success

# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

# 开放指定区域端口
[root@root ~]# firewall-cmd --permanent --add-port=8080-8085/tcp
success
# 说明:
	–zone 作用域
    –add-port=8080/tcp 添加端口,格式为:端口/通讯协议
    –permanent 永久生效,没有此参数重启后失效

## !!设置完记得重启防火墙 

# 查看防火墙的开放的端口
[root@root ~]# firewall-cmd --permanent --list-ports
8080-8085/tcp


#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;

6、配置文件设置(默认是在/etc/firewalld/zones/public.xml)

[root@root zones]# pwd
/etc/firewalld/zones
[root@root zones]# vi 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="dhcpv6-client"/>
  <service name="ssh"/>
  <port protocol="tcp" port="80"/>
  <port protocol="tcp" port="8080-8085"/>
  <port protocol="tcp" port="8888"/>
</zone>