linux firewalld防火墙放行端口

1. firewalld常用命令

# 查看所有放行端口
firewall-cmd --zone=public --list-ports
# 禁止IP访问机器
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="192.168.0.1" drop'
# 禁止一个IP段,比如禁止192.168.*.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="192.168.0.1/16" drop'
# 禁止一个IP段,比如禁止192.168.0.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="192.168.0.1/24" drop'
# 禁止机器IP从防火墙中删除
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address="192.168.0.1" drop'
# 允许http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --add-service=http
# 关闭http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --remove-service=http
# 允许端口:3306
firewall-cmd --permanent --add-port=3306/tcp
# 允许端口:1-3306
firewall-cmd --permanent --add-port=1-3306/tcp
# 关闭放行中端口:3306
firewall-cmd --permanent --remove-port=3306/tcp
# 查看firewall的状态
firewall-cmd --state    
# 查看防火墙规则(只显示/etc/firewalld/zones/public.xml中防火墙策略)
firewall-cmd --list-all
# 查看所有的防火墙策略(即显示/etc/firewalld/zones/下的所有策略)
firewall-cmd --list-all-zones
# 重新加载配置文件
firewall-cmd --reload
# 更改配置后一定要重新加载配置文件
firewall-cmd --reload

2. firewalld中放行端口

xxxx/tcp        端口/通讯协议
zone              作用域
permanent     永久生效,没有此参数重启后失效
firewall-cmd --zone=public --add-port=xxxx/tcp --permanent
#开放端口必须加上协议
[root@web02 ~]# firewall-cmd --add-port=80/tcp
success
#添加多个端口
[root@web02 ~]# firewall-cmd --add-port={443/tcp,80/udp}
success

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

3. firewalld刷新生效

#重启 firewalld
firewall-cmd --reload
#查看是否生效
firewall-cmd --list-ports
firewall-cmd --list-all

linux中常用防火墙软件有两个firewalld和iptables,以上命令只对firewall生效,如果无效,尝试关闭iptables。

4. firewalld放行服务

#放行某个服务
[root@web02 ~]# firewall-cmd --add-service=http
success

#放行多个服务
[root@web02 ~]# firewall-cmd --add-service={https,mysql,redis}
success

#所有可放行的服务全都写在这个目录,可以自行修改
[root@web02 /usr/lib/firewalld/services]# pwd
/usr/lib/firewalld/services

#自定义配置服务
[root@web02 /usr/lib/firewalld/services]# cp http.xml nginx.xml
[root@web02 ~]# firewall-cmd --reload
success
[root@web02 ~]# firewall-cmd --add-service=nginx
success

5. 放行ip

#允许一个IP所有的操作
[root@web02 ~]# firewall-cmd --add-source=10.0.0.1 --zone=trusted
success

来源:https://www.dandelioncloud.cn/article/details/1507074604224040962

posted @ 2022-12-02 09:12  可惜流年  阅读(1054)  评论(0编辑  收藏  举报