1. 1、启动防火墙
  2. [root@share ~]# systemctl start firewalld.service
  3. 2、指定IP与端口
  4. [root@share ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.77" port protocol="tcp" port="5432" accept"
  5. 指定多个端口(连续)
  6. [root@share ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.77" port protocol="tcp" port="13330-13339" accept"
  7. 指定ip段可以访问
  8. [root@share ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.0/24" port protocol="tcp" port="5432" accept"
  9. 3、重新载入,使配置生效
  10. [root@share ~]# firewall-cmd --reload
  11. 4、查看配置结果
  12. [root@share ~]# firewall-cmd --list-all
  13. 5、删除规则
  14. [root@share ~]# firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.100.77" port protocol="tcp" port="445" accept"
  15. CentOS 7.0默认使用的是firewall作为防火墙
  16. 1、查看firewall服务状态
  17. systemctl status firewalld
  18. 2、查看firewall的状态
  19. firewall-cmd --state
  20. 3、开启、重启、关闭、firewalld.service服务
  21. #查看linux哪些程序正在使用互联网
  22. firewall-cmd --permanent --list-services ssh dhcpv6-client

开启

service firewalld start

重启

service firewalld restart

关闭

  1. service firewalld stop
  2. 4、查看防火墙规则
  3. firewall-cmd --list-all
  4. 停止firewall
  5. systemctl stop firewalld.service
  6. 禁止firewall开机启动
  7. systemctl disable firewalld.service
  8. 5、查询、开放、关闭端口

查询端口是否开放

firewall-cmd —query-port=8080/tcp

开放80端口

firewall-cmd —permanent —add-port=80/tcp
firewall-cmd —permanent —add-port=8080-8085/tcp

移除端口

firewall-cmd —permanent —remove-port=8080/tcp
查看防火墙的开放的端口
firewall-cmd —permanent —list-ports

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

参数解释

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

常用命令配置白名单,限制指定ip只能访问服务器指定的端口和去掉开通的端口

  1. #配置规则
  2. firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="指定的ip地址" port protocol="tcp" prot="指定的端口" accept"
  3. #配置所有端口都可以访问
  4. firewall-cmd --permanent --zone=public --add-port=8080/tcp
  5. #批量配置规则
  6. firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="指定的ip地址段(192.168.1.0/24" port protocol="tcp" prot="指定的端口" accept"
  7. #reload配置生效
  8. firewall-cmd --reload
  9. #关闭端口
  10. firewall-cmd --zone=public --remove-port=80/tcp --permanent
  11. #批量添加区间端口
  12. firewall-cmd --zone=public --add-port=4400-4600/udp --permanent
  13. #查看防火墙排除端口
  14. firewall-cmd --permanent --list-port
  15. #查看所有开放的端口
  16. iptables -L -n