Linux ports in use by process

How to find out which Port number a process is using

I want to be able to find out which port number a process is and filtering the results using a keyword. For example, I may want to quickly find out which port numbers are being used by «node» js apps. This did not work:

netstat tulnap | grep "node" 

By the way, i notice that on osx the command doesnt have a hyphen. Here are the results i got, not useful: Address Type Recv-Q Send-Q Inode Conn Refs Nextref Addr

3 Answers 3

This is how I found a solution:

 » lsof -i -P | grep node node 14489 me 12u IPv4 0x. 0t0 TCP *:4000 (LISTEN) 

Also if i knew the port and I was looking for the process name I would:

 » lsof -i :4000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 14489 me 12u IPv4 0x. 0t0 TCP *:terabase (LISTEN) 

Active Internet connections (w/o servers)

Active Internet connections (only servers)

What you’re doing may also never work since GREP is never guaranteed to deliver either no, one, or more than 1 results.

But, in context of your specific case, any process IS actually unique, and has a unique number. So, you must do your search first to find a process number.

Doing a simple grep will still give you either none, one, or more than one process number. But you’ll need to find an additional filter, so that you end up with 1 process number.

If you have 1 process number, you can check port(s) being used by that process.

What makes you think «node» is reported in PS ? It may also not be.

Источник

3 Ways to Find Out Which Process Listening on a Particular Port

A port is a logical entity that represents an endpoint of communication and is associated with a given process or service in an operating system. In previous articles, we explained how to find out the list of all open ports in Linux and how to check if remote ports are reachable using the Netcat command.

In this short guide, we will show different ways of finding the process/service listening on a particular port in Linux.

1. Using netstat Command

netstat (network statistics) command is used to display information concerning network connections, routing tables, interface stats, and beyond. It is available on all Unix-like operating systems including Linux and also on Windows OS.

Читайте также:  Lxqt для linux mint

In case you do not have it installed by default, use the following command to install it.

$ sudo apt-get install net-tools [On Debian/Ubuntu & Mint] $ sudo dnf install net-tools [On CentOS/RHEL/Fedora and Rocky Linux/AlmaLinux] $ pacman -S netstat-nat [On Arch Linux] $ emerge sys-apps/net-tools [On Gentoo] $ sudo dnf install net-tools [On Fedora] $ sudo zypper install net-tools [On openSUSE]

Once installed, you can use it with the grep command to find the process or service listening on a particular port in Linux as follows (specify the port).

Check Port Using netstat Command

In the above command, the flags.

  • l – tells netstat to only show listening sockets.
  • t – tells it to display tcp connections.
  • n – instructs it to show numerical addresses.
  • p – enables showing of the process ID and the process name.
  • grep -w – shows matching of exact string (:80).

Note: The netstat command is deprecated and replaced by the modern ss command in Linux.

2. Using lsof Command

lsof command (List Open Files) is used to list all open files on a Linux system.

To install it on your system, type the command below.

$ sudo apt-get install lsof [On Debian, Ubuntu and Mint] $ sudo yum install lsof [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/lsof [On Gentoo Linux] $ sudo pacman -S lsof [On Arch Linux] $ sudo zypper install lsof [On OpenSUSE]

To find the process/service listening on a particular port, type (specify the port).

Find Port Using lsof Command

3. Using fuser Command

fuser command shows the PIDs of processes using the specified files or file systems in Linux.

You can install it as follows:

$ sudo apt-get install psmisc [On Debian, Ubuntu and Mint] $ sudo yum install psmisc [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/psmisc [On Gentoo Linux] $ sudo pacman -S psmisc [On Arch Linux] $ sudo zypper install psmisc [On OpenSUSE]

You can find the process/service listening on a particular port by running the command below (specify the port).

Then find the process name using PID number with the ps command like so.

$ ps -p 2053 -o comm= $ ps -p 2381 -o comm=

Find Port and Process ID in Linux

You can also check out these useful guides about processes in Linux.

You might also like:

That’s all! Do you know of any other ways of finding the process/service listening on a particular port in Linux, let us know via the comment form below.

Источник

Linux Check Port Used by Process — Find Which Process is Listening on a Port

Learn how to use netstat, lsof, and ss commands to find which process is using a specific port in Linux. Discover tips and tricks to filter the output and improve your troubleshooting skills.

Читайте также:  Install without root linux

As a Linux user or administrator, it is essential to be able to check which process is using a particular port on your system. This knowledge can help you troubleshoot network issues or even identify potential security threats. In this article, we will explore several methods for finding out which process is listening on a port in Linux.

Netstat command

One of the most popular and straightforward ways to find which process is using a port in Linux is by using the netstat command. The netstat command is a built-in tool that displays network connections, routing tables, and network interface statistics.

To display the PID of a process using a specific port, run the following command:

$ netstat -nlp | grep :[port_number] 

The -n option disables name resolution, the -l option shows only listening sockets, and the -p option displays the PID and name of the program that is using the socket. The grep command filters the output to show only the line containing the port number you are interested in.

It is essential to note that the netstat command can display both TCP and UDP connections. Therefore, it is crucial to specify the protocol you want to check.

Here are some best practices for using the netstat command:

  • Use the -n option to disable name resolution and speed up the command’s execution time.
  • Use the -p option to display the PID and process name of the program using the socket.
  • Use the -t option to display only TCP connections, or the -u option to display only UDP connections.

Lsof command

Another powerful command-line tool for finding which process is using a particular port in Linux is the lsof command. The lsof command (List Open Files) lists all open files in a Linux system, including network sockets , processes, and files.

To list all processes using a specific port, run the following command:

The -i option specifies the Internet address or protocol family of the sockets, and the :[port_number] specifies the port number you want to check. The sudo command is necessary to run the lsof command with root privileges.

By default, the lsof command displays all types of sockets, including TCP, UDP, and RAW sockets. To show only TCP sockets, use the following command:

$ sudo lsof -i TCP:[port_number] 

Here are some tips for using the lsof command:

  • Use the -i option to specify the Internet address or protocol family of the sockets.
  • Use the -n option to disable name resolution and speed up the command’s execution time.
  • Use the -P option to show port numbers instead of port names.

SS command

The ss command (Socket Statistics) is another powerful tool for finding which process is listening on a port in Linux. The ss command is a modern replacement for the netstat command and provides more detailed information about TCP and UDP sockets.

Читайте также:  Linux mint чтобы пароль root

To display all listening sockets and show TCP connections, run the following command:

The -l option shows only listening sockets, the -t option shows only TCP sockets, and the -n option disables name resolution.

To display TCP and state information in addition to socket information, use the following command:

The -a option shows all sockets, the -n option disables name resolution, and the -t option shows only TCP sockets.

Here are some tips for using the ss command:

  • Use the -l option to show only listening sockets.
  • Use the -t option to show only TCP sockets.
  • Use the -n option to disable name resolution and speed up the command’s execution time.

Other methods

Besides the netstat, lsof, and ss commands, you can also use other methods to find which process is listening on a port in Linux. One of the most popular methods is using the nmap command (Network Mapper). The nmap command is a powerful network scanner that can be used to discover hosts and services on a network.

To check for open ports on a Linux system, use the following command:

The -sS option specifies the TCP SYN scan method, which sends a SYN packet to the target port and waits for a response. If the port is open, the target system responds with a SYN-ACK packet.

It is essential to note that the nmap command is more intrusive than the netstat, lsof, and ss commands and can be detected by some firewalls or intrusion detection systems.

Other helpful code examples for checking which process is using a port in Linux

In Shell , linux which process is using a port code example

$ sudo netstat -ltnp | grep ':80' 

In Shell , in particular, process runninng on particular port code sample

In Shell , check process on a given port linux code example

sudo netstat -peanut | grep ":8000 " 

In Shell case in point, check what ports are open linux

## if you use linux sudo ss -tulw 

In Shell , in particular, check process on port linux code sample

Conclusion

In conclusion, finding which process is listening on a port in Linux is an essential task for any Linux user or administrator. In this article, we explored several methods for checking which process is using a particular port, including the netstat, lsof, and ss commands, and the nmap command. By using these tools, you can troubleshoot network issues, identify potential security threats, and optimize your system’s performance.

Источник

Оцените статью
Adblock
detector