How do I close an open port from the terminal on the Mac?
Asked Answered
G

17

330

I opened port #5955 from a java class to comunicate from a client. How do i close this port after I am done? and also which command can show me if port open or closed?

Graniteware answered 12/9, 2012 at 22:50 Comment(1)
How did you open that port? It is not clear whether you are talking about opening and closing the ports in the java application or from the firewall.Rodriquez
J
639
  1. Find out the process ID (PID) which is occupying the port number (e.g., 5955) you would like to free

    sudo lsof -i :5955
    
  2. Kill the process which is currently using the port using its PID

    sudo kill -9 PID
    
Jodyjoe answered 17/7, 2013 at 14:48 Comment(3)
Sending -9 should not be the first attempt to kill a process, and it a very bad habit. As I recall, you should first just use kill PID(which implies -15), then try -2 and -1. -9 is the last resort only if every other options failed to work.Stonedead
did kill pID without any flags as suggested by @Stonedead and this worked for meChee
this answer works for me, but what does those numbers (15,2,1,9) mean?? thanks in advance.Airburst
C
111

To find the process try:

sudo lsof -i :portNumber

Kill the process which is currently using the port using its 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.

Cranium answered 6/10, 2015 at 5:55 Comment(0)
L
77

EDIT

In 09/2022 this helped me for MacOS Monterey M1 Pro Chip:

sudo lsof -t -i tcp:yourPortNumber | sudo xargs kill

In 2018 here is what worked for me using MacOS HighSierra:

sudo lsof -nPi :yourPortNumber

then:

sudo kill -9 yourPIDnumber

Livingston answered 19/12, 2018 at 5:18 Comment(2)
lsof -nPi is slightly better because the port number is actually visible in the output and you get a better feeling of what you are killing.Odilia
Also helpful just to see all listening ports' info: sudo lsof -nPi | grep LISTENRapturous
D
38

very simple find port 5900:

sudo lsof -i :5900

then considering 59553 as PID

sudo kill 59553
Dillman answered 10/11, 2018 at 7:14 Comment(2)
This doesn't add anything to the existing answers!Portcullis
clear and concise solution.Gobbet
A
36

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

 kill -9 $(lsof -i:9604 -t) 2> /dev/null 
Almire answered 6/11, 2019 at 10:1 Comment(0)
S
35

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.

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, e.g.:

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. You can see all of your computer's IPv4 and IPv6 addresses with the ifconfig command.

Spangler answered 12/9, 2012 at 22:57 Comment(0)
G
8

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:

sudo netstat -ap | grep :8000

will output the line corresponding to the process holding port 8000, for example:

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).

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 4683

Again, in general, it's better to avoid sending SIGKILL (-9) if you can.

Glacialist answered 10/11, 2017 at 13:11 Comment(0)
H
8

Find the process ID using command

lsof -n -i4TCP:8080 

After getting the processId

sudo kill -9 processID

Then provide your system password.

Helgoland answered 22/9, 2021 at 6:46 Comment(0)
C
6

I use lsof combined with kill, as mentioned above; but wrote a quick little bash script to automate this process.

With this script, you can simply type killport 3000 from anywhere, and it will kill all processes running on port 3000.

https://github.com/xtrasimplicity/killport

Consumerism answered 6/3, 2016 at 8:34 Comment(1)
You saved my day. For some reason lsof in combination with kill didn't return anything and made my script hang on macOS Sierra. But killport works like a charm and is faster. Thanks!Italic
J
6

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.

Jugglery answered 27/10, 2018 at 7:59 Comment(0)
K
6

Simple One-liner

There is a way more straightforward command today, than the other ones (without Sudo, packages or multiple lines) To kill port 8080 simply call:

lsof -ti tcp:8080 | xargs kill

Kylix answered 31/5, 2022 at 21:16 Comment(0)
S
5

This seem to work for me. Just change your_port_number into the port number you want to stop.

sudo lsof -t -i tcp:your_port_number | xargs kill -9
Sorel answered 8/9, 2021 at 13:46 Comment(0)
C
4

For Linux/macOS:
Step 1:

sudo lsof -i :5955

Step 2: Note the PID (Process ID) value that appears in the printout.
Step 3:

kill <PID>
Cummerbund answered 16/8, 2023 at 19:41 Comment(0)
P
3

try below, assuming running port is 8000:

free-port() { kill "$(lsof -t -i :8000)"; }

I found the reference here

Parlor answered 8/12, 2018 at 13:9 Comment(0)
F
1

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.

Flesh answered 28/9, 2012 at 6:3 Comment(0)
I
0

This script to Stop port will help you to stop the port by writing stop <portnumber> to kill the running port in the terminal. it is usually handy when I try to start a program and the port is already occupied. It uses the below code.

pid=$(lsof -ti tcp:$1)
if [[ $pid ]]; then
  kill -9 $pid
  echo "Congrats!! $1 is stopped."
else
   echo "Sorry nothing running on above port" 
fi
Ischium answered 25/2 at 14:32 Comment(0)
S
-3
  1. First find out the Procees id (pid) which has occupied the required port.(e.g 5434)

    ps aux | grep 5434

2.kill that process

   kill -9 <pid>
Selimah answered 31/7, 2015 at 11:52 Comment(1)
Use following command to find port sudo lsof -i :5955Rycca

© 2022 - 2024 — McMap. All rights reserved.