zl程序教程

您现在的位置是:首页 >  系统

当前栏目

工作总结之linux防火墙配置命令适用centos7、centos8

Linuxcentos7配置命令防火墙 总结 工作 适用
2023-09-27 14:27:52 时间

一、防火墙服务

1、启动、关闭、重启防火墙服务。

systemctl start  firewalld.service
systemctl stop  firewalld.service
systemctl restart  firewalld.service

2、显示防火墙的状态。

systemctl status firewalld.service

3、开机启动防火墙。

systemctl enable firewalld.service

4、开机时禁用防火墙。

systemctl disable firewalld.service

5、查看防火墙是否开机启动。

systemctl  is-enabled  firewalld.service

6、查看已启动的服务列表。

systemctl list-unit-files|grep enabled

7、查看启动失败的服务列表。

systemctl  --failed

8、启动、停止、重启httpd服务。

systemctl   start    httpd
systemctl   stop     httpd
systemctl   restart  httpd

二、配置防火墙

1、查看版本。

firewall-cmd --version

2、查看帮助。

firewall-cmd  --help

3、显示状态。

firewall-cmd --state

4、查看所有打开的端口。

firewall-cmd --zone=public --list-ports

5、重新载入,更新防火墙规则。

firewall-cmd --reload

6、查看区域信息。

firewall-cmd --get-active-zones

7、查看指定接口所属区域。

firewall-cmd --get-zone-of-interface=eth0

8、拒绝所有包。

firewall-cmd --panic-on

9、取消拒绝状态。

firewall-cmd --panic-off

10、查看是否拒绝。

firewall-cmd --query-panic

11、开启80端口,–permanent永久生效,没有此参数重启后失效。

firewall-cmd --zone=public --add-port=80/tcp --permanent   

12、查看80端口是否开放。

firewall-cmd --zone=public --query-port=80/tcp 

13、删除80端口配置。

firewall-cmd --zone=public --remove-port=80/tcp --permanent 

在CentOS 7中,开启指定区域(zone)中的指定端口8080,或者端口3306、或者指定范围内的端口8080-8088。

**使用以下三种方式其一,**对firewalld服务(防火墙)进行增加区域、以及修改配置。

1、工具有例如firewall-config图形界面工具,

*2、firewall-cmd命令行工具,*

3、在配置文件目录/etc/firewalld/zones中创建、或者拷贝系统默认的/usr/lib/firewalld/zones区域(zone)中的服务配置文件(XML),然后使用vim直接做出相应修改。

其中/usr/lib/firewalld/zones被用于默认和备用的服务,服务是 firewalld 所使用的有关端口和选项的规则集合,被启动的服务会在 firewalld 服务开启或者运行时自动加载,默认情况下,这里的很多服务是有效的。

但是,/etc/firewalld/zones被用于用户创建的,可以自定义其中的服务配置文件(XML),想要创建自己的服务,需要在该目录下自定相关服务。


三、使用vim编辑/etc/firewalld/zones/public.xml文件,开启指定区域(zone)中的指定源IP。

1、1、使用vim编辑/etc/firewalld/zones/public.xml文件,*在标签中添加子标签,***

<?xml version="1.0" encoding="utf-8"?>



<zone>   



<short>Public</short>  



<description>For use in public areas.</description>   



<rule family="ipv4">    



<source address="122.10.70.234"/>     



<port protocol="udp" port="514"/>     



<accept/>   



</rule>   



<rule family="ipv4">     



<source address="123.60.255.14"/>     



<port protocol="tcp" port="10050-10051"/>     



<accept/>  



</rule>  



<rule family="ipv4">     



<source address="192.249.87.114"/> 放通指定ip,指定端口、协议     



<port protocol="tcp" port="80"/>     



<accept/>   



</rule>



<rule family="ipv4"> 放通任意ip访问服务器的9527端口     



<port protocol="tcp" port="9527"/>     



<accept/>   



</rule>



</zone>

上述的一个配置文件可以很好的看出:
1、添加需要的规则,开放通源ip为122.10.70.234,端口514,协议tcp;
2、开放通源ip为123.60.255.14,端口10050-10051,协议tcp;
3、开放通源ip为任意,端口9527,协议tcp;