How to Identify and Resolve “Address Already in Use” Errors on Linux
Learn to quickly diagnose the “Address already in use” error on Linux by listing listening ports with netstat, ss, and lsof, interpreting their output, filtering results, and freeing conflicted ports, with example commands and explanations of each option.
Background
The error message Address already in use appears when a program tries to bind to a network port that another process is already listening on. Resolving this requires identifying which ports are in use and which processes own them.
What Is a Listening Port?
A network port is identified by a number, an 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. Only one service can listen on the same IP address and port combination; attempting to start another service on that combination will trigger the error.
Using netstat to Inspect Listening Ports
netstatprovides information about network connections. To list all listening TCP and UDP ports together with the owning process, run: sudo netstat -tunlp The options mean: -t – show TCP ports. -u – show UDP ports. -n – display numeric addresses (no DNS lookup). -l – show only listening sockets. -p – show the PID and program name (requires root).
Typical output looks like:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0:22 0:* LISTEN 445/sshd
tcp6 0 0 :::80 :::* LISTEN 515/apache2
...Key columns are Proto (protocol), Local Address (IP and port), and PID/Program name (process identifier).
To filter for a specific port, pipe the output to grep. For example, to find the process listening on TCP port 22: sudo netstat -tnlp | grep :22 If the command returns nothing, the port is free.
Using ss (the modern replacement for netstat )
ssoffers similar functionality with faster execution and more detailed TCP state information. The command syntax is almost identical: sudo ss -tunlp The output format mirrors that of netstat, making migration straightforward.
Using lsof to List Open Files and Sockets
In Linux, everything is a file, including network sockets. lsof can list processes that have opened network files. To list all listening TCP sockets: sudo lsof -nP -iTCP -sTCP:LISTEN Options: -n – do not resolve hostnames. -P – do not resolve port numbers to names. -iTCP – restrict to TCP sockets. -sTCP:LISTEN – show only sockets in the LISTEN state.
Sample output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 445 root 3u IPv4 16434 0t0 TCP *:22 (LISTEN)
apache2 515 root 4u IPv6 16590 0t0 TCP *:80 (LISTEN)
mysqld 534 mysql 30u IPv6 17636 0t0 TCP *:3306 (LISTEN)Columns such as COMMAND, PID, and NAME identify the owning process and port.
Filtering for Specific Ports
To locate the process using a particular port (e.g., 3306 for MySQL), run: sudo lsof -nP -iTCP:3306 -sTCP:LISTEN The output will show the MySQL daemon as the owner of port 3306.
Conclusion
By combining netstat, ss, and lsof, you can reliably discover which services occupy which ports, filter the results for specific ports or processes, and take corrective action (stop the conflicting service or reconfigure the port) to eliminate 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.
