In Terminal, type the following command to find the process ID (PID) of the port. Replace PORT_NUMBER
with the actual port number you want to close.
sudo lsof -i :PORT_NUMBER
Copy the PID number, then use the following command to kill the process using the PID you found.
sudo kill PID_NUMBER
To verify that the port has been closed, you can try to connect to the port.
nc localhost PORT_NUMBER
If it returns immediately without any output, the port is closed. If it retruns some input, try killing it with:
sudo kill -9 PID_NUMBER