In Linux, a port is a logical communication endpoint that is associated with a running service in the operating system. Ports are entirely managed by the operating system, and each port is associated with a specific service. Ports help administrators identify which services are running on the system.
Each port is usually assigned a number. Port numbers span from 0 to 65535. Ports lower than 1024 are referred to as privileged ports and are used for special protocols. For example, web traffic (HTTP) goes through port 80, while SSH listens to port 22. Ports greater than 1024 are called non-privileged ports and can be used for test purposes. For example, you can configure your webserver to run on one of those ports instead of its default port number.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux Processes queries on Ubuntu Linux System.
In this context, we shall look into how you can find out which processes are listening on which ports in Ubuntu 20.04 Linux System.
The Linux Netstat command is a portmanteau of the words network and statistics. As you might have guessed, it’s a command-line utility for displaying network & protocol statistics on a Linux system. Additionally, it allows you to print out the routing table, status of TCP & UDP endpoints, and information about your network interfaces.
To view which process is listening to a particular port, use the syntax below:
# netstat -pnltu | grep -w port_number
Let's break down the list of options:
p – Displays process name and ID
n – Displays port address
l – Lists listening sockets only
t – Prints out TCP connections
u – Prints out UDP connections
In the example below, we are probing for the service listening on port 80.
# netstat -pnltu | grep -w 80
The output indicates that it is the Apache webserver service that is listening on port 80.
To check the service listening on port 3306. Execute:
# netstat -pnltu | grep -w 3306
An acronym for List Open Files, the lsof command is a command used for revealing open files & directories. With the -i option, it can reveal the services listening to a particular port.
In the example below, we are searching for the service listening to TCP port 22, which is the SSH (Secure Shell) service.
# lsof -i TCP:22
Lastly, we have the fuser command. The fuser utility is a smart command-line tool that is used for locating processes using a socket, a file, or alternatively, a directory. It also does many things such as displaying the type of process, PID of each process, the user that owns the process.
To use fuser to find out the service listening to a port, first, find out the PID of the process as shown.
# fuser 22/tcp
Thereafter proceed and invoke the ps command to reveal the name of the service. Note that 34093 is the PID of the process that we got from the previous command
# ps -p 34093 -o comm=
This article covers methods to find out the processes that are listening to particular ports on your Linux system. A port is nothing but an endpoint of communication used in computer networks.
You have physical or wireless connections at the hardware level. At software or operating system level a port act as a logical construct that acts as communication port of network service such as SSH, HTTPD and more.
TCP and UDP are the most common port. TCP is an acronym for Transmission Control Protocol. UDP is an acronym for User Datagram Protocol.
How to Find Out Which Process Is Listening Upon a Port in Linux ?
1. netstat command or ss command – a command-line tool that displays network connections, routing tables, and a number of network interface statistics.
2. fuser command – a command line tool to identify processes using files or sockets.
3. lsof command – a command line tool to list open files under Linux / UNIX to report a list of all open files and the processes that opened them.
4. /proc/$pid/ file system – Under Linux /proc includes a directory for each running process (including kernel processes) at /proc/PID, containing information about that process, notably including the processes name that opened port.