Check Service Listening Port on Linux with 3 Easy Methods

Posted on

Check Service Listening Port on Linux with 3 Easy Methods

Check Service Listening Port on Linux with 3 Easy Methods

In this guide from the Linux Tutorials, we aim to demonstrate How To Check Service Listening Port on Linux. A port is an addressable network location implemented in an operating system to differentiate traffic destined for different services or applications. A port is always associated with the IP address of a host and the protocol type used for communication.

A service is said to be “listening” on a port when it binds to a port/protocol/IP address combination to wait for requests from clients. Upon receipt of the request, a one-to-one server-client dialog using the same port number is established. This article will provide you with the ability to Check Service Listening Port on Linux.

Steps To Check What Service is Listening on a Specific Port on Linux

If you’re asking: How do I find out which service is listening on a specific port? And how do I find out what program is listening on a specific TCP Port?, the following steps will provide the answers. Let’s dive into how to Check Service Listening Port on Linux.

Method 1. Check Service Listening Port on Linux with lsof command

The lsof command stands for List Of Open Files. This command provides a list of files that are opened, giving information about which files are opened by which process. This is a quick and easy way to Check Service Listening Port on Linux.

To see IPv4 Service Listening Port, use the command below:

lsof -Pnl +M -i4

Example output:

Use lsof command to Check Check Service Listening Port on Linux

To see IPv6 Service Listening Port, use the following command:

lsof -Pnl +M -i6

Example output:

Use lsof command to check listening port IPv6

The output provides information about which service is listening on a specific port.

For more information about the lsof command, use the command below:

man lsof

Method 2. Get Service Port Information with netstat command

You can use the netstat command to get information about the service port on Linux.

The netstat command, meaning network statistics, is a Command Prompt command used to display very detailed information about how your computer is communicating with other computers or network devices.

Specifically, it can show details about individual network connections, overall and protocol-specific networking statistics, and much more, all of which could help troubleshoot certain kinds of networking issues.

To do this, use the commands below:

# netstat -tulpn
Or
# netstat -npl

Example Output:

Use netstat command to check listening port

The last column, PID/Program name, gives information regarding the program name and port. To get more information about the netstat command, use the man page:

man netstat

Method 3. Use /etc/services File To Get Services Port

The /etc/services file is used by applications to translate human-readable service names into port numbers when connecting to a machine across a network. The file typically includes the service name, port/protocol, any aliases, and comments.

To do this, use the following commands:

$ cat /etc/services
$ grep 110 /etc/services
$ less /etc/services

Example Output:

Use /etc/services File to check service listening port

Alternative Methods to Check Service Listening Port on Linux

While the lsof, netstat, and /etc/services methods are effective, Linux offers other powerful tools for network analysis. Here are two alternative approaches to Check Service Listening Port on Linux:

1. Using the ss command (Socket Statistics)

The ss command is part of the iproute2 package and is designed to be a successor to netstat. It’s generally faster and provides more detailed socket information.

  • Explanation: ss directly retrieves information from the kernel, making it more efficient than parsing output from other tools. It offers numerous filtering options, allowing you to target specific ports or protocols.

  • Code Example:

    To list all listening TCP ports and the processes associated with them, you can use the following command:

    ss -ltnp
    • -l: Show only listening sockets.
    • -t: Show only TCP sockets.
    • -n: Do not try to resolve service names.
    • -p: Show process using socket.

    To find the process listening on a specific port (e.g., port 80):

    ss -ltnp sport = :80

    The output will display the process ID (PID) and name associated with the socket listening on port 80. This is another efficient way to Check Service Listening Port on Linux.

2. Utilizing nmap (Network Mapper)

nmap is a powerful network scanning tool that can identify open ports and the services running on them. Although primarily a security tool, it’s also useful for general network diagnostics.

  • Explanation: nmap probes network ports to determine their state (open, closed, filtered). It can also attempt to identify the service running on each open port based on the server’s response.

  • Code Example:

    To scan localhost for listening TCP ports and attempt to identify the services:

    nmap -sT -O localhost
    • -sT: TCP connect scan (requires a full TCP handshake).
    • -O: Enable OS detection.

    For a quicker scan of specific ports (e.g., 22, 80, 443):

    nmap -p 22,80,443 localhost

    nmap‘s output will show the state of each port (open, closed, filtered) and, if possible, the service name. Note that nmap might require root privileges for some operations, particularly OS detection. This is a slightly more involved method to Check Service Listening Port on Linux, but can be very useful.

Conclusion

You have now learned to Check Service Listening Port on Linux. Checking service listening ports on Linux helps ensure that required services are running and accessible while identifying potential security risks. It also helps troubleshoot network issues and manage firewall settings.

Hope you enjoy it. Please subscribe to us on Facebook and YouTube.

Also, you may also like these guides:

Open and Close Ports with FirewallD on Rocky Linux 8

Check Open Ports on Windows 10 and 11

Change RDP Port on Windows

Leave a Reply

Your email address will not be published. Required fields are marked *