How to Quickly Identify and Resolve “Address Already in Use” Errors on Linux
Learn how to diagnose the “Address already in use” error by listing listening ports on Linux and macOS using netstat, ss, and lsof, understand port concepts, interpret command output, filter results, and find which processes occupy specific ports.
What is a listening port
Network ports are 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 waits for incoming connections, acting as a communication endpoint. A port can be opened or closed by a firewall, and two services cannot listen on the same IP address and port simultaneously.
Using netstat to check listening ports
The netstat command can list all TCP and UDP ports that are in the LISTEN state, together with the owning process. The most common invocation is: sudo netstat -tunlp Options: -t – show TCP ports. -u – show UDP ports. -n – display numeric addresses instead of resolving host names. -l – show only listening sockets. -p – show the 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/apache2
...Key columns are Proto (protocol), Local Address (IP and port), and PID/Program name. To filter for a specific port, pipe the output to grep, e.g.: sudo netstat -tnlp | grep :22 If the result is empty, no process is listening on that port.
Note: netstat is considered deprecated on many modern systems; ss and ip provide similar functionality.
Using ss to check listening ports
The ss utility is a newer, faster alternative to netstat. Its options are largely compatible: sudo ss -tunlp The output format is similar to netstat and includes the same columns.
Using lsof to check listening ports
lsoflists open files, and because sockets are treated as files in Unix, it can show listening ports: sudo lsof -nP -iTCP -sTCP:LISTEN Important options: -n – do not resolve host names. -P – do not convert port numbers to names. -iTCP -sTCP:LISTEN – show only TCP 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 that owns a particular port, e.g. 3306: sudo lsof -nP -iTCP:3306 -sTCP:LISTEN The output shows that the MySQL server (PID 534) 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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
