Sometimes you might find a program that was supposed to have finished using a particular port on your machine still using it. Here’s how you go about finding and killing that process in Linux:

  1. Using the List Open File command lsof, type lsof -i:portNumber (e.g. lsof -i:8000) to list the IP sockets.

  2. If there is a program running on that port, you should get the following list:

COMMAND     PID     USER    FD   TYPE   DEVICE SIZE/OFF NODE NAME
nameOfProg  123456 someuser 10u  IPv6  12345678   0t0   TCP *:irdmi (LISTEN)
  1. In order to kill the process, type the following:

    kill $(lsof -t -i:8000)

  2. If you want to kill the process immediately add -9 after the kill command: kill -9 $(lsof -t -i:8000)

$() is called command substitution. It allows a subshell to be executed and the result is then returned. In this case it is the Process ID (PID).