6

看完这篇文章,小白都会开启防火墙特定端口了

 3 years ago
source link: http://www.justdojava.com/2021/02/06/firewall/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

运维相关的知识,说起来是比较尴尬的

对于专业做运维的人来说,那些知识真是太简单了,就是一条命令的事情;但是对于做开发的小伙伴来说,又有些难,难就难在, Linux 环境和我们平时使用的 Windows , Mac 环境不一样,有时候虽然只是一条命令的事情,但是却迟迟不敢按下回车键

阿粉也是从一个什么都不知道的小白成长起来的,所以对于刚开始操作服务器的畏惧心理是了解一些的,今天就讲讲好操作,容易上手的 – CentOS7 开启防火墙及特定端口

开启防火墙服务

1,首先我们来看下防火墙的状态:

firewall-cmd --state

下图所示为关闭防火墙,接下来需要开启

01-notRunning.jpg

2, 开启防火墙,启动firewall:

systemctl start firewalld.service

设置开机自启:

systemctl enable firewalld.service

3, 重启防火墙:

systemctl restart firewalld.service

4, 检查防火墙状态是否打开:

firewall-cmd --state

如图显示已经打开:

02-Running.jpg

5, 查看防火墙设置开机自启是否成功:

systemctl is-enabled firewalld.service;echo $?

如图所示,即为成功

03-enabled.jpg

开启特定端口

在开启防火墙之后,我们有些服务就会访问不到,是因为服务的相关端口没有打开。

在此以打开80端口为例,命令:

开端口命令:firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙:systemctl restart firewalld.service
 
命令含义:
 
--zone #作用域
 
--add-port=80/tcp  #添加端口,格式为:端口/通讯协议
 
--permanent   #永久生效,没有此参数重启后失效

如图,可看到开启端口成功:

04-success.jpg

如果不放心,可以通过命令:

netstat -ntlp
或:firewall-cmd --list-ports

查看开启的所有端口,具体如图

05-firewall.jpg

其中 netstat -ntlp 是查看监听服务,在上面也能看到有个 state 的状态,下面都是 Listen 状态,所以这条命令只有在端口处于使用的状态才能看到,在最后一行的 PID/Program name 我们能够看到,使用端口对应的程序是哪些

firewall-cmd --list-ports 这条命令看起来就简单很多了,它就是看开启的端口,开启了就会列出来,没有开启就不会列出来

今天这个简单的分享,有没有帮助到你呢?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK