How to View Open Ports in a Linux System

  1. 1. 1. Use the netstat command to view
  • Basic syntax<span>netstat -tuln</span>
    • • Here, the <span>-t</span> option indicates that only TCP protocol-related port information will be displayed; the <span>-u</span> option is used to show UDP protocol-related ports; the <span>-l</span> option indicates that only ports in the listening state will be listed, which are ports waiting to receive incoming connection requests; the <span>-n</span> option is to display information in numeric form (such as IP addresses and port numbers) rather than resolving port numbers to service names, making it easier to see the port numbers directly.
  • Example output and explanation
    Active Internet connections (only servers)
    Proto Recv - Q Send - Q Local Address           Foreign Address         State
    tcp        0      0 0.0.0.0:22              0.0.0.0:0               LISTEN
    tcp        0      0 127.0.0.1:25            0.0.0.0:0               LISTEN
    udp        0      0 0.0.0.0:68              0.0.0.0:0               ESTABLISHED
    • • In this output, the <span>Proto</span> column shows the protocol type (TCP or UDP). The <span>Recv - Q</span> and <span>Send - Q</span> columns represent the number of bytes in the receive and send queues, respectively. The <span>Local Address</span> column is the most important part, showing the local IP address and port number that the server is listening on, for example, <span>0.0.0.0:22</span> indicates that the server is listening on port 22 (usually for SSH) on all available local IP addresses, while <span>127.0.0.1:25</span> indicates listening on port 25 (usually for SMTP) on the local loopback address.<span>Foreign Address</span> column shows the remote address, which is usually <span>0.0.0.0:0</span> for listening ports, as no connection has been established yet.<span>State</span> column shows the state of the port, such as <span>LISTEN</span> indicating that the port is listening for client connection requests, and <span>ESTABLISHED</span> indicating that a connection has been established.
  • 2. 2. Use the ss command to view
    • Basic syntax<span>ss -tuln</span>,the meaning of the parameters is basically the same as that of <span>netstat -tuln</span>
    • Advantages:In high-load server environments, the <span>ss</span> command usually performs better than the <span>netstat</span> command, allowing for faster retrieval of port information. Its output format is similar to that of <span>netstat</span>, but may be more concise and clear. For example, it can handle a large number of connections more efficiently, reducing system resource usage.
    • Example output
      State       Recv - Q Send - Q      Local Address:Port      Peer Address:Port
      LISTEN      0      0         0.0.0.0:22               0.0.0.0:0
      LISTEN      0      0         127.0.0.1:25             0.0.0.0:0
      ESTAB       0      0         0.0.0.0:68               0.0.0.0:0
      • • Here, the <span>State</span><span>Recv - Q</span><span>Send - Q</span><span>Local Address:Port</span>、and <span>Peer Address:Port</span> columns have meanings similar to the corresponding columns in the output of the <span>netstat</span> command, clearly showing the port information in different states in the system.
  • 3. 3. Use the lsof command to view (from the process perspective)
    • Basic syntax<span>lsof -i -n -P</span>
      • • Here, the <span>-i</span> option indicates displaying all network-related files (in Linux, network connections are treated as files), the <span>-n</span> option indicates displaying IP addresses and port numbers in numeric form, and the <span>-P</span> option is to avoid converting port numbers to service names.
    • Example output and explanation
      COMMAND    PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
      sshd       1234  root    3u  IPv4  123456    0t0    TCP *:22 (LISTEN)
      sendmail   1235  root    4u  IPv4  123457    0t0    TCP 127.0.0.1:25 (LISTEN)
      dhclient   1236  root    6u  IPv4  123458    0t0    UDP *:68
      • • In this output, the <span>COMMAND</span> column shows the associated process name, such as <span>sshd</span> (SSH service process), <span>sendmail</span> (mail sending service process), etc. The <span>PID</span> column is the process ID number, the <span>USER</span> column shows the user who owns the process. The <span>FD</span> column indicates the file descriptor, which identifies how the process is associated with the network connection (e.g., <span>3u</span> indicates using the third file descriptor for read/write operations). The <span>TYPE</span> column shows the type of connection (e.g., <span>IPv4</span>), the <span>DEVICE</span> column indicates the device number, the <span>SIZE/OFF</span> column indicates the file size or offset, the <span>NODE</span> column indicates node information, and the <span>NAME</span> column shows detailed information about the network connection, including the listening IP address, port number, and state (e.g., <span>*:22 (LISTEN)</span> indicates listening on port 22 on all local IP addresses and in the listening state). This command allows you to see which processes are using which ports from the process perspective.

    Leave a Comment