Home/Engineer/Troubleshooting/Address already in use — Fin…
TroubleshootingBeginnerUbuntuDebianCentOSRHELmacOSAddress already in uselsofss

Address already in use — Finding and resolving port conflicts

When you hit a bind: address already in use error, use ss, lsof, and fuser to identify the process occupying the port, then free it safely or change the port.

Contents
  1. Finding the occupying process
  2. After identifying the process
  3. When the port is occupied in TIME_WAIT state
  4. Summary

If you see a bind: address already in use or EADDRINUSE error when starting a server, another process is already occupying that port.


Finding the occupying process

Bash
# Check occupancy of a specific port (e.g. 3000)
sudo ss -tlnp | grep :3000

# Example output
# LISTEN 0  511  0.0.0.0:3000  0.0.0.0:*  users:(("node",pid=12345,fd=22))
#                                                        ↑ process name  ↑ PID
Bash
# View all LISTEN ports and processes at once
sudo ss -tlnp

lsof

Bash
sudo lsof -i :3000

# Example output
# COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
# node    12345 deploy   22u  IPv4  123456      0t0  TCP *:3000 (LISTEN)

fuser

Bash
sudo fuser 3000/tcp
# Output: 3000/tcp:  12345   ← PID

After identifying the process

Bash
# Identify the process by PID
ps -p 12345 -o pid,ppid,user,comm,args

# Check the executable path
ls -la /proc/12345/exe

Method 1: When managed as a service

Bash
# Check the service name
sudo systemctl status 12345    # When it's hard to find by PID
# or
cat /proc/12345/comm           # process name

# Restart the service
sudo systemctl restart myapp

# or fully stop then start
sudo systemctl stop myapp
sudo systemctl start myapp

Method 2: Killing a leftover process

Bash
# Graceful shutdown (SIGTERM)
sudo kill 12345

# Force-kill if still running after 3 seconds
sleep 3 && sudo kill -9 12345

# Kill all processes matching a name
sudo pkill -f "node server.js"

Method 3: Change the port (without killing the process)

Bash
# Example: change the port in the app config (Node.js)
# PORT=3001 node server.js

# or edit the environment file
# .env: PORT=3001

# Change Nginx/Apache config
sudo nano /etc/nginx/sites-available/mysite
# listen 3001;  ← change, then reload
sudo nginx -t && sudo systemctl reload nginx

When the port is occupied in TIME_WAIT state

This happens when the port still won't open even after you killed the process.

Bash
# Check TIME_WAIT state
sudo ss -tn | grep TIME-WAIT | grep :3000

# Check wait timeout (default 60 seconds)
cat /proc/sys/net/ipv4/tcp_fin_timeout

Quick fix — use the SO_REUSEADDR option (in app code):

Node.js example:

JavaScript
server.listen({ port: 3000, host: '0.0.0.0' }, () => {});
// add reuseAddr: true to the server options (net module)

Quick fix — shorten kernel TIME_WAIT:

Bash
# Apply immediately (resets after reboot)
sudo sysctl -w net.ipv4.tcp_fin_timeout=15
sudo sysctl -w net.ipv4.tcp_tw_reuse=1

# Persist across reboots
echo "net.ipv4.tcp_fin_timeout = 15" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_tw_reuse = 1"     | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Summary

SituationCommand
Find occupying process`sudo ss -tlnp
Identify process by PIDps -p PID -o comm,args
Graceful shutdownsudo kill PID
Force killsudo kill -9 PID
TIME_WAIT waitsudo sysctl -w net.ipv4.tcp_fin_timeout=15
#Address already in use#lsof#ss#Troubleshooting#bind#netstat
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Questions & answers

Ask a question about this guide. We'll review and reply.