هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- Overview — how many sockets in which state:
ss -s
Total: 812
TCP: 623 (estab 211, closed 380, orphaned 0, timewait 377)- All connections with the program behind them:
ss -tunp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
tcp ESTAB 0 0 10.0.1.11:443 203.0.113.50:51544 users:(("nginx",pid=1234,fd=12))
tcp ESTAB 0 0 10.0.1.11:48122 10.0.2.21:5432 users:(("python3",pid=2210,fd=7))- Filter — by state, port and address (the filter syntax is powerful):
| Question | Command |
|---|---|
| Who is connected to my HTTPS? | ss -tn state established '( sport = :443 )' |
| Where does my server connect to on 5432? | ss -tn state established '( dport = :5432 )' |
| Everything with one IP | ss -tn dst 203.0.113.50 |
| Only listening ports | ss -tlnp |
| Connections of one program | ss -tnp | grep 'pid=2210' |
- Count per remote IP (a bot, an attack, or one app with a pool) — quick answer, last line.
- Read the states:
| State | Means |
|---|---|
ESTAB | Open connection, data can flow. |
LISTEN | A server waits for new connections. |
TIME-WAIT | Closed by this side; kept for about a minute. Many are normal on busy web servers. |
CLOSE-WAIT | The other side closed, this program did not — many = a bug in the program (leak). |
SYN-SENT | Trying to connect, no answer yet (firewall? host down?). |
SYN-RECV | Many = maybe a SYN flood. |
- Other views:
lsof -i -P -n(by program, also files), and for NAT/firewall gatewaysconntrack -L(the kernel's connection tracking table).
Check that it worked
You can name every unexpected connection: which program (PID), which remote address, which state. Unknown programs talking to unknown IPs are a security question — see the next steps.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Process column is empty | Not root — you see only your own programs. | Run with sudo. |
netstat: command not found | Old tool, not installed any more. | Use ss (same idea, faster). |
Thousands of TIME-WAIT | Many short connections (normal on proxies). | Usually fine; use keep-alive / connection pools if ports run out. |
Many CLOSE-WAIT that never go away | The program does not close sockets. | Bug in the app; restart is only a workaround. |
Many SYN-SENT to one host | Firewall drops, or the host is down. | Test the port: nc -zv HOST PORT. |
| An unknown program connects to an unknown IP | Could be malware. | Note PID and IP, ls -l /proc/PID/exe, isolate the server if in doubt. |
Go further
- Command pages: ss, lsof, awk.
- Next how-tos: Find which process uses a port, Capture network traffic with tcpdump, See who is connected to a database.
- Learn it: RHCSA — Check ports, RH124 — Ports.
- Full guide: Install and use Nmap (the view from outside).
1What does CLOSE-WAIT mean, and what do many of them tell you?
The other side closed the connection but this program did not; many of them usually mean a bug in the program.
2Which option shows the program behind each connection?
-p (run as root to see all programs).