[英]How do I close an open port from the terminal on the Mac?
I opened port #5955 from a java class to comunicate from a client.我从 java 类中打开端口 #5955 以与客户端通信。 How do i close this port after I am done?完成后如何关闭此端口? and also which command can show me if port open or closed?还有哪个命令可以告诉我端口是打开还是关闭?
Find out the process ID (PID) which is occupying the port number (eg, 5955) you would like to free找出占用您想要释放的端口号(例如 5955)的进程 ID (PID)
sudo lsof -i :5955
Kill the process which is currently using the port using its PID使用其 PID 杀死当前正在使用该端口的进程
sudo kill -9 PID
To find the process try:要查找该过程,请尝试:
sudo lsof -i :portNumber
Kill the process which is currently using the port using its PID使用其 PID 杀死当前正在使用该端口的进程
kill PID
and then check to see if the port closed.然后检查端口是否关闭。 If not, try:如果没有,请尝试:
kill -9 PID
I would only do the following if the previous didnt work如果前一个不起作用,我只会执行以下操作
sudo kill -9 PID
Just to be safe.只是为了安全。 Again depending on how you opened the port, this may not matter.再次取决于您打开端口的方式,这可能无关紧要。
In 2018 here is what worked for me using MacOS HighSierra:在 2018 年,这对我使用 MacOS HighSierra 有用:
sudo lsof -nPi :yourPortNumber
then:然后:
sudo kill -9 yourPIDnumber
very simple find port 5900:很简单的找到5900端口:
sudo lsof -i :5900
then considering 59553 as PID然后将 59553 视为 PID
sudo kill 59553
However you opened the port, you close it in the same way.无论您如何打开端口,都以相同的方式关闭它。 For example, if you created a socket, bound it to port 0.0.0.0:5955, and called listen, close that same socket.例如,如果您创建了一个套接字,将其绑定到端口 0.0.0.0:5955,并调用 listen,关闭同一个套接字。
You can also just kill the process that has the port open.您也可以直接终止打开端口的进程。
If you want to find out what process has a port open, try this:如果您想找出哪个进程打开了端口,请尝试以下操作:
lsof -i :5955
If you want to know whether a port is open, you can do the same lsof command (if any process has it open, it's open; otherwise, it's not), or you can just try to connect to it, eg:如果你想知道一个端口是否打开,你可以执行相同的 lsof 命令(如果任何进程打开它,它是打开的;否则,它不是),或者你可以尝试连接它,例如:
nc localhost 5955
If it returns immediately with no output, the port isn't open.如果它立即返回而没有输出,则端口未打开。
It may be worth mentioning that, technically speaking, it's not a port that's open, but a host:port combination.值得一提的是,从技术上讲,它不是开放的端口,而是主机:端口组合。 For example, if you're plugged into a LAN as 10.0.1.2, you could bind a socket to 127.0.0.1:5955, or 10.0.1.2:5955, without either one affecting the other, or you could bind to 0.0.0.0:5955 to handle both at once.例如,如果您以 10.0.1.2 的身份插入 LAN,则可以将套接字绑定到 127.0.0.1:5955 或 10.0.1.2:5955,其中一个不会影响另一个,或者您可以绑定到 0.0.0.0 :5955 同时处理两者。 You can see all of your computer's IPv4 and IPv6 addresses with the ifconfig
command.您可以使用ifconfig
命令查看计算机的所有 IPv4 和 IPv6 地址。
One liner is best一个班轮是最好的
kill -9 $(lsof -i:PORT -t) 2> /dev/null
Example : On mac, wanted to clear port 9604. Following command worked like a charm示例:在 mac 上,想清除端口 9604。以下命令就像一个魅力
kill -9 $(lsof -i:9604 -t) 2> /dev/null
You can also use this first command to kill a process that owns a particular port:您还可以使用第一个命令来终止拥有特定端口的进程:
sudo netstat -ap | grep :<port_number>
For example, say this process holds port 8000 TCP, then running the command:例如,假设这个进程持有 TCP 8000端口,然后运行命令:
sudo netstat -ap | grep :8000
will output the line corresponding to the process holding port 8000 , for example:将输出与持有端口8000的进程对应的行,例如:
tcp 0 0 *:8000 *:* LISTEN 4683/procHoldingPort
In this case, procHoldingPort is the name of the process that opened the port, 4683 is its pid, and 8000 (note that it is TCP) is the port number it holds (which you wish to close).在这种情况下, procHoldingPort是打开端口的进程的名称, 4683是它的 pid, 8000 (注意它是 TCP)是它持有的端口号(您希望关闭它)。
Then kill the process, following the above example:然后按照上面的例子杀死进程:
kill 4683
As others mentioned here out, if that doesn't work (you can try using kill with -9 as an argument):正如其他人在这里提到的那样,如果这不起作用(您可以尝试使用 kill 和 -9 作为参数):
kill -9 4683
Again, in general, it's better to avoid sending SIGKILL
(-9) if you can.同样,一般来说,如果可以的话,最好避免发送SIGKILL
(-9)。
Find the process ID using command使用命令查找进程 ID
lsof -n -i4TCP:8080
After getting the processId得到processId后
sudo kill -9 processID
Then provide your system password.然后提供您的系统密码。
I have created a function for this purpose.为此,我创建了一个函数。
function free_port() {
if [ -z $1 ]
then
echo no Port given
else
PORT=$1;
PID=$(sudo lsof -i :$PORT) # store the PID, that is using this port
if [ -z $PID ]
then
echo port: $PORT is already free.
else
sudo kill -9 $PID # kill the process, which frees the port
echo port: $PORT is now free.
fi
fi
}
free_port 80 # you need to change this port number
Copy & pasting this block of code in your terminal should free your desired port.在您的终端中复制并粘贴这段代码应该可以释放您想要的端口。 Just remember to change the port number in last line.请记住在最后一行更改端口号。
I use lsof
combined with kill
, as mentioned above;如上所述,我将lsof
与kill
结合使用; but wrote a quick little bash script to automate this process.但是写了一个快速的小 bash 脚本来自动化这个过程。
With this script, you can simply type killport 3000
from anywhere, and it will kill all processes running on port 3000
.使用此脚本,您可以在任何地方简单地键入killport 3000
,它将杀死在端口3000
上运行的所有进程。
https://github.com/xtrasimplicity/killport https://github.com/xtrasimplicity/killport
This seem to work for me.这似乎对我有用。 Just change your_port_number into the port number you want to stop.只需将 your_port_number 更改为您要停止的端口号。
sudo lsof -t -i tcp:your_port_number | xargs kill -9
When the program that opened the port exits, the port will be closed automatically.当打开端口的程序退出时,端口会自动关闭。 If you kill the Java process running this server, that should do it.如果您终止运行此服务器的 Java 进程,则应该这样做。
WTF.跆拳道。 I want close socket without process kills.我想关闭套接字而没有进程终止。
There is a way more straightforward command today, than the other ones (without Sudo, or multiple lines) To kill port 8080 simply call:今天有一种比其他命令更直接的命令(没有 Sudo 或多行)要杀死端口 8080,只需调用:
lsof -ti tcp:8080 | xargs kill
First find out the Procees id (pid) which has occupied the required port.(eg 5434)首先找出占用了所需端口的Procees id(pid)。(例如5434)
ps aux | ps辅助| grep 5434 grep 5434
2.kill that process 2.杀死那个进程
kill -9 <pid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.