How to Quickly Identify and Resolve “Address Already in Use” Errors on Linux
This guide shows how to quickly locate and resolve the “Address already in use” error on Linux and macOS by using netstat, ss, and lsof to list listening ports, interpret their output, and filter results for specific services or ports.
When you encounter the "Address already in use" message, you need to find out which service is listening on the conflicting port. This article explains how to list listening ports on Linux/macOS using netstat, ss, and lsof.
What is a listening port
A network port is identified by its number, associated IP address, and protocol (TCP or UDP). A listening port is a port on which an application or process waits for incoming connections, acting as a communication endpoint. Firewalls can open or close ports, and a port cannot be used by two services simultaneously.
Using netstat to check listening ports
netstatis a command‑line tool that provides information about network connections. To list all listening TCP and UDP ports with the owning processes, run: sudo netstat -tunlp The options mean: -t – show TCP ports -u – show UDP ports -n – display numeric addresses -l – show only listening sockets -p – show PID and program name (requires root)
Sample output:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0:22 0:* LISTEN 445/sshd
tcp 0 0 0:25 0:* LISTEN 929/master
tcp6 0 0 :::3306 :::* LISTEN 534/mysqld
tcp6 0 0 :::80 :::* LISTEN 515/apache2Key columns are Proto (protocol), Local Address (IP and port), and PID/Program name (process identifier).
You can filter the result with 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. Note that netstat is considered deprecated and has been superseded by ss and ip, though it remains widely used.
Using ss
ssis a newer alternative to netstat. It provides similar information with a faster execution speed. To list all listening ports, run: sudo ss -tunlp The output format is almost identical to netstat:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0:22 0:* users:("sshd",pid=445,fd=3)
LISTEN 0 128 *:3306 *:* users:("mysqld",pid=534,fd=30)
LISTEN 0 128 *:80 *:* users:("apache2",pid=515,fd=4)Using lsof
lsoflists open files, and because sockets are treated as files in Linux, it can show listening ports. To list all listening TCP ports:
sudo lsof -nP -iTCP -sTCP:LISTEN -n– do not resolve hostnames -P – do not convert port numbers to names -iTCP – select TCP sockets -sTCP:LISTEN – show only sockets in 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)To find the process using a specific port, e.g., 3306: sudo lsof -nP -iTCP:3306 -sTCP:LISTEN Output shows that the MySQL server is listening on port 3306.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
