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:
-
Using the List Open File command
lsof
, typelsof -i:portNumber (e.g. lsof -i:8000)
to list the IP sockets. -
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)
-
In order to kill the process, type the following:
kill $(lsof -t -i:8000)
-
If you want to kill the process immediately add
-9
after thekill
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
).