Linux 如何检查某个端口是否打开和未使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14001825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:05:52  来源:igfitidea点击:

How to check if a certain port is open and unused?

linux

提问by Richa

I want to install webserver-apache on a Linux platform which uses port no.80 but I am not sure whether is port is open or not , and being used by some other application or not.

我想在使用端口号 80 的 Linux 平台上安装 webserver-apache,但我不确定端口是否打开,以及是否被其他应用程序使用。

  1. Output of grep 80 /etc/servicesis:
  1. 的输出grep 80 /etc/services是:

http 80/tcp www www-http #World Wide web Http

http 80/tcp www www-http #World Wide web Http

http 80/udp www www-http #Hypertext transfer protocol

http 80/udp www www-http #Hypertext transfer protocol

2.netstat -an | grep 80 | more:

2 netstat -an | grep 80 | more.:

It gives some IP's one of which is IP:80 TIME_WAIT

它提供了一些 IP,其中之一是 IP:80 TIME_WAIT

Could you please help and tell how can i find out if port 80 is open and unused so that I can start installation.

能否请您帮忙告诉我如何确定端口 80 是否已打开且未使用,以便我可以开始安装。

回答by Dillon Benson

Try piping lsof into grep and searching for a port number:

尝试将 lsof 管道输入 grep 并搜索端口号:

lsof|grep <port>

If nothing shows up that means the port is not in use

如果没有任何显示,则表示该端口未在使用中

You can kill a process on a specific port using

您可以使用以下命令终止特定端口上的进程

kill -9 <pid>

Where pid is the process id obtained from the first command.

其中 pid 是从第一个命令获得的进程 ID。

回答by MichaelMoser

       netstat -tln | tail -n +3 | awk '{ print  }'  

this one displays bind addresses of tcp listening endpoint. all other endpoints are free; Also if on Unix and you are not root, then you can't bind to a 'privileged' port number (port number lower than 1024)

这个显示 tcp 侦听端点的绑定地址。所有其他端点都是免费的;此外,如果在 Unix 上并且您不是 root,则无法绑定到“特权”端口号(端口号低于 1024)

explained in more detail:

更详细地解释:

netstat -tln - all listening tcp ports

netstat -tln - 所有监听 tcp 端口

tail -n +3 - cut of the header of netstat

tail -n +3 - netstat 头部的剪切

awk '{ print $4 }' - print the fourth column that consists of [ip]:[port]

awk '{print $4 }' - 打印由 [ip]:[port] 组成的第四列

for the general case you still need to care to cut out all irrelevant interfaces; a listening address 0.0.0.0 is listening on all network cards, if there is an IP address than that's the specific IP of the network car/network interface.

对于一般情况,您仍然需要注意删除所有不相关的接口;侦听地址 0.0.0.0 正在侦听所有网卡,如果有一个 IP 地址,那就是网卡/网络接口的特定 IP。

回答by Jordan Stewart

As part of a script you would probably want to use something like this:

作为脚本的一部分,您可能希望使用以下内容:

resp=`netstat -tunl | grep ":80 "`
if [ -z "$resp" ]; then
    echo "80 Port is free"
else
    echo "80 Port is not free"
fi

回答by tink

sudo netstat -anp | grep ':80 '

That should give you pid & name of the process that holds port 80

这应该给你 pid 和持有端口 80 的进程的名称

回答by Neeraj Kumar

Here is command which you need to kill

这是您需要杀死的命令

netstat -tulpno | grep ':80'

回答by Seff

This can be achieved using the nccommand as follows:

这可以使用nc如下命令来实现:

# nc -z IP PORT

# nc -z IP PORT

It will return TRUE if the port is already in use, or FALSE is it (i.e, available not listening currently).

如果端口已被使用,它将返回 TRUE,或者是 FALSE(即,当前不侦听可用)。

I don't recommend lsofor netstatmethod as it first try to scan all running PIDs to get all bounded ports:

我不推荐lsofnetstat方法,因为它首先尝试扫描所有正在运行的 PID 以获取所有有界端口:

# time lsof -i:8888
real    0m1.194s
user    0m0.137s
sys 0m1.056s```

# time nc -z 127.0.0.1 8888
real    0m0.014s
user    0m0.011s
sys 0m0.004s

Here 8888 is an unused port. The nccommand is ~85 times faster in the above example.

这里 8888 是一个未使用的端口。nc在上面的示例中,该命令的速度提高了约 85 倍。

Eg 1:

例如 1:

$ nc -z 127.0.0.1 80 && echo "IN USE" || echo "FREE"
IN USE

$ nc -z 127.0.0.1 81 && echo "IN USE" || echo "FREE"
FREE

Eg 2:

例如2:

If you are trying with a remote IP, it is better to add a timeout to auto-exit if it is not accepting connection for the specified time.

如果您尝试使用远程 IP,如果在指定时间内不接受连接,最好添加超时以自动退出。

$ nc -w 2 -z 216.58.196.174 81

$ nc -w 2 -z 216.58.196.174 81

Its Google's IP which is not used, so it will timeout after trying for 2 seconds.

它的谷歌IP没有使用,所以尝试2秒后会超时。