firewall防火墙服务

发布时间 2023-07-11 21:31:58作者: 村尚chun叔

使用firewall防火墙命令,查看系统提供了哪些模板

1.列出所有的区域模板

列出区域模板,以及具体的信息
[root@yuchao-linux01 ~]# firewall-cmd --list-all-zones

列出所有的区域的名字
[root@yuchao-linux01 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

2.列出当前使用的区域

[root@yuchao-linux01 ~]# firewall-cmd --get-default-zone 
public

3.查看当前的public区域,以及其详细信息

[root@yuchao-linux01 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client ntp
  ports: 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

4.先运行一个80端口的服务

[root@yuchao-linux01 ~]# python -m SimpleHTTPServer 80

5.给当前的防火墙区域,添加一个策略,允许80端口通过

[root@yuchao-linux01 ~]# firewall-cmd --add-port=80/tcp
success

6.再添加一个8000端口的规则,我们接触的绝大多数,都是端口号/tcp 这个即可

[root@yuchao-linux01 ~]# firewall-cmd --add-port=8000/tcp
success

7.删除,添加的端口规则

[root@yuchao-linux01 ~]# firewall-cmd --remove-port=80/tcp
success
[root@yuchao-linux01 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

8.针对服务名添加,比如ntp服务

[root@yuchao-linux01 ~]# firewall-cmd --add-service=ntp

9. 查看当前public区域,使用了哪些规则

[root@yuchao-linux01 ~]# firewall-cmd --list-all

10. firewalld,作用其实是添加iptables的规则

查看系统上所有iptables的命令

iptables -L

tcp 是一个安全可靠的连接,需要双向确认,客户端,和服务端,都要确认对方已经连接上了

udp 是一个不可靠的连接协议,客户端可以随便给服务端发,不需要对方确认,比如一个很差的网络环境下,网页无法访问,无法做dns解析(网络服务,网站服务,用的都是tcp协议),但是QQ可以收发消息(QQ用的是udp协议,以及ntp用的也是udp协议)

查看到firewalld命令,添加的防火墙规则如下
[root@yuchao-linux01 ~]# iptables -L |grep ntp
ACCEPT     udp  --  anywhere             anywhere             udp dpt:ntp ctstate NEW

11.永久性添加 8000/tcp的策略

[root@yuchao-linux01 ~]# firewall-cmd  --permanent --add-port=8000/tcp

12.需要重新加载firewalld服务

[root@yuchao-linux01 ~]# firewall-cmd --reload
success

13.重新加载后,规则自动生成了,生效了

[root@yuchao-linux01 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client ntp
  ports: 80/tcp 8000/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
1.查询支持的所有服务名字有哪些(nginx,httpd,统一被他制作为了 http服务,放行80端口)

firewall-cmd --get-services

2. 查询当前区域,所用的服务名有哪些
firewall-cmd --list-services

3. 添加http服务,放行80端口即可
firewall-cmd --add-service=http

4.移除该服务,禁用80端口的请求
firewall-cmd --remove-service=http

5.建议,最好还是直接针对端口号,协议号,添加规则,

iptables 支持很多复杂的参数,针对协议,来源端口,目标端口等等
公有云的安全组(阿里云提供的硬件防火墙),也是基于iptables这样的规则添加的