π§° Linux/Unix: Diagnostics β netstat
, ss
, traceroute
, mtr
Explained with Output & Usage
π§² Introduction β Why Learn Linux Network Diagnostics?
When network issues arise, system administrators need quick, reliable tools to diagnose connections, identify bottlenecks, or trace packet paths. Linux provides powerful utilities like netstat
, ss
, traceroute
, and mtr
to inspect sockets, monitor ports, and track routes in real time.
π― In this guide, youβll learn:
- How to analyze open ports and active connections
- How to trace network paths using hops and latency
- The difference between traditional and real-time tools
π 1. netstat
β Legacy Tool for Network Statistics
β
What is netstat
?
netstat
displays network connections, routing tables, interface stats, and listening ports. Itβs now deprecated in favor of ss
.
π οΈ Syntax:
netstat [options]
πΉ Common Options:
Option | Meaning |
---|---|
-t | Show TCP connections only |
-u | Show UDP connections only |
-l | Show listening ports |
-n | Show numeric addresses |
-p | Show PID and program names |
-r | Show routing table |
π§ͺ Example: Show all TCP listening ports
netstat -tlnp
π€ Output:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1056/sshd
π§ Shows active TCP ports and which process is using them.
π¦ Install with:
sudo apt install net-tools
π 2. ss
β Modern Replacement for netstat
β
What is ss
?
ss
(socket statistics) is faster and more accurate than netstat
for displaying socket connections and performance.
π οΈ Syntax:
ss [options]
πΉ Common Options:
Option | Meaning |
---|---|
-t | Display TCP sockets |
-u | Display UDP sockets |
-l | Show only listening sockets |
-n | Show numerical addresses/ports |
-p | Show process using the socket |
-a | Display all sockets |
π§ͺ Example: Show listening TCP ports with process names
ss -tlnp
π€ Output:
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1056,fd=3))
π§ Much faster than netstat
and works well with scripting.
π 3. traceroute
β Track Packet Path to Destination
β
What is traceroute
?
traceroute
maps the route packets take to reach a destination, showing each network hop and its latency.
π οΈ Syntax:
traceroute [destination]
πΉ Options:
Option | Description |
---|---|
-n | Don’t resolve hostnames |
-w | Set timeout per probe |
-m | Max number of hops (default: 30) |
π§ͺ Example: Trace route to Google
traceroute google.com
π€ Output:
1 192.168.0.1 (192.168.0.1) 2.123 ms 1.452 ms 1.478 ms
2 100.65.32.1 (100.65.32.1) 5.876 ms 4.903 ms 4.832 ms
3 ...
π§ Each line is a hop; shows how long each hop takes in milliseconds.
π¦ Install with:
sudo apt install traceroute
π‘ 4. mtr
β Real-Time Traceroute with Stats
β
What is mtr
?
mtr
combines traceroute
and ping
into a real-time visual diagnostic tool. It continuously sends probes and updates live statistics about packet loss and latency.
π οΈ Syntax:
mtr [destination]
π§ͺ Example: Run mtr to google.com
mtr google.com
π€ Output:
My traceroute [v0.94]
Host Loss% Snt Last Avg Best Wrst StDev
1. 192.168.0.1 0.0% 10 1.1 1.2 1.0 1.5 0.2
2. 100.65.32.1 0.0% 10 5.5 5.6 5.3 6.2 0.3
...
π§ Best tool for detecting packet loss and instability across the route.
π¦ Install with:
sudo apt install mtr
π§ Diagnostic Tool Comparison
Tool | Purpose | Realtime | Best For |
---|---|---|---|
netstat | Legacy socket & port display | β | Quick one-off checks (legacy) |
ss | Modern socket display | β | Active connections, performance |
traceroute | Trace path to destination | β | Path tracking, hop delays |
mtr | Real-time traceroute & ping | β | Detect packet loss, instability |
π Summary β Recap & Next Steps
These diagnostic tools help you detect connectivity issues, trace network hops, monitor open ports, and spot real-time packet drops. Each serves a unique role in Linux networking diagnostics.
π Key Takeaways:
- Use
ss
for fast socket/port inspection - Use
netstat
for legacy compatibility - Use
traceroute
to map the route of packets - Use
mtr
to monitor route quality in real time
β FAQs
β What replaced netstat
in modern Linux?
β
The ss
command is the recommended replacementβfaster and more detailed.
β Can I use traceroute
and mtr
on servers without GUI?
β
Yes. Both work entirely in the terminal.
β Whatβs the difference between traceroute
and mtr
?
β
traceroute
is one-time; mtr
is live, continuous, and shows packet loss stats.
β How do I find which process is using a port?
β
Use:
sudo ss -tulnp | grep 8080
β How to interpret packet loss in mtr
?
β
Anything >0% is suspicious. Check routers showing consistent loss across hops.
Share Now :