| by Arround The Web | No comments

Check Which Process Is Using a Port on Linux

What Is a Port?

In computer networking, a port represents a logical entry and exit point for a connection. Ports are based on software and are entirely virtual. These ports on a computer are managed by the operating system.

What Will We Talk About?

This quick tutorial demonstrates the various methods to determine which Linux process or service is currently listening on a specific port. Let’s talk about ports and their purpose.

How Are Ports Analogous to Physical Ports?

Just as physical ports help to interact with various peripheral devices connected to a computer, ports help the different services to communicate with each other. These services can be on the same computer or on different computers.

A Bit About Port of a Service

To listen for incoming connection requests, a process associates itself with a port number. Most processes are set up with a default port, and they have to use that port as per their specification. They do not automatically switch to the other port unless their configuration is explicitly modified.

A few examples of protocols and their associated default ports include the Secure Shell (SSH) protocol (port22), the Apache HTTP (port80), the MySQL database server (port3306), and so forth. You may use this information to discover which default port does a service utilizes.

The config file of these services can be edited to use some other port as well.

Checking the Ports on Linux

Let’s now see how to check what port/ports a process is using on Linux. Here, we will show you the different commands for this purpose.

1. Lsof Command

The lsof utility is helpful to obtain a list of the ports which are used by your system. Let’s consider the following example to get an information about a process (processes) using the TCP port 22:

$ sudo lsof -i TCP:22

The lsof command gives more information like the user’s name and what process IDs are linked to each process. It works with both TCP and UDP ports.

2. SS Command

The ss command is another way to find out which processes are linked to a certain port. Although lsof is the more common abbreviation, some people may find ss to be more handy.

Let’s look for the processes or services that listen on port 3306:

$ sudo ss -tunap | grep :3306

Let’s break down this command:

1. t: It tells the ss command to display the TCP packets.

2. u: It tells the ss command to display the UDP packets.

3. n: It is used to display the port numbers instead of their translations.

4. a: It is used to display the listening as well as non-listening sockets of all types.

5. p: It is used to display the processes that utilize a socket.

The result of the previous command shows which process is utilizing which port. You may also issue the following command:

$ sudo ss -tup -a sport = :80

Here, sport signifies the source port.

These two approaches may help you find the IDs of the processes that are connected to different ports.

3. Netstat Command

The netstat command shows the information about your network and can be used to fix the problems or change the way that your network is set up. It can also keep a close watch on your network connections.

This command is often used to see an information about inbound and outbound connections, routing tables, port listening, and usage stats. Although it has been rendered obsolete in recent years, netstat is still a useful tool for analyzing networks.

With the grep command, netstat can determine which process or service is using a certain port (by mentioning the port):

$ sudo netstat -ltnp | grep -w ':80'

The options used here can be classified as follows:

1. t: It only shows the TCP connection.

2. l: It is used to display the results in a list.

3. n: It displays addresses and port numbers in numerical format.

4. p: It displays the PID and program name which are associated with each socket.

4. Fuser Command

The fuser command determines the processes that utilize the files or sockets. You can use it to list the services which run on a specific port. Let’s take the example of port 3306 and see what services are running here:

$ sudo fuser 3306/tcp

This provides us with the process numbers using this port. You can use this process number to find the corresponding process names. For example, if the process number is 15809, the command to use here is as follows:

$ ps -p 15809 -o comm=

However, certain tools are required to identify the processes that utilize a non-standard port. “LSOF” is a tool for discovering what services are available on a network and what ports they use. Consider the following example. This shows how to list the UDP and TCP listening ports:

$ sudo lsof -Pni | egrep "(UDP|LISTEN)"

The following is a description of the options that are used here:

1. P: It suppresses the port service name lookup.

2. n: It displays the numeric network addresses.

3. i: It lists the IP sockets.

Both the ports and the associated processes are shown in the previously-mentioned result. This way is particularly useful for processes with non-default ports.

Conclusion

In this article, we talked about four possible Linux command-line tools and provided the examples on how to use them to find out which process is listening on a certain port.

Share Button

Source: linuxhint.com

Leave a Reply