How to Find Process Locking a Port on Mac
Mac users may occasionally come across a situation where a process is locking a port, preventing another app or process from using that port. If you come across such a situation, for example maybe you’re trying to use React.js and find out that something is already running on port 3000, you can easily track down what process(es) are using a port on the Mac by turning to the command line.
We’ll show you how to find and kill what process is locking a port on MacOS.
How to Find What Process is Locking a Port on Mac
The syntax to use would be the following, replacing PORTNUMBER with the port number you wish to discover the process(es) using:
sudo lsof -i :PORTNUMBER
For example, to find the process using port 445:
sudo lsof -i :445
Or to find the process using and locking port 3000:
sudo lsof -i :3000
How to Find & Kill Process Locking a Port on Mac
Once you have the PID (process ID) from the command output of lsof, you can then either quit the app, shutdown the service, or terminate that process, to release and free up the locked port.
The simplest way to kill a process is with the kill command:
kill -9 PID
So for example, if the process ID using port 3000 is “8384” then you’d use the following command syntax:
kill -9 8384
If the process is owned by root, admin, or another user, you’d need to prefix the command with sudo:
sudo kill -9 8384
For what it’s worth, the lsof command used here is focused on the Mac, but will work the same in linux as well.
We’re assuming you have some knowledge of the command line, but of course if you did not then you likely wouldn’t be here in the first place, or worried about what is using or locking a port.
There are other ways to do this, but obviously here we are using the lsof command, which is very powerful, and can also use it to find what is listening on a TCP port, to find what apps or processes are interacting with particular files, to find all processes using internet connectivity, and more.
Do you have another method for determining what process is locking or using a particular port? Share with us your own approaches to this in the comments below.
One should not routinely use “-9” with kill.
That terminates the process immediately, without giving it any opportunity to “clean up” after itself. This can result in “zombie” processes that also hold onto resources, which may not be killable. So by using -9 to terminate a process that is locking a port, you may be stranding other processes that lock that same port, or some other open file.
Any signal besides -9 (or “-KILL”) will allow the process the opportunity to go away gracefully. I use the following sequence, and only resort to -9 if none of these work: none, -1, -2, -3, and -6. Note that if no signal is specified, it defaults to TERMinate, or -15.