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

user1667108 picture user1667108 · Sep 13, 2012 · Viewed 291.8k times · Source

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?

Answer

Future2020 picture Future2020 · Jul 17, 2013
  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