Using netstat to find the process listening on specific port
Linux #linux #bash #shell-scripting
netstatis a command line tool to print network connections, routing tables, interface statistics etc
We can use netstat to check which process is using a specific port.
# print the process using port 4000
netstat -n -l -p | grep "4000"
Output
tcp 0 0 127.0.0.1:4000 0.0.0.0:* LISTEN 77369/ruby3.0
The above output is printed in the following format
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
We can read the output as, program 77369/ruby3.0 is listening on tcp port 4000 with ip address 127.0.0.1
Switches
-n means print numeric host. e.g. in the above example if -n is removed then localhost will be printed instead of 127.0.0.1 (this switch is not required for finding process listening on port but useful to know if you want to find ip address as well)
-l means print all ports which are in LISTEN state
-p means print the program id
we can also combine these switches, the command can be updated for above example like below
netstat -nlp | grep "4000"