متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 67Network · Linux

How to list open network connections

Who is connected to my server right now? Which program talks to that strange IP? Are there thousands of connections in TIME_WAIT? ss (socket statistics) answers all of this in one tool — it replaced the old netstat. This how-to lists connections with their programs, filters by port, state and address, counts them per IP, and helps you read the states.

  • 5 الدروس
  • 8 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

67.1

The quick answer

ss -s
ss -tunp
ss -tnp state established '( sport = :443 )'
ss -tn state established | awk 'NR>1 {split($4,a,":"); print a[1]}' | sort | uniq -c | sort -rn | head

-t TCP, -u UDP, -n numbers (no DNS), -p program (needs root to see other users' programs), -l listening only.

67.2

Step by step

  1. Overview — how many sockets in which state:
ss -s
Total: 812
TCP:   623 (estab 211, closed 380, orphaned 0, timewait 377)
  1. 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))
  1. Filter — by state, port and address (the filter syntax is powerful):
QuestionCommand
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 IPss -tn dst 203.0.113.50
Only listening portsss -tlnp
Connections of one programss -tnp | grep 'pid=2210'
  1. Count per remote IP (a bot, an attack, or one app with a pool) — quick answer, last line.
  2. Read the states:
StateMeans
ESTABOpen connection, data can flow.
LISTENA server waits for new connections.
TIME-WAITClosed by this side; kept for about a minute. Many are normal on busy web servers.
CLOSE-WAITThe other side closed, this program did not — many = a bug in the program (leak).
SYN-SENTTrying to connect, no answer yet (firewall? host down?).
SYN-RECVMany = maybe a SYN flood.
  1. Other views: lsof -i -P -n (by program, also files), and for NAT/firewall gateways conntrack -L (the kernel's connection tracking table).

الأوامر في هذا الدرس

67.3

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.

67.4

Common problems

ProblemCauseFix
Process column is emptyNot root — you see only your own programs.Run with sudo.
netstat: command not foundOld tool, not installed any more.Use ss (same idea, faster).
Thousands of TIME-WAITMany short connections (normal on proxies).Usually fine; use keep-alive / connection pools if ports run out.
Many CLOSE-WAIT that never go awayThe program does not close sockets.Bug in the app; restart is only a workaround.
Many SYN-SENT to one hostFirewall drops, or the host is down.Test the port: nc -zv HOST PORT.
An unknown program connects to an unknown IPCould be malware.Note PID and IP, ls -l /proc/PID/exe, isolate the server if in doubt.

الأوامر في هذا الدرس

67.5

Go further

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).

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل