How to find PID of process listening on a port in Linux? netstat and lsof command examples

In Linux, many times, you want to find out the PID of a process which is listening on a port e.g. if multiple tomcat servers are running on a host then, how do you find the PID of the tomcat listening on port 8080? There are many UNIX commands to find the process using a specific port, but I'll share what I use. I always use the netstat command with -p option, which displays process id of the process listening on a port. Btw, netstat is not the only command to find all processes using a particular port, you can also use lsof command for the same purpose. If you remember, we have used lsof earlier to find all the processes accessing a file but it can also be used to find all processes accessing a specific port. You will see the example of both netstat and lsof commands in this article to find PID of process listening on a specific port in Linux.

On the related note, it's very important for a Java developer to learn about Linux operating system and commands. The more you know about Linux Operating system and different shell e.g. bash, csh or ksh, the better you can support your Java application, that's why it's important to read The Linux Command Line: A Complete Introduction, the best guide to learning about shell commands in Linux. It will not only help you to learn about fundamental of Linux OS but also all important commands you need to work efficiently in Linux.

How to find PID of process listening on a port


Netstat command to find the PID of process listening on a port

So to find the PID of your Java server listening on port 8080, you can use following UNIX command:
$ netstat -nap | grep 8080
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 25414/java

here you go, 25414 is the PID of your tomcat server. Since tomcat is a Java web application it started with java command and that's why you see 25414/java.


Remember, if you are not logged in as root or the user under which your server is running, you might see following error:

No info could be read for "-p": geteuid()=XXX but you should be root

If you see this error, then just sudo as the user which is running the tomcat. If you know how Linux works, then you can solve this error pretty quickly but if you don't then you will have a hard time.

How to find the process id of a program listening on a port


lsof command example to find the processes using a specific port

Here is the example of lsof command to list the process listening on a port.

$ lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
java 25414 appuser 44u IPv4 3733348 TCP *:XXX (LISTEN)

just remember to -i option and color (:) before the port i.e.  :8080. Btw, if you don't find lsof command in your PATH, search in /usr/sbin, more often /usr/sbin is not added into user's PATH. In that you can run the command as shown below:

$ /usr/sbin/lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
java 25414 appuser 44u IPv4 3733348 TCP *:XXX (LISTEN)

It will display the same result.

That's all about how to find the PID of the process listening on a port in UNIX or Linux. You can use both netstat and lsof command to get the PID, but I mostly use netstat because sometimes I don't remember the port but I know the PID. Since in the case of netstat I am just doing grep, even if give PID it will fetch the line from netstat output then I can get the port on which a particular process is listening. Anyway, I will show you a couple of more tricks to find the port on which a particular port is listening in next tutorial.

Here is the summary of how to find the process listening on a particular port in UNIX:

UNIX command to find the PID of the process listening on specific port



Related UNIX and Linux command tutorials for Java Programers:
  • 10 examples of find command in UNIX (examples)
  • How to call REST web service from UNIX command line? (command)
  • 10 examples of grep command in UNIX (examples)
  • The difference between the soft link and hard link in Linux? (answer)
  • 10 examples of date command in Linux (examples)
  • How to get IP address from hostname and vice-versa in Linux (command)
  • 10 examples of tar command in UNIX (examples)
  • How to delete empty files and directory in UNIX (solution)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 5 examples of sort command in Linux (examples)
  • How to make directory tree in one command? (example)
  • 10 examples of chmod command in UNIX (examples)
  • UNIX command to find out how long a process is running? (answer)
  • 5 examples of kill command in Linux (examples)
  • How to how long argument of a process in Solaris (command)
  • 10 examples of xargs command in Linux (examples)
  • UNIX command to find the size of file and directory? (command)
  • 10 tips to work fast in UNIX? (tips)
Further Reading

No comments :

Post a Comment