Have you ever found yourself in a position where you need to use a port but it’s not available?The worst part is when you don’t even know what process is using the port. Worry not, it’s super duper easy to free ports that are in use.
I can think of two ways to solve this:
- The fast and lazy way
- The nice careful way
The fast and lazy way
First, you find out what process is using that port. The lazy way only gives you the process id of whatever is using the port and not the name.
You do this by running: $lsof -ti:<port>
Example: $lsof -ti:5900
The command above will give you an ID.
Then, run: $lsof -ti:5900 | xargs kill -9
This will kill whatever process that is using that port.
Remember to substitute the port number with the actual port number in question.
NB: Note that killing a process without knowing which process it really is is probably not a good idea. I am however, a very lazy person and thus had to include a lazy option.
The nice careful way
This give you a friendlier report on what is using the port.
To use the nice way, run:
$ netstat -tulpn | grep <port>
Example: $netstat -tulpn | grep 5900
You can also use the ss command like: ss -ltp | grep <port>