Linux netstat Command: Connections, Listening Ports, Routes, and Interface Statistics
Learn how to use Linux netstat to inspect sockets, listening ports, processes, TCP states, protocol statistics, interfaces, and routes, plus modern ss alternatives.
netstat reports information from the Linux networking stack. It can show active network sockets, services waiting for connections, routing information, network-interface counters, and protocol statistics.
A socket is a network communication endpoint used by a process. A socket is commonly identified by a protocol, an IP address, and a port. A port is a transport-layer number that identifies an application or service endpoint.
For command-line background, see Linux and Bourne Again Shell Bash.
Basic netstat usage
Running netstat without options displays its default connection report:
netstat
The default view generally focuses on active network connections rather than every socket on the system. Output commonly includes columns such as protocol, receive and send queues, local endpoint, remote endpoint, and a TCP state.
| Field | Meaning | What to watch for |
|---|---|---|
| Proto | The transport protocol, such as TCP or UDP. | Use it to distinguish connection-oriented TCP from connectionless UDP. |
| Recv-Q | Data received by the kernel that is waiting for the local application to read it. | A sustained nonzero value can indicate that the application is not reading quickly enough. |
| Send-Q | Data queued for sending to the remote endpoint. | Persistent growth can indicate a slow, unreachable, or nonresponsive peer. |
| Local Address | The local IP address and port. | A wildcard such as 0.0.0.0, ::, or an asterisk means the socket may be bound to all local addresses. |
| Foreign Address | The peer IP address and port. | A wildcard peer commonly appears for listening or unconnected sockets. |
| State | A TCP lifecycle state. | UDP normally does not have a TCP-style connection-state field. |
| PID/Program name | The process identifier and executable associated with the socket when process output is requested. | Use it to find the owner of an unexpected or conflicting port. |
Viewing all sockets and listening services
The -a option includes both active connections and listening sockets:
netstat -a
A listening socket belongs to a server process waiting for incoming connection requests. An established socket represents an active client-server session. Checking listening sockets is useful after starting a daemon because it verifies that the daemon opened the expected port.
Filtering TCP and UDP sockets
Use -t for TCP and -u for UDP. They can be combined:
netstat -t
netstat -u
netstat -tu
netstat -atu
The common combination -atu shows all TCP and UDP sockets, including listening sockets. UDP is connectionless and does not use TCP's handshake and lifecycle states, so UDP output normally lacks a meaningful State column.
Numeric addresses and ports
Use -n to prevent hostname and service-name resolution:
netstat -atun
Without -n, netstat may translate IP addresses through DNS and port numbers through the local service database. Numeric output is usually faster, avoids DNS delays, and makes troubleshooting less ambiguous. For example, 192.0.2.10:443 identifies a local HTTPS endpoint, while 198.51.100.20:53124 identifies a remote address and ephemeral client port.
Finding the process that owns a port
The -p option requests the PID and program name associated with each socket. Seeing every process often requires administrative privileges:
sudo netstat -tulpn
This command lists listening TCP and UDP sockets, uses numeric addresses and ports, and shows process ownership. It is useful when a service cannot start because a port is already in use, or when an unexpected service is listening.
For a web-service problem, first check whether the expected port is listening, whether it is bound to all interfaces or only loopback, and whether the expected process owns it. A binding such as 127.0.0.1:8080 accepts local connections only; a wildcard binding such as 0.0.0.0:8080 can accept connections through local IPv4 addresses, subject to firewall and service policy.
Reading a numeric socket listing
netstat -atun
Read each row as a pair of endpoints: the local address and port, and the foreign address and port. A listening server might show a wildcard local address and a wildcard foreign address. An established TCP session shows a specific local endpoint connected to a specific peer.
Continuous monitoring
Use -c to repeat the selected report:
netstat -atunc
Repeated output helps you observe connections appearing and disappearing, and lets you watch TCP states change. Stop continuous output with Ctrl+C.
Protocol statistics
The -s option prints counters maintained by network protocols:
netstat -s
netstat -st
Depending on the system, reports can include IP, TCP, UDP, and ICMP statistics. Counters may include packets or segments sent and received, receive and send errors, discarded packets, retransmissions, failed connection attempts, and ICMP messages.
Use netstat -st to focus on TCP statistics. Increasing retransmission counters can point to loss, congestion, or an unreliable path. Receive or send errors and discards can indicate malformed traffic, resource pressure, or filtering. Interpret counters over time rather than treating one absolute value as proof of a problem.
Network interface statistics
Use -i to display interface-level counters:
netstat -i
The report identifies interfaces and commonly includes receive and transmit packet totals, errors, drops, overruns, and collisions where the platform supplies those counters. Rising receive or transmit errors and drops can suggest a cable, NIC, driver, switch-port, MTU, or congestion problem. Capture the counters twice over an interval so that you can determine whether they are increasing.
Routing table display
Use -r to display the kernel routing table:
netstat -r
A route selects a destination network, an optional gateway, and an interface. Typical columns include:
- Destination: the network or host the route matches.
- Gateway: the next-hop router; a direct route may show no gateway.
- Genmask: the netmask used to determine how specific the destination match is.
- Flags: properties such as whether the route is up, a gateway route, or a host route.
- Metric: a preference value used when routes compete.
- Iface: the network interface used for traffic.
The default route is used when no more-specific route matches the destination. An absent or incorrect default gateway can make remote networks unreachable. An incorrect interface or destination route can also send traffic along the wrong path.
TCP connection states
TCP is a connection-oriented transport protocol with ordered delivery and a lifecycle. Netstat can expose that lifecycle through the State column.
| State | Meaning | Troubleshooting significance |
|---|---|---|
| LISTEN | A server socket is waiting for connection requests. | Confirms that a service is awaiting connections on the displayed local address and port. |
| ESTABLISHED | The TCP connection is active. | Shows a functioning session between the displayed endpoints. |
| SYN_SENT | The local host sent a connection request and is waiting for a response. | Persistent entries can suggest routing, firewall, reachability, or remote-service problems. |
| SYN_RECEIVED | The local host received a request and has sent a response while completing the handshake. | Many persistent entries can suggest missing return traffic or a remote client that does not complete the handshake. |
| FIN_WAIT_1 | The local host has begun closing and is waiting for the peer's response. | Short-lived entries are normal; persistence can indicate close-handshake issues. |
| FIN_WAIT_2 | The local host received the peer's acknowledgment of its close but awaits the peer's final close. | Long-lived entries can indicate a peer or application that does not finish closing. |
| TIME_WAIT | The local endpoint has closed and retains state briefly to handle delayed packets. | Many entries can be normal after frequent short-lived connections. |
| CLOSE_WAIT | The peer closed its side, but the local application has not closed its socket. | Persistent entries usually indicate application cleanup problems or a resource leak. |
| LAST_ACK | The local host has sent its final close and awaits acknowledgment. | Persistent entries can indicate that the peer is not acknowledging the close. |
| CLOSED | No connection exists. | Usually not shown for active socket listings because the socket has ended. |
Common troubleshooting workflow
- Check the required service with
sudo netstat -tulpn. Verify the intended port and local bind address. - Confirm the PID and program name. If another process owns the port, decide whether to stop it or configure the new service to use a different port.
- Inspect active TCP sessions with
netstat -tanand examine states such asESTABLISHED,SYN_SENT, andCLOSE_WAIT. - Review
Recv-QandSend-Qwhen an application is slow or connections appear stalled. - Use
netstat -stto review retransmissions, errors, discards, and send/receive counters. - Use
netstat -ito inspect interface counters andnetstat -rto verify route selection. - Use numeric output such as
netstat -atunduring troubleshooting to remove name-resolution ambiguity.
Example: a service cannot be reached
Run sudo netstat -tulpn. If no listener exists, inspect the service status and configuration. If the service listens only on loopback, review its bind address. If it listens on the intended interface, continue by checking firewall policy, routing, and whether the client is using the correct address and port.
Example: a port is already in use
Locate the requested port in sudo netstat -tulpn, record the PID and program name, and determine whether the existing owner should be stopped or whether the new service should use another configured port.
Example: connections remain in CLOSE_WAIT
netstat -tan
sudo netstat -tanp
The remote side has closed these connections, but the local application has not released its sockets. Investigate application behavior, connection cleanup, and possible resource leaks.
Example: connections are stuck during establishment
netstat -tan
netstat -r
Look for persistent SYN_SENT or SYN_RECEIVED entries. Check the destination route, firewall policy, remote service availability, address correctness, and return-path routing.
Example: packet loss or interface errors
netstat -i
Compare receive and transmit errors, drops, overruns, and other counters over time. Correlate increasing values with physical cabling, the NIC, driver messages, switch-port configuration, MTU, and congestion.
Option reference
| Option | Purpose | Typical use | Example |
|---|---|---|---|
-a | Include listening and non-listening sockets. | Find services waiting for connections. | netstat -a |
-t | Show TCP sockets. | Inspect connection-oriented traffic. | netstat -t |
-u | Show UDP sockets. | Inspect connectionless services. | netstat -u |
-n | Use numeric addresses and ports. | Avoid DNS and service-name lookups. | netstat -atun |
-c | Repeat the report continuously. | Observe socket changes over time. | netstat -atunc |
-s | Show protocol statistics. | Investigate errors, retransmissions, and counters. | netstat -s |
-p | Show PID and program name. | Identify the process owning a socket. | sudo netstat -tulpn |
-i | Show interface statistics. | Inspect packet and error counters. | netstat -i |
-r | Show the kernel routing table. | Check gateways and interface selection. | netstat -r |
-tulpn | Combine TCP, UDP, listening, process, and numeric views. | Find listening services and owners. | sudo netstat -tulpn |
-atun | Show all TCP and UDP sockets numerically. | Inspect every socket without name lookups. | netstat -atun |
Modern alternatives: ss and ip
ss provides comparable socket-inspection capabilities and is commonly supplied through the iproute2 package. It is generally preferred on current Linux systems.
| Task | Legacy command | Modern command |
|---|---|---|
| Show listening TCP and UDP sockets | netstat -tulpn | sudo ss -tulpn |
| Show socket process ownership | sudo netstat -anp | sudo ss -anp |
| Show routes | netstat -r | ip route |
| Show interface statistics | netstat -i | ip -s link |
| Show protocol and socket statistics | netstat -s | ss -s for socket summaries; use protocol-specific tools or kernel statistics when detailed protocol counters are required. |
For new scripts and operational habits, learn ss for sockets, ip route for routing, and ip -s link for interface counters. Keep netstat knowledge because it remains present on older systems and in many troubleshooting guides.
Exam-relevant notes
-aincludes listening sockets; without it, a server may not appear in the default active-connection view.-navoids DNS and service-name lookups, which improves speed and removes resolver ambiguity.-pidentifies the owning PID and program, but administrative privileges may be needed for complete results.TIME_WAITis often normal after many short-lived TCP sessions.- Persistent
CLOSE_WAITusually points to a local application that failed to close a peer-terminated connection. - TCP has lifecycle states; UDP normally does not have a TCP-style state field.
- The default route handles destinations for which no more-specific route exists.