Free your blocked ports on Linux

Subscribe to my newsletter and never miss my upcoming articles

You closed all your code editors and terminals but your ports are still blocked.

The only solution you would have in your mind as a beginner is, to restart the computer. That would definitely fix this.

But what if there is a better way?

What if I can check where my port is being used and close that application, rather than restarting the computer?

For example, my blocked port is 3005 and I would like to see which application is still using it. I would simply run this in my terminal-

lsof -i :3005

This would give an output with a table of all the programs using this port 3005. Notice the PID column there and note down its value. In my case, it was 6595.

Now run the following command to kill the process and free the port from this evil app-

kill 6595

Now run your project on that port and live happily ever after.

used-ports-bg.png

Closing Notes

This is not a very frequent problem but can be frustrating for beginners and developers who are not proficient in Linux.

Comments (2)

Corey O'Donnell's photo

If you were interested in finding all the ports actively used on your machine

You can use this:

sudo lsof -i | grep LISTEN

I set it up as an alias in my .bash_profile because I have had to use this more than once.

alias openPorts="sudo lsof -i | grep LISTEN"

So now in my terminal, I just type openPorts and it displays all the active ports being used

spotify 23576  corey   101u  IPv4 5641  0t0  TCP *:54063 (LISTEN)
node    23594  corey   59u   IPv4 4541  0t0  TCP *:3000 (LISTEN)
Pankaj Sanam's photo

It is nice to have this as an alias for frequent use. Thanks for the tip Corey 🥂