Linux 释放 TCP/IP 端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/750604/
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-03 17:12:37  来源:igfitidea点击:

Freeing up a TCP/IP port?

linuxnetworkingtcpport

提问by

netstat -tulnapshows me what ports are in use. How to free up a port in Linux?

netstat -tulnap显示正在使用的端口。如何在 Linux 中释放端口?

回答by Gleb

Kill the process that is listening to the port in question. I believe netstat shows you process ids.

杀死正在侦听相关端口的进程。我相信 netstat 会向您显示进程 ID。

回答by Tilo Prütz

I think the only way will be to stop the process which has opened the port.

我认为唯一的方法是停止打开端口的进程。

回答by paxdiablo

The "netstat --programs"command will give you the process information, assuming you're root. Then you will have to kill the "offending" process which may well start up again just to annoy you :-).

"netstat --programs"假设您是 root,该命令将为您提供进程信息。然后你将不得不终止“冒犯”的进程,它很可能会再次启动,只是为了惹恼你:-)。

What are you actually trying to achieve here? Solutions will vary based on the processes holding those ports.

你真正想在这里实现什么?解决方案将根据持有这些端口的进程而有所不同。

回答by uzi

As the others have said, you'll have to kill all processes that are listening on that port. The easiest way to do that would be to use the fuser(1) command. For example, to see all of the processes listening for http requests on port 80 (run as root or use sudo):

正如其他人所说,您必须杀死在该端口上侦听的所有进程。最简单的方法是使用 fuser(1) 命令。例如,要查看在端口 80 上侦听 http 请求的所有进程(以 root 身份运行或使用 sudo):

# fuser 80/tcp

If you want to kill them, then just add the -k option.

如果你想杀死它们,那么只需添加 -k 选项。

回答by Paul de Vrieze

If you really want to kill a process immediately, you send it a KILL signal instead of a TERM signal (the latter a request to stop, the first will take effect immediately without any cleanup). It is easy to do:

如果你真的想立即杀死一个进程,你发送一个 KILL 信号而不是一个 TERM 信号(后者是一个停止请求,第一个将立即生效,无需任何清理)。很容易做到:

kill -KILL <pid>

Be aware however that depending on the program you are stopping, its state may get badly corrupted when doing so. You normally only want to send a KILL signal when normal termination does not work. I'm wondering what the underlying problem is that you try to solve and whether killing is the right solution.

但是请注意,根据您要停止的程序,执行此操作时其状态可能会严重损坏。您通常只想在正常终止不起作用时发送 KILL 信号。我想知道您试图解决的根本问题是什么,以及杀戮是否是正确的解决方案。

回答by Zonxwedop

sudo killall -9 "process name"

sudo killall -9 “进程名称”

回答by Zonxwedop

Shutting down the computer always kills the process for me.

关闭计算机总是会杀死我的进程。

回答by AlexT

You can use tcpkill(part of the dsniffpackage) to kill the connection that's on the port you need:

您可以使用tcpkilldsniff包的一部分)来终止您需要的端口上的连接:

sudo tcpkill -9 port PORT_NUMBER

回答by Anil Chahal

To a kill a specific port in Linux use below command

要杀死 Linux 中的特定端口,请使用以下命令

sudo fuser -k Port_Number/tcp

replace Port_Number with your occupied port.

用您占用的端口替换 Port_Number。

回答by user2332505

In terminal type :

在终端类型:

netstat -anp|grep "port_number"

netstat -anp|grep "port_number"

It will show the port details. Go to last column. It will be in this format . For example :- PID/java

它将显示端口详细信息。转到最后一栏。它将采用这种格式。例如:- PID/java

then execute :

然后执行:

kill -9 PID. Worked on Centos5

kill -9 PID. Worked on Centos5

For MAC:

对于 MAC:

lsof -n -i :'port-number' | grep LISTEN

Sample Response :

示例响应:

java   4744 (PID)  test  364u  IP0 asdasdasda   0t0  TCP *:port-number (LISTEN)

and then execute :

然后执行:

kill -9 PID 

Worked on Macbook

在 Macbook 上工作