Check the Processes Listening on a Given Port in Linux (3 Ways)

Check the Processes Listening on a Given Port in Linux (3 Ways)

Ports are an important element of communication among apps and software. They serve as an endpoint for communication. In the case of an operating system like Linux, a port is a logical construct that recognizes a particular process or a network service. At any given point in time, there might be multiple services listening to a port. In this article, learn how to check which processes are listening on a given port in Linux.

There are multiple ways of checking the stats. Here are three ways.

Method 1: With netstat Command

When there’s a need to display information regarding network connection, netstat is a go-to option for most admins. Besides that, it also can reveal information on interface stats and routing tables. You can use it for checking the processes that are listening to a port.

Before you can start using it, you need to install it first. Use the commands below:

For Debian/Ubuntu & Mint - $ sudo apt-get install net-tools

For CentOS/Fedora/Rocky Linux - $ sudo dnf install net-tools

For Arch Linux - $ pacman -S netstat-nat

For Gentoo - $ emerge sys-apps/net-tools

For openSUSE - $ sudo zypper install net-tools

After you’ve installed it, use grep command like this:

Code:
$ netstat -ltnp | grep -w ':80'
Here’s what the letters mean:

l - directs netstat to display the sockets that are listening

t - directs to show tcp connections

n - directs to display numerical addresses

p - permits to display process ID and process name

grep -w -displays matching of exact string (in the above case, :80)

Please note that the netstat command is deprecated. It is now replaced by ss command. So if netstat doesn’t work, try executing the ss command.

Method 2: With lsof Command

With lsof command, you can view all the open files in Linux.

To install it, use the following relevant command:

For Debian/Ubuntu & Mint - $ sudo apt-get install lsof

For CentOS/Fedora/Rocky Linux - $ sudo yum install lsof

For Arch Linux - $ sudo pacman -s lsof

For Gentoo - $ sudo emerge sys-apps/net-tools

For openSUSE - $ sudo zypper install net-tools

Now, you can view the list of processes or services listening to the port using this command:

Code:
$ lsof -i :80
Method 3: With fuser Command

With fuser command, you can know about the processes that are using a particular file. You can also view the PIDs of the processes.

Install fuser with the following relevant command:

For Debian/Ubuntu & Mint - $ sudo apt-get install psmisc

For CentOS/RHEL/Fedora/Rocky Linux - $ sudo yum install psmisc

For Arch Linux - $ sudo pacman -s psmisc

For Gentoo - $ sudo emerge -a sys-apps/psmisc

For openSUSE - $ sudo zypper install psmisc

To view the processes listening on a particular port, use this command:

Code:
$ fuser 80/tcp
To find processes using PID number, here’s the command to execute:

Code:
$ ps -p 2053 -o comm=
So that’s how you find about the processes that are active on a particular port.
Author
kumkumsharma
Views
3,302
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top