Marcio Cunha

How to Check Open Ports and Listening Connections Using the lsof Command

Learn how to inspect processes and network connections on Unix systems using the lsof command to identify which ports are open and actively listening for connections.

Marcio Cunha12 min
Also available in:EspañolPortuguês
Summary
  • The lsof utility maps open file descriptors managed by the operating system, treating network connections like standard files.
  • Targeted protocol and port filters prevent screen clutter during rapid network troubleshooting sessions.
  • Identifying the exact process responsible for an open port instantly resolves server startup conflicts.
  • Superuser permissions reveal hidden network connections maintained by background system daemons.
  • Routine port audits prevent accidental exposure of internal application services to public networks.

Understanding the Concept of Open Ports and the lsof Tool

When we set up a web server, a database, or any other internet-connected application, we need to ensure it can communicate with the outside world. In the Linux and Unix ecosystem, this communication relies on network ports, which act much like physical doors in a building, directing data traffic to the correct apartment or software application. Knowing which ports are open and waiting for connections—a state technically known as listening—is a fundamental skill for system administrators and developers. This is where the lsof command comes in, standing literally for 'list open files'. In practice, the Unix philosophy treats almost everything as a file, including network connections, TCP and UDP sockets, communication pipes, and disk storage. When a program decides to listen on a network port to welcome incoming visitors, the operating system opens a special file descriptor to manage that conversation. The lsof utility examines these internal kernel structures and delivers a detailed report of who is listening to what in real time.

The Basic Anatomy of Executing the lsof Command

Running lsof for the first time in the terminal can produce a flood of data on the screen, since a typical computer maintains thousands of open files and connections simultaneously to keep the operating system running smoothly. By default, typing just lsof without any arguments instructs the tool to list absolutely everything currently open, ranging from libraries loaded by text editors to temporary files and active network connections. To filter only network traffic and isolate what truly matters, we need to use specific parameters. The -i flag is our primary ally in this task, instructing the command to focus exclusively on files associated with network sockets. In practice, this means we ignore all disk files and concentrate solely on communication ports. Combining this flag with other modifiers allows us to refine our search to find the exact service occupying a specific port, sparing us the manual effort of searching for a needle in a digital haystack.

Filtering Connections by Protocol and Specific Port

Often, our goal when investigating a server is not to view every open socket, but rather to answer a very direct question: which program is using port 80 or port 443? To answer this, we can append specific modifiers to the network argument of lsof. If we want to inspect the TCP protocol, we can run lsof -i TCP, or narrow it down even further to a specific port using the syntax lsof -i :80. In practice, this colon notation followed by the port number tells the utility to ignore everything else and display only the process tied to that exact port. This becomes extremely useful when attempting to start a web server only to receive the frustrating error message that the address is already in use. With a single quick command, we discover whether an old instance of Nginx, Apache, or another application is blocking the port, allowing us to take immediate action to resolve the conflict.

Interpreting the Detailed Command Output

When lsof returns the results of a network query, it presents a structured table featuring informative columns that deserve our close attention. Typical columns displayed include the command or program name (COMMAND), the numerical process identifier (PID), the user owning the task (USER), the file descriptor (FD), the file type (TYPE), the device or address size (DEVICE), and finally the connection state and involved addresses (NAME). Looking at the state column is the key to identifying ports that are genuinely waiting for incoming connections, as they explicitly display the marker (LISTEN). If the marker shows states such as (ESTABLISHED), it means the connection has already been established and an active conversation is underway between two computers. Grasping this distinction prevents us from confusing an active data channel with a service that is merely available and listening for new clients.

The Power of Numeric Mode to Avoid Performance Delays

An important operational detail when utilizing lsof in production environments is the automatic translation behavior for IP addresses and ports into service names and domains. By default, lsof attempts to query local host files and DNS servers to convert numeric addresses into readable names, translating port 22 into ssh and the IP 127.0.0.1 into localhost. While this might seem convenient at first glance, on servers handling massive connection counts or experiencing network degradation, this lookup phase can introduce noticeable execution delays. To bypass this sluggishness and guarantee an instant response, we use the -n flag to disable hostname resolution and the -P flag to prevent translating port numbers into service names. In practice, executing sudo lsof -i -P -n ensures all data renders cleanly, strictly numerically, and immediately, making it ideal for automation scripts and fast diagnostic routines under pressure.

Identifying Connections and Processes of Other Users with Privileges

A common pitfall when starting with lsof is running the command as a regular user and noticing that many crucial ports simply do not appear in the output. The Linux operating system safeguards the privacy and security of running processes, preventing standard users from inspecting tasks owned by other accounts or system daemons running under the superuser root umbrella. In practice, if a database service is listening on an internal port but belongs to the postgres user, a developer logged in under their personal account might fail to see it without elevated privileges. To overcome this limitation and obtain a comprehensive overview of everything open on the machine, it is necessary to prefix the command with the sudo utility. This grants read access over internal kernel structures, allowing you to audit the server from end to end without blind spots.

Final Considerations and Continuous Security Auditing

Mastering the lsof command is an essential competence for any technical professional interacting with Unix-based servers. By treating network connections like files, the utility provides a unified and remarkably powerful interface to inspect the hidden behavior of our applications in real time. Whether resolving an annoying port conflict during local development or auditing a production server for unauthorized access, practical command of this tool elevates our level of technical autonomy. Incorporating routine checks for listening ports into our operational workflow helps maintain a clean, secure environment properly monitored against unwanted surprises.