How to Quickly Identify and Fix “Address Already in Use” Errors on Linux
This guide explains why the “Address already in use” message appears, defines listening ports, and shows step‑by‑step how to use netstat, ss, and lsof commands to list, filter, and pinpoint the processes occupying specific ports on Linux and macOS systems.
When you encounter the Address already in use error, the first step is to determine which ports are currently being listened to and which processes own them. The article explains how to use three common command‑line tools— netstat, ss, and lsof —to discover listening ports on any Linux or Unix‑like operating system, including macOS.
What Is a Listening Port?
A network port is identified by a number, an associated IP address, and a protocol (TCP or UDP). A listening port is a port on which an application or process is waiting for incoming connections, acting as a communication endpoint. Firewalls can open or close these ports, and a single port cannot be bound by two services simultaneously.
Using netstat to Inspect Listening Ports
netstatprovides information about network connections. To list all listening TCP and UDP ports with associated services, run: sudo netstat -tunlp The options mean: -t – show TCP ports -u – show UDP ports -n – display numeric addresses (no name resolution) -l – list only listening sockets -p – show the PID and program name (requires root privileges)
Typical output includes columns such as Proto, Local Address, and PID/Program name. To filter for a specific port, pipe the result to grep. For example, to find the process listening on TCP port 22: sudo netstat -tnlp | grep :22 If the output is empty, no process is listening on that port.
Using ss (Socket Statistics)
ssis a modern replacement for netstat. Its syntax is similar, and it provides faster, more detailed TCP state information. To list all listening ports: sudo ss -tunlp The output format is comparable to netstat, showing state, local address, and the owning process.
Using lsof to Find Open Files and Sockets
lsoflists information about files opened by processes; in Unix, sockets are treated as files. To list all listening TCP sockets: sudo lsof -nP -iTCP -sTCP:LISTEN Key columns include COMMAND, PID, USER, and NAME (the port number). To locate the process using a specific port, such as 3306: sudo lsof -nP -iTCP:3306 -sTCP:LISTEN The result shows the program (e.g., mysqld) that is bound to that port.
These three tools— netstat, ss, and lsof —provide complementary ways to diagnose port conflicts and resolve the “Address already in use” error.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
