Developer & Network Tools

netstat and ss: Seeing Open Connections

List listening ports and live connections, find the program behind them and read connection states.

Key takeaways

  • A listening socket waits for new connections; an established one is carrying a conversation.
  • Linux favors ss; Windows and macOS include netstat, with different options.
  • The local address tells you whether a service is reachable only from this machine or from the network.
  • Process names often require administrator or root rights.
  • An unfamiliar connection is a reason to investigate, not proof of anything malicious.

netstat and ss show the network sockets on your computer: which programs are waiting for connections, which connections are open, and what state each one is in. On Linux, ss -tulpn answers "what is listening here?" On Windows, netstat -ano does a similar job.

These tools look only at your own machine. That makes them ideal for finding which program grabbed a port, checking whether a service actually started, or reviewing what your computer is talking to.

Sockets, ports and states

A socket is an endpoint for network communication. For TCP, a connection is identified by four values: local address, local port, remote address and remote port. A port is the number that lets one computer run many services at once.

A web server listens on port 443. When your browser connects, your side uses a temporary, high-numbered port picked by the operating system, and the server side stays on 443. Many connections can share the server's port because the other three values differ.

Linux: ss

ss ships with the iproute2 tools on most distributions.

ss -tln
ss -uln
sudo ss -tulpn
ss -tan
ss -tn state established
ss -s

The letters are worth learning: t for TCP, u for UDP, l for listening sockets, a for all, n for numeric output without name lookups, and p for process information. ss -s prints a summary of counts.

Filters narrow large lists:

ss -tn '( dport = :443 or sport = :443 )'
ss -tn dst 192.0.2.10

Older systems may have netstat, from the net-tools package, where netstat -tulpn is the familiar equivalent.

Windows: netstat and PowerShell

netstat -ano
netstat -ano | findstr :8080
netstat -b
netstat -s
netstat -r

-a shows all connections and listening ports, -n keeps output numeric, and -o adds the process ID. -b shows the executable name and needs an elevated prompt; it can be slow. -s prints protocol statistics and -r the routing table.

To turn a process ID into a name, use tasklist /FI "PID eq 4321", replacing the number. PowerShell can do it in one step:

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

Get-Process -Id (Get-NetTCPConnection -LocalPort 8080 -State Listen).OwningProcess

For UDP, use Get-NetUDPEndpoint.

macOS

The macOS netstat doesn't use the Linux options for process names. lsof is often the more convenient tool:

netstat -an -p tcp
sudo lsof -nP -iTCP -sTCP:LISTEN
sudo lsof -nP -i :8080

-n and -P stop address and port numbers being replaced by names, which keeps output fast and unambiguous. lsof works on many Linux systems too.

Reading the local address

The local address column matters as much as the port.

What the listening address means
127.0.0.1 or ::1 Loopback only Programs on this machine can connect Good for local databases and dev servers One network address Listens on that interface address Reachable from networks that can route to it Breaks if the address changes 0.0.0.0, :: or * All interfaces of that family, or both Reachable wherever firewalls allow Check that this is intended 127.0.0.1 or ::1 Loopback only Programs on this machine can connect Good for local databases and dev servers One network address Listens on that interface address Reachable from networks that can route to it Breaks if the address changes 0.0.0.0, :: or * All interfaces of that family, or both Reachable wherever firewalls allow Check that this is intended

A development server bound to 127.0.0.1 can't be reached from your phone, which is a frequent source of confusion. A database bound to 0.0.0.0 on a cloud server is the opposite problem: it may be exposed to the internet unless a firewall blocks it.

Listening doesn't guarantee reachability. Host firewalls, router NAT and cloud security rules all sit between a listening socket and the outside world.

Reading a line of output

A hypothetical set of lines from ss -tanp might look like this, trimmed for width:

State   Local Address:Port     Peer Address:Port     Process
LISTEN  0.0.0.0:22             0.0.0.0:*             sshd
LISTEN  127.0.0.1:5432         0.0.0.0:*             postgres
ESTAB   192.168.1.34:52814     198.51.100.20:443     firefox
ESTAB   192.168.1.34:22        192.168.1.50:61022    sshd

The first line is a remote login server accepting connections on every IPv4 interface. The second is a database that only local programs can reach. The third is an outgoing connection: the local port is a high temporary number and the remote port is 443, so this machine is the client. The fourth is incoming: the local port is 22, so another device on the network has connected to us.

That rule of thumb, well-known port on one side and temporary port on the other, tells you the direction of most connections even though the tool doesn't spell it out.

