Linux 防火墙


Linux 防火墙

1、查看firewall服务状态

1
systemctl status firewalld

2、查看firewall的状态

1
firewall-cmd --state

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

1
2
3
4
5
6
# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop

4、查看防火墙规则

1
firewall-cmd --list-all

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

1
2
3
4
5
6
7
8
9
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
1
2
3
4
# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;

6、允许192.168.1.10所有访问所有端口

1
firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.1.10" accept' --permanent

7、配置文件

/etc/firewalld/zones/public.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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"/>
<port protocol="tcp" port="22"/>
<port protocol="tcp" port="8080"/>
<port protocol="tcp" port="443"/>
<rule family="ipv4">
<source address="120.25.xx.xx"/>
<accept/>
</rule>
<rule family="ipv4">
<source address="172.18.xx.xx"/>
<accept/>
</rule>
</zone>