howtouselinux

2 ways to Get Process Name using Port Number in Linux

Table of Contents

In Linux, we can get the process name from the port number by using ss, lsof command. In this blog post, we will show you how to do that.

We will also explain the difference between ss and lsof commands. Let’s get started!

Get process name using port number with ss command in Linux

To get the process name and ID associated with a particular port number in Linux, you can use the ss command with the -tulpn options.

The -t option tells ss to display TCP sockets, the -u option tells it to display UDP sockets, the -l option tells it to display listening sockets only, the -p option tells it to display the process ID and name associated with each socket, and the -n option tells it to display numerical addresses rather than trying to resolve them to hostnames.

Here’s an example:

$ ss -tulpn | grep :80
tcp LISTEN 0 128 *:80 *:*
users:(("httpd",pid=2867,fd=4),("httpd",pid=2866,fd=4),("httpd",pid=2865,fd=4),("httpd",pid=2864,fd=4),("httpd",pid=2863,fd=4),("httpd",pid=2862,fd=4),("httpd",pid=2861,fd=4)))

In this example, the ss command is displaying all TCP and UDP sockets that are in the listening state and shows the process ID and name associated with each socket.

The output is then piped to grep and filtered to show only lines that contain :80, which is the port number we’re interested in.

The output shows that the process name is httpd and there are several instances of it running, each with a different process ID.

You can use a similar command to find the process name and ID associated with any port number you’re interested in.

Just replace :80 with the desired port number in the grep command.

For example, to find the process name and ID associated with port number 443 (the default port for HTTPS), you would use the following command:

$ ss -tulpn | grep :443

Get process name using port number with lsof command in Linux

To use the lsof command to get the process name based on the port number in Linux, you can run the following command: lsof -i :[port_number], replacing [port_number] with the actual port number.

This will display a list of processes which have connections open to that specific port. You can then identify the process names from this output.

For example, if we need to find the process name running on port 8080, we can use the following command.

lsof -i :8080

This will display a list of all processes that have a network connection open on port 8080. The output will include the process ID (PID), the process name (COMMAND), and the user that owns the process (USER).

Here’s an example of the output you might see:

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 12345 user1 123u IPv4 12345 0t0 TCP *:8080 (LISTEN)

In this example, the process associated with port 8080 is a Java process with a PID of 12345 and is owned by user “user1”. The process is in a listening state, indicated by the (LISTEN) at the end of the line.

You can also use the -n flag to specify a specific network address or hostname. For example, to find the process associated with port 8080, you could use the following command:

lsof -n -i:8080

Here is another example to list all processes using the 5432 port (commonly used by postgres)

lsof -i :5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 777 1ma 7u IPv6 0x0123456789abcdef 0t0 TCP localhost:postgresql (LISTEN)
postgres 777 1ma 8u IPv4 0x1123456789abcdef 0t0 TCP localhost:postgresql (LISTEN)

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn