keepcalculator

06 Numbers and Powers

Check Listening Ports: Netstat, lsof & ss Guide

Card No. 06·80163 · Updated · Standard formula, shown below

To check which ports are listening on your system, use the netstat, lsof, or ss command with the appropriate flags. On Linux, ss -tuln shows all TCP and UDP listening ports; on macOS, netstat -anv | grep LISTEN works; on Windows, netstat -ano | findstr LISTENING does the job. These commands reveal the port number, the service bound to it, and the process ID (PID) so you can identify what is running.

Why Check Listening Ports?

Every network service—web servers, SSH, databases, or a local dev server—opens a port to accept connections. Checking listening ports helps you:

  • Verify that a service started correctly and is reachable.
  • Find which application is using a specific port (e.g., port 8080 is occupied).
  • Detect unexpected services that might indicate malware or misconfiguration.
  • Troubleshoot firewall rules or port-forwarding issues.

For example, if you run python -m http.server 8000 and it fails with “Address already in use,” checking listening ports shows what is already bound to port 8000.

How Listening Ports Work

When a program wants to receive network traffic, it binds to a port and a network interface (like 0.0.0.0 for all interfaces or 127.0.0.1 for localhost only). The operating system’s network stack keeps a table of these bindings. The ss, netstat, and lsof commands read that table and display it in a human-readable format.

Each listening socket shows:

  • Protocol – TCP or UDP (TCP is connection-oriented; UDP is connectionless).
  • Local address – IP and port the service is listening on, e.g., 0.0.0.0:22 means all interfaces on port 22.
  • State – For TCP, LISTEN means actively waiting for connections; other states like ESTABLISHED are active connections, not listening.
  • Process – The PID and name of the program that owns the socket (if you have permission to see it).

Checking Ports on Linux (ss, netstat, lsof)

Modern Linux distributions include ss as part of iproute2. It is faster and more detailed than netstat. Basic usage:

ss -tuln

This shows TCP (-t) and UDP (-u) listening ports (-l) with numeric addresses (-n). To see the process name and PID, add -p (requires root for other users’ processes):

sudo ss -tulpn

If ss is not available, install net-tools or use the classic netstat:

netstat -tulpn

For a specific port, filter with grep:

ss -tuln | grep :8080

Or use lsof to find which process owns a port:

sudo lsof -i :8080

This shows the PID and command name, which is often more useful than just the port list.

Checking Ports on Windows

Windows uses netstat with different flags. To list all listening TCP ports with their PIDs:

netstat -ano | findstr LISTENING

The -a shows all connections, -n uses numeric addresses, -o displays the owning PID. To find the program name from the PID, use Task Manager or:

tasklist /FI "PID eq 1234"

Replace 1234 with the actual PID. For UDP, use netstat -ano | findstr UDP (UDP has no state, so it lists all bound UDP ports).

PowerShell offers an alternative:

Get-NetTCPConnection -State Listen | Select-Object LocalPort, OwningProcess

Checking Ports on macOS

macOS includes netstat but with different options. To see listening ports with process names:

netstat -anv | grep LISTEN

The -v flag adds the PID and process name. For a more readable output, use lsof (pre-installed):

sudo lsof -iTCP -sTCP:LISTEN -P -n

This lists all TCP listening ports with numeric ports and PIDs. To check a specific port:

lsof -i :8080

Common Problems and Misconceptions

One frequent misunderstanding is confusing “listening” with “established.” A listening port is open for new connections; an established connection is an active session. For example, netstat -an shows both, but only LISTEN entries matter for opening a port.

Another issue: netstat may not show the process name unless you run it as root/administrator. On Linux, without sudo, the PID column is blank for other users’ processes. On Windows, you must match the PID manually.

Also, a port can be listening on only one interface. If you see 127.0.0.1:8080 instead of 0.0.0.0:8080, the service is only accessible locally, not from other machines. This is a common cause of “I can’t connect from another device.”

When to Use Which Tool

  • Linux: Use ss by default; it’s installed on most modern distributions. Use lsof when you need to know the exact process or when you want to check a specific port.
  • Windows: Use netstat for a quick check; use PowerShell’s Get-NetTCPConnection for more details.
  • macOS: Use lsof for clarity; netstat is also available but less user-friendly.

For scripting or automation, ss with JSON output (ss -tuln -j) is handy on Linux.

Frequently Asked Questions

What does “0.0.0.0” mean in the listening address?

0.0.0.0 means the service is listening on all network interfaces (all IP addresses of the machine). This makes it reachable from any network the machine is connected to. In contrast, 127.0.0.1 means only local connections are accepted.

How do I check if a port is open from outside?

Use a port scanner like nc (netcat) or nmap from another machine. For example, nc -zv example.com 80 tests if port 80 is reachable. Note that your local firewall and router must allow the traffic.

Why does netstat show a port but ss doesn’t?

They may be reading different tables or the service may have changed. Also, netstat might show IPv6 sockets separately. Run ss -tuln and netstat -tuln and compare; any difference is usually due to flags or the tool’s version.

Can I see which program is using a port without being root?

On Linux, you can see your own processes without root, but for others you need sudo. On Windows, netstat -ano shows PIDs without admin rights, but you may need admin to see the process name in Task Manager. macOS requires sudo for lsof to show other users’ processes.

What is the difference between TCP and UDP listening?

TCP listening means the service accepts connections and maintains a session. UDP listening means the service is bound to a port to receive datagrams without a connection. UDP has no “LISTEN” state; it just shows as bound. For example, DNS uses UDP port 53.

How do I stop a service listening on a port?

Find the PID with ss -tulpn or lsof -i :port, then kill it with kill PID (or taskkill /PID PID /F on Windows). Be careful to identify the correct process before killing it.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →