linux-troubleshooting

Linux Port Already in Use: Find the Process and Fix It

If an application reports Address already in use, another socket may already occupy the address and port it wants. Start by identifying the listener, then decide whether to stop your own process or configure a different port.

sudo lsof -nP -iTCP:8080 -sTCP:LISTEN

Replace 8080 with the port from the error. This is an inspection command; it does not stop anything. -nP keeps addresses and ports numeric. -sTCP:LISTEN excludes established client connections from this TCP listener check.

Read the evidence before changing the process

A sample row might look like this:

COMMAND  PID   USER   FD  TYPE DEVICE SIZE/OFF NODE NAME
python3  4182  alex    3u  IPv4  12345      0t0  TCP 127.0.0.1:8080 (LISTEN)

The important values are the command, PID, user, and listening address. 127.0.0.1 is loopback; 0.0.0.0 means all IPv4 interfaces. Your PID will differ. Never copy a sample PID into a kill command.

Check the actual process you found:

ps -p 4182 -o pid,user,lstart,args

Use your real PID. A service manager may restart a process that you kill directly. If this is a managed service, inspect its status and configuration with the systemctl guide before deciding whether a stop is appropriate. If the existing listener is supposed to be running, change your new application’s port instead.

Practice: create and resolve your own port conflict

Use a Linux machine with Python 3 and lsof. This lab binds only to localhost and serves a newly created empty temporary directory. It does not expose your home directory.

In terminal A:

lab_dir=$(mktemp -d)
python3 -m http.server 8080 --bind 127.0.0.1 --directory "$lab_dir"

Expected: Python reports that it is serving on port 8080. Keep this terminal open. If port 8080 was already occupied before you started, inspect it and choose another unused port for every command in the exercise; do not stop someone else’s application.

In terminal B, try starting a second listener on the same address:

second_dir=$(mktemp -d)
python3 -m http.server 8080 --bind 127.0.0.1 --directory "$second_dir"

Expected: the second server exits with OSError: [Errno 98] Address already in use. If it unexpectedly starts, press Ctrl+C immediately and verify the first server and port number. The second command is intended only to demonstrate a failed bind. Once that command exits, remove its empty directory with rmdir -- "$second_dir".

Now identify the first listener from terminal B:

lsof -nP -iTCP:8080 -sTCP:LISTEN
lsof -t -iTCP:8080 -sTCP:LISTEN

The first command shows a complete row. The second prints only the PID. Match that PID with ps and confirm it is your Python process. Read what lsof -t means if you have encountered it inside a command substitution.

Return to terminal A and press Ctrl+C. Then run the listener check again in terminal B. Expected: no matching listener remains. You have completed the exercise when you can explain which process owned the port, demonstrate that it was your test server, and verify that stopping it released the port.

Remove the empty directory from terminal A:

rmdir -- "$lab_dir"

What if lsof returns nothing?

Check the exact port and protocol. The command above checks TCP listeners, not UDP. For UDP use sudo lsof -nP -iUDP:8080. You may need elevated access to see another user’s process.

A second tool can help:

sudo ss -ltnp 'sport = :8080'

The process may also be short-lived or inside a different network namespace. Run the inspection where the application runs. Container port mappings and host listeners are separate layers; do not assume a host-side command sees every container socket.

Why not paste a kill -9 one-liner?

Combining discovery and forceful termination removes your opportunity to check the owner. SIGKILL prevents normal cleanup, and a broad port match can include more than the listener you intended to stop. In this lab, Ctrl+C in the owning terminal gives you an unambiguous, reversible workflow.

For a background process you own, verify the current process before requesting normal termination. Use force only after understanding why it cannot exit normally. The process management guide covers signals and verification.

Continue practicing

Write a short ticket note: error, listening address, PID, owner, action, and result. That is the transferable skill—not memorizing one destructive command.

Start Chapter 1 free in Shell Samurai for guided Linux foundations. This Python exercise runs on your own suitable Linux environment; it is not a promise that every hosted training box includes Python or lsof.

References: lsof manual, Python http.server documentation.

Practice This in a Real Terminal

Shell Samurai gives you safe Linux missions so the commands actually stick. Chapter 1 is free; the full practice path is a one-time purchase, not another subscription.