netstat Commands: A Comprehensive Guide

"Is anything listening on port 8080?" is a question you ask surprisingly often when running servers: a port conflict, a service that failed to bind, or an unknown process holding a port. netstat (Network Statistics) is the classic Linux tool for answering it. This guide covers the flags that matter, shows real output so you know what you are looking at, and covers the errors you will meet — including the one that happens the first time you try -p without root.

Linux netstat command output showing listening ports and sockets

What netstat Can Tell You

netstat is not always installed by default on modern systems; it is part of the net-tools package. The newer alternative ss ships with iproute2 and is faster on large outputs — see the comparison at the end.

# On Debian/Ubuntu systems
sudo apt-get install net-tools

# On RHEL/Rocky Linux/CentOS systems
sudo yum install net-tools

Essential netstat Commands

1. Find Which Process Is Listening on a Port

This is the command I run most: every TCP listener with its PID and program name.

netstat -nlpt

Example output on a machine running Keycloak and PostgreSQL:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      12879/java
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1042/postgres
tcp        0      0 127.0.0.1:22            0.0.0.0:*               LISTEN      890/sshd

0.0.0.0:8080 means the service listens on all interfaces — reachable from the network. 127.0.0.1:5432 means local connections only. If you expected a service to be reachable remotely and it only shows on 127.0.0.1, that is your answer.

2. List All Listening TCP and UDP Ports

netstat -tuln

Adds -u for UDP. Useful for a quick "what is open on this box" audit.

3. Show Active (Established) Connections

netstat -tn

Hides listeners and shows live connections — handy when checking who is connected to your application.

4. Protocol Statistics

netstat -s

Per-protocol counters: packets sent/received, retransmissions, checksum errors, and dropped segments. A growing Retransmitted packets count points to network loss or congestion.

5. Everything (Listeners + Connections + Unix Sockets)

netstat -a

6. Kernel Routing Table

netstat -r

Shows the default gateway and route rules. If your machine cannot reach the internet but networking is "up", check here that a default route exists.

7. Full Port Audit (TCP + UDP + PIDs)

netstat -tulpn

The standard "show me everything open on this box" command. The n keeps it fast on busy servers.

8. Filter by IP Version

netstat -4   # IPv4 only
netstat -6   # IPv6 only

9. Map Connections to Program Names

netstat -tp

Same as -nlpt but without the listener filter — includes established connections.

Putting It Together: Diagnosing a Port Conflict

Suppose your new service refuses to start with "Address already in use" on port 8080. Here is the exact sequence I use:

# 1. Confirm what is on 8080
netstat -tulpn | grep 8080

# 2. Result: another java process owns it
tcp6       0      0 :::8080                 :::*                    LISTEN      12879/java

# 3. Inspect that process to decide if it can be stopped
ps -fp 12879

You can then either stop the conflicting service or configure yours to use a different port.

Common Problems

Problem 1: "netstat: command not found"

Cause: net-tools is not installed on your distribution.

Solution: install it (commands above), or use the built-in ss: ss -tulpn is the direct modern equivalent.

Problem 2: "You are not allowed to see the process info" or PIDs shown as "-"

Cause: -p needs root permission to see processes owned by other users.

Solution: run with sudo netstat -tulpn.

Problem 3: service listening but unreachable from outside

Cause: the process bound to 127.0.0.1 instead of 0.0.0.0, or a firewall rule is blocking the port.

Solution: check the Local Address column in netstat -tulpn; then check iptables/firewall rules and test the port with netcat.

Problem 4: slow output / netstat seems stuck

Cause: without -n, netstat performs reverse DNS lookups on every address.

Solution: always include -n for production use.

netstat vs ss

Featurenetstatss
Availabilitynet-tools (often needs install)iproute2 (default on most distros)
Speed with many socketsSlowerFaster
Key commandnetstat -tulpnss -tulpn
Mature scriptsStill widely used in legacy scriptsPreferred for new work

Best Practices

Conclusion

netstat -tulpn answers most port questions in one line: what is listening, on which address, and owned by which process. Read the Local Address column carefully (0.0.0.0 vs 127.0.0.1), remember that -p wants root, and you will resolve the majority of binding and connection issues without leaving the terminal.

Related Articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles