Linux: What Process is Listening on a Port

Understanding which processes are listening on specific ports in Linux is crucial for system administration and security. This knowledge helps diagnose network issues, secure the system, and ensure services are running as expected. In this article, we’ll guide you through identifying listening ports and the processes using them on your Linux system.

In this tutorial you will learn:

  • How to find which processes are listening on specific ports in Linux
  • How to list all listening ports on your Linux system
  • How to determine which process is using a particular port
Linux: What Process is Listening on a Port
Linux: What Process is Listening on a Port
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux-based operating system
Software netstat, lsof, ss
Other None
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Identifying Listening Ports and Processes in Linux

Monitoring listening ports and associated processes is essential for effective network management in Linux. Several methods and tools can accomplish this task, each providing different levels of detail.

  1. Using netstat: Netstat is a powerful network utility that displays various network-related information. It is commonly used to identify listening ports and their associated processes.
    # netstat -tuln

    This command lists all listening ports (both TCP and UDP) without resolving hostnames and port names. The output includes the protocol, local address, and the state of the ports. To include the process ID and name of the process listening on each port, use the following command:

    # netstat -tulnp

    This will add a column showing the PID and name of the program.

    identify listening ports and their associated processes
    identify listening ports and their associated processes
  2. Using lsof: The lsof command stands for ‘List Open Files’, and it is a versatile tool for reporting a list of all open files and the processes that opened them. Network connections are treated as files in Linux, so lsof can be used to identify the process listening on a port.
    # lsof -i -P -n

    This command lists all open Internet and network files. The -i option restricts the listing to network files, -P inhibits the conversion of port numbers to port names, and -n inhibits the conversion of network numbers to host names. To find the process using a specific port, say port 80, you can use:

    # lsof -i :80

    This will show you the command, PID, and user that is running the process listening on port 80.



  3. Using ss: ss (socket statistics) is a utility to investigate sockets. It can display more detailed and faster information about network connections than netstat.
    # ss -tuln

    This command lists all TCP and UDP listening sockets. Adding the -p option will show the processes using the sockets:

    # ss -tulnp

    This output includes similar information to netstat, but often with more detail and faster performance.

Conclusion

Understanding which processes are listening on which ports is a fundamental skill for Linux administrators. Using tools like netstat, lsof, and ss provides comprehensive insights into network connections, helping in troubleshooting and securing your system. Regular monitoring can prevent unauthorized access and ensure that legitimate services are running as intended.



Comments and Discussions
Linux Forum