How to Check Port Usage on Linux with lsof, netstat, and a Python Script
This guide shows how to quickly determine whether specific service ports are occupied on a Linux system using commands such as lsof and netstat, explains the meaning of key parameters, and provides a ready‑to‑run Python program that monitors port availability on any IP address.
Linux Port Inspection
When developing a Python port‑scanning tool, you may need to verify whether specific service ports are already in use on a Linux host.
Using lsof
Run lsof -i:<port> (e.g., lsof -i:8000) to list processes that have opened the given port.
The output shows that port 8000 is occupied by the lightweight file system forwarding service lwfs .
Using netstat
Execute netstat -tunlp | grep <port> (e.g., netstat -tunlp | grep 8000) to display the PID/Program name associated with the port.
The command reveals the process details for port 8000.
Understanding the parameters
The options used in netstat are:
-t : TCP connections
-u : UDP connections
-n : Show numerical addresses
-l : Listening sockets
-p : Show program name/PID
-e : Extended information
Python port‑monitoring script
A ready‑to‑run Python script is provided to check whether a given IP address’s port is occupied.
Running the script produces output similar to the screenshot below, indicating the port status.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
