Who's Using My Port? Quickly Find & Fix 'Address Already in Use' on Linux
This guide explains how to identify which process is occupying a network port on Linux using netstat, ss, and lsof commands, with detailed flag explanations and real output examples for troubleshooting 'Address already in use' errors.
What Is a Listening Port
A network port is identified by its number, associated IP address, and communication protocol (TCP or UDP). A listening port is a network port on which an application or process listens, acting as a communication endpoint. Each listening port can be opened or closed via a firewall. You cannot have two services listening on the same port on the same IP address. For example, if Apache is already using ports 80 and 443, installing Nginx will fail because those ports are already in use.
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 along with the service using each port and the socket state, run: sudo netstat -tunlp The options mean: -t – Show TCP ports. -u – Show UDP ports. -n – Show numerical addresses instead of resolving hosts. -l – Show only listening ports. -p – Show the PID and name of the listener process (requires root/sudo).
Example output:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 445/sshd
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 929/master
tcp6 0 0 :::3306 :::* LISTEN 534/mysqld
tcp6 0 0 :::80 :::* LISTEN 515/apache2
tcp6 0 0 :::22 :::* LISTEN 445/sshd
tcp6 0 0 :::25 :::* LISTEN 929/master
tcp6 0 0 :::33060 :::* LISTEN 534/mysqld
udp 0 0 0.0.0.0:68 0.0.0.0:* 966/dhclientKey columns: Proto – Protocol used by the socket. Local Address – IP address and port number the process is listening on. PID/Program name – PID and process name.
To filter results, pipe to grep. For example, to find the process listening on TCP port 22: sudo netstat -tnlp | grep :22 Output shows port 22 is used by the SSH server:
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 445/sshd
tcp6 0 0 :::22 :::* LISTEN 445/sshdIf output is empty, nothing is listening on that port. You can also filter by PID, protocol, state, etc. Note: netstat is deprecated and replaced by ss and ip, but remains widely used.
Using ss to Check Listening Ports
ssis the modern replacement for netstat. It lacks some netstat features but exposes more TCP states and is slightly faster. Command options are nearly identical, making migration easy. To get a list of all listening ports: sudo ss -tunlp Output is almost identical to netstat:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=445,fd=3))
LISTEN 0 100 0.0.0.0:25 0.0.0.0:* users:(("master",pid=929,fd=13))
LISTEN 0 128 *:3306 *:* users:(("mysqld",pid=534,fd=30))
LISTEN 0 128 *:80 *:* users:(("apache2",pid=765,fd=4),("apache2",pid=764,fd=4),("apache2",pid=515,fd=4))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=445,fd=4))
LISTEN 0 100 [::]:25 [::]:* users:(("master",pid=929,fd=14))
LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=534,fd=33))Using lsof to Check Listening Ports
lsof(list open files) is a powerful utility that shows information about files opened by processes. In Linux, everything is a file; sockets can be thought of as files written to the network. To list all listening TCP ports: sudo lsof -nP -iTCP -sTCP:LISTEN Options used: -n – Do not convert port numbers to port names. -P – Do not resolve hostnames; show numerical addresses. -iTCP -sTCP:LISTEN – Show only network files with TCP state LISTEN.
Example output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 445 root 3u IPv4 16434 0t0 TCP *:22 (LISTEN)
sshd 445 root 4u IPv6 16445 0t0 TCP *:22 (LISTEN)
apache2 515 root 4u IPv6 16590 0t0 TCP *:80 (LISTEN)
mysqld 534 mysql 30u IPv6 17636 0t0 TCP *:3306 (LISTEN)
mysqld 534 mysql 33u IPv6 19973 0t0 TCP *:33060 (LISTEN)
apache2 764 www-data 4u IPv6 16590 0t0 TCP *:80 (LISTEN)
apache2 765 www-data 4u IPv6 16590 0t0 TCP *:80 (LISTEN)
master 929 root 13u IPv4 19637 0t0 TCP *:25 (LISTEN)
master 929 root 14u IPv6 19638 0t0 TCP *:25 (LISTEN)Most columns are self-explanatory: COMMAND, PID, USER – Name, PID, and user of the program associated with the port. NAME – Port number.
To find the process listening on a specific port (e.g., 3306): sudo lsof -nP -iTCP:3306 -sTCP:LISTEN Output shows MySQL server using port 3306:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 534 mysql 30u IPv6 17636 0t0 TCP *:3306 (LISTEN)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.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical 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.