The queue columns that ss and netstat print can help too. For an established connection, a receive queue that stays large means the local program isn't reading its data quickly enough. A send queue that stays large means data is waiting to be sent or acknowledged, pointing toward a slow receiver or a lossy path. For listening sockets, Linux uses these columns to describe the backlog of connections waiting to be accepted.

Understanding TCP states

TCP connections move through defined states. These are the ones you'll see most:

State Meaning
LISTEN Waiting for incoming connections
SYN-SENT We asked to connect and haven't had an answer
SYN-RECEIVED A request arrived and the handshake is in progress
ESTABLISHED Open and able to carry data
FIN-WAIT-1, FIN-WAIT-2 We started closing
CLOSE-WAIT The other side closed; our program hasn't yet
TIME-WAIT Closed, and lingering briefly to absorb stray packets

Spelling varies slightly between tools, for example ESTAB, SYN_SENT or TIME_WAIT.

Life of a client connection
1 SYN-SENT The client has sent a request to open a connection. 2 ESTABLISHED The handshake finished and data can move both ways. 3 FIN-WAIT The client closes first and waits for the other side. 4 TIME-WAIT A short pause before the details are forgotten. SYN-SENT The client has sent a request to open a connection. 1 ESTABLISHED The handshake finished and data can move both ways. 2 FIN-WAIT The client closes first and waits for the other side. 3 TIME-WAIT A short pause before the details are forgotten. 4

The opening steps are described in the TCP three-way handshake.

UDP has no connection setup, so its entries don't show these states in the same way. See TCP vs. UDP.

What unusual numbers suggest

Many entries in TIME-WAIT are normal on a busy client or server. They only become a problem if you run short of temporary ports, usually because software opens a new connection for every small request when it could reuse one.

A growing pile of CLOSE-WAIT connections points to an application bug: the remote side hung up and your program never closed its end. Restarting hides it; fixing the code solves it.

Connections stuck in SYN-SENT mean your attempts get no reply, often because a firewall silently drops them or the host is unreachable. Many half-open incoming connections in SYN-RECEIVED can indicate packet loss on the return path or a flood of connection attempts.

IPv4 and IPv6 side by side

You'll often see the same service listed twice, once on 0.0.0.0 and once on ::, or only an IPv6 entry that also accepts IPv4 connections, depending on how the program and system are configured. IPv6 addresses appear in square brackets in many tools so the port can be told apart, as in [::1]:8080.

This explains a puzzling development bug. The name localhost can resolve to ::1, 127.0.0.1 or both. If a server listens only on one and a client tries only the other, the connection is refused although everything seems to be running. Check the listening address, then connect using that exact address. Use -4 or -6 with ss, or the protocol options of other tools, to look at one family at a time.

Finding what holds a port

A classic error is "address already in use" when starting a server. The routine is:

  1. List listeners and filter for the port.
  2. Note the process ID and name.
  3. Decide whether that process should be running.
  4. Stop it properly through its service manager or window, or choose another port for your new program.

Force-killing a process you don't recognize can lose data or break a system service. Look it up first.

Checking for unexpected activity

Listing established connections can be reassuring or puzzling. Browsers, sync clients, updaters and chat apps keep many open, often to cloud and content delivery addresses whose names reveal little.

For each unfamiliar entry, find the program, check where its file lives and whether it's digitally signed, and consider when it was installed. A reverse lookup of the remote address may help, though many addresses have no useful name. If you suspect malware, use reputable security software and, on a work machine, tell your IT team. Software designed to hide can conceal its connections from local tools, so a clean list isn't proof of a clean system.

Counting and summarizing

On a busy server, the list is too long to read line by line. Summaries show the shape. On Linux, this counts TCP sockets by state:

ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn

In PowerShell, the equivalent is:

Get-NetTCPConnection | Group-Object State | Sort-Object Count -Descending

Compare the counts with what's normal for that machine. A sudden change is more informative than any absolute figure. If one remote address holds a surprising share of connections, find out whether it's a load balancer, a monitoring system or a reverse proxy before treating it as abuse. Behind a proxy, every visitor can appear to come from the proxy's address.

Combining with other tools

ss and netstat tell you what your machine thinks. To check whether another computer can reach a port, run a test from that other computer, as in testing a port. To see the packets themselves, use a capture tool, described in packet capture with Wireshark.

For continuous watching on Linux, watch -n 2 'ss -tan' refreshes the list every two seconds. Snapshots saved to files before and after starting a program make it easy to spot exactly what it opened.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Developer & Network Tools →

Latest news on this topic