GNU/Linux >> LINUX-Kenntnisse >  >> Linux

Linux netstat Command Tutorial für Anfänger (8 Beispiele)

Wenn Sie neu in der Linux-Befehlszeile sind und daran interessiert sind, Netzwerksachen zu lernen, gibt es viele Befehlszeilen-Dienstprogramme, die Sie kennen sollten. Ein solches Tool ist netstat . In diesem Artikel werden wir diesen Befehl anhand einiger leicht verständlicher Beispiele besprechen.

Aber bevor wir das tun, ist es erwähnenswert, dass alle diese Beispiele auf einem Ubuntu 16.04 LTS-Rechner getestet wurden.

Linux netstat-Befehl

Mit dem Befehl netstat können Sie Netzwerkverbindungen, Routing-Tabellen, Schnittstellenstatistiken, Masquerade-Verbindungen und Multicast-Mitgliedschaften drucken. Es folgt die Syntax:

netstat [OPTIONS]

So erklärt es die Manpage:

       Netstat  prints  information about the Linux networking subsystem.  The
       type of information printed is controlled by  the  first  argument,  as
       follows:

   (none)
       By  default,  netstat  displays  a  list of open sockets.  If you don't
       specify any address families, then the active sockets of all configured
       address families will be printed.

   --route , -r
       Display  the kernel routing tables. See the description in route(8) for
       details.  netstat -r and route -e produce the same output.

   --groups , -g
       Display multicast group membership information for IPv4 and IPv6.

   --interfaces, -i
       Display a table of all network interfaces.

   --masquerade , -M
       Display a list of masqueraded connections.

   --statistics , -s
       Display summary statistics for each protocol.

Im Folgenden finden Sie einige Beispiele im Q&A-Stil, die Ihnen eine gute Vorstellung davon vermitteln sollen, wie der Befehl netstat funktioniert.

Q1. Wie verwende ich den netstat-Befehl?

Die grundlegende Verwendung ist ziemlich einfach - führen Sie einfach den netstat-Befehl ohne Optionen aus.

netstat

In meinem Fall wurde beispielsweise die folgende Ausgabe erzeugt:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.1.7:59510       mails11.telegram.:https ESTABLISHED
tcp        0      0 192.168.1.7:57318       13.70.5.200:https       ESTABLISHED
tcp        0      1 192.168.1.7:51238       52.114.32.7:https       SYN_SENT   
tcp      390      0 192.168.1.7:60392       del03s09-in-f10.1:https ESTABLISHED
tcp        0      1 192.168.1.7:51240       52.114.32.7:https       SYN_SENT   
tcp        0      0 192.168.1.7:50196       del03s10-in-f10.1:https TIME_WAIT  
tcp        0      0 192.168.1.7:41996       maa05s05-in-f4.1e:https TIME_WAIT  
tcp        0      0 192.168.1.7:56680       wa-in-f94.1e100.n:https ESTABLISHED
tcp        0      0 192.168.1.7:54578       del03s10-in-f14.1:https TIME_WAIT  
tcp        0      0 192.168.1.7:51310       del03s10-in-f2.1e:https TIME_WAIT

Daher zeigt der Befehl standardmäßig eine Liste aller offenen Sockets an.

Q2. Wie bringt man netstat dazu, numerische Adressen anzuzeigen?

Standardmäßig versucht der Befehl netstat, symbolische Host-, Port- oder Benutzernamen zu ermitteln. Dies kann das Tool verlangsamen. Wenn Sie jedoch mit numerischen Werten zufrieden sind, können Sie das Tool mit der Befehlszeilenoption -n dazu bringen, numerische Werte auszugeben.

netstat -n

In meinem Fall wurde beispielsweise die folgende Ausgabe erzeugt:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.1.7:59510       149.154.171.22:443      ESTABLISHED
tcp        0      0 192.168.1.7:57318       13.70.5.200:443         ESTABLISHED
tcp        0      0 192.168.1.7:51682       52.114.32.7:443         TIME_WAIT  
tcp        0      0 192.168.1.7:54884       172.217.161.14:443      TIME_WAIT  
tcp        0      0 192.168.1.7:35112       172.217.161.5:443       ESTABLISHED
tcp        0      0 192.168.2.97:45662      148.251.237.53:21       ESTABLISHED
tcp        0      0 192.168.1.7:50892       149.154.164.224:443     ESTABLISHED
tcp        0      0 192.168.1.7:58772       172.217.194.189:443     TIME_WAIT  
tcp        0      1 192.168.1.7:47728       144.76.158.93:443       SYN_SENT   
tcp        0      1 192.168.1.7:47730       144.76.158.93:443       SYN_SENT   
tcp        0      0 192.168.1.7:59548       149.154.171.22:443      ESTABLISHED
tcp        0      0 192.168.1.7:35818       169.55.74.50:443        ESTABLISHED
tcp        0      0 192.168.1.7:50888       149.154.164.224:443     ESTABLISHED
tcp        0      0 192.168.1.7:44156       152.195.11.6:443        ESTABLISHED
tcp        0      0 192.168.1.7:58478       52.229.174.94:443       ESTABLISHED
tcp       52      0 127.0.0.1:60342         127.0.1.1:139           ESTABLISHED
tcp        0      0 192.168.1.7:58820       172.217.161.3:443       ESTABLISHED
tcp        0      0 192.168.1.7:45588       52.175.17.224:443       TIME_WAIT  
tcp        0      0 192.168.1.7:50890       149.154.164.224:443     ESTABLISHED
tcp        0      0 192.168.1.7:58192       52.77.195.144:443       ESTABLISHED
tcp        0      0 192.168.1.7:54224       172.217.31.2:443        ESTABLISHED
tcp        0      0 127.0.1.1:139           127.0.0.1:60342         ESTABLISHED
tcp        0      0 192.168.1.7:34500       192.241.190.153:443     ESTABLISHED
tcp        0      0 192.168.1.7:54888       172.217.161.14:443      ESTABLISHED

Sie können also sehen, dass diesmal nur numerische Werte in der Ausgabe (in der Spalte Foreign Address) erzeugt wurden.

Q3. Wie bringt man netstat dazu, nur TCP-Verbindungen anzuzeigen?

Dies können Sie mit der Befehlszeilenoption -t tun.

netstat -t

In meinem Fall wurde beispielsweise die folgende Ausgabe erzeugt:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.1.7:44236       152.195.11.6:https      ESTABLISHED
tcp        0      0 192.168.1.7:59510       mails11.telegram.:https ESTABLISHED
tcp        0      0 192.168.1.7:57318       13.70.5.200:https       ESTABLISHED
tcp        0      0 192.168.1.7:42706       maa05s05-in-f4.1e:https ESTABLISHED
tcp        0      0 192.168.1.7:51984       del03s10-in-f2.1e:https ESTABLISHED
tcp        0      0 192.168.1.7:51786       52.114.32.7:https       ESTABLISHED
tcp        0      0 192.168.1.7:52294       xx-fbcdn-shv-01-m:https ESTABLISHED
tcp        0      0 192.168.1.7:54372       del03s01-in-f2.1e:https ESTABLISHED
tcp        0      1 192.168.1.7:47788       hyperserver.semse:https SYN_SENT   
tcp        0      0 192.168.1.7:52298       xx-fbcdn-shv-01-m:https ESTABLISHED
tcp        0      0 192.168.1.7:51840       149.154.164.224:https   TIME_WAIT  
tcp        0      0 192.168.1.7:35258       del03s10-in-f1.1e:https ESTABLISHED
tcp        0      0 192.168.2.97:45662      blog.gsmarena.com:ftp   ESTABLISHED
tcp        0      0 192.168.1.7:50728       del03s10-in-f10.1:https TIME_WAIT  
tcp        0      0 192.168.1.7:56468       del03s09-in-f3.1e:https ESTABLISHED
tcp        0      0 192.168.1.7:50892       149.154.164.224:https   ESTABLISHED
tcp        0      0 192.168.1.7:45670       52.175.17.224:https     ESTABLISHED
tcp        0      0 192.168.1.7:55180       del03s10-in-f14.1:https TIME_WAIT  
tcp        0      1 192.168.1.7:47790       hyperserver.semse:https SYN_SENT   
tcp        0      0 192.168.1.7:59548       mails11.telegram.:https ESTABLISHED
tcp        0      0 192.168.1.7:35818       32.4a.37a9.ip4.st:https ESTABLISHED
tcp        0      0 192.168.1.7:50888       149.154.164.224:https   ESTABLISHED
tcp        0      0 192.168.1.7:33360       del03s10-in-f13.1:https TIME_WAIT  
tcp        0      0 192.168.1.7:52332       207.46.140.70:https     ESTABLISHED
tcp        0      0 192.168.1.7:58478       52.229.174.94:https     ESTABLISHED
tcp        0      0 192.168.1.7:54374       del03s01-in-f2.1e:https TIME_WAIT  
tcp        0      0 192.168.1.7:50216       104.24.1.68:https       TIME_WAIT  
tcp       52      0 localhost:60342         ansh:netbios-ssn        ESTABLISHED
tcp        0      0 192.168.1.7:50890       149.154.164.224:https   ESTABLISHED
tcp        0      0 192.168.1.7:55200       del03s10-in-f14.1:https TIME_WAIT  
tcp        0      0 192.168.1.7:54224       del03s01-in-f2.1e:https ESTABLISHED
tcp        0      0 ansh:netbios-ssn        localhost:60342         ESTABLISHED
tcp        0      0 192.168.1.7:55212       del03s10-in-f14.1:https TIME_WAIT  
tcp        0      1 192.168.1.7:47800       hyperserver.semse:https SYN_SENT   
tcp        0      0 192.168.1.7:45668       52.175.17.224:https     ESTABLISHED
tcp        0      0 192.168.1.7:58878       del03s10-in-f3.1e:https TIME_WAIT  
tcp        0      0 192.168.1.7:34500       do-11.lastpass.co:https ESTABLISHED
tcp        0      0 192.168.1.7:41844       a23-35-40-198.dep:https ESTABLISHED

Q4. Wie man Netstat dazu bringt, nur UDP-Verbindungen anzuzeigen

Dies können Sie mit der Befehlszeilenoption -u tun.

netstat -u

F5. Wie bringt man netstat dazu, nur lauschende Sockets anzuzeigen?

Dies ist mit der Befehlszeilenoption -l möglich.

netstat -l

In meinem Fall wurde beispielsweise die folgende Ausgabe erzeugt:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 ansh:domain             *:*                     LISTEN     
tcp        0      0 localhost:ipp           *:*                     LISTEN     
tcp        0      0 *:microsoft-ds          *:*                     LISTEN     
tcp        0      0 *:netbios-ssn           *:*                     LISTEN     
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN     
tcp6       0      0 [::]:microsoft-ds       [::]:*                  LISTEN     
tcp6       0      0 [::]:netbios-ssn        [::]:*                  LISTEN     
...
...
...

Q6. Wie lässt sich Netstat dazu bringen, den Namen und die PID von Programmen anzuzeigen?

Falls Sie möchten, dass netstat Namen von Prozess-IDs von Prozessen anzeigt, zu denen jeder Socket gehört, verwenden Sie die Befehlszeilenoption -p.

netstat -p

Zum Beispiel:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.1.7:57318       13.70.5.200:https       ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:36732       13.76.170.95:https      ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:52050       149.154.164.224:https   ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:55974       del03s10-in-f14.1:https ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:52074       149.154.164.224:https   ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:60590       mails11.telegram.:https ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:52254       149.154.164.224:https   ESTABLISHED 3247/firefox    
tcp        0      0 192.168.2.97:45662      blog.gsmarena.com:ftp   ESTABLISHED 3160/filezilla  
tcp        0      0 192.168.1.7:35818       32.4a.37a9.ip4.st:https ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:52528       52.114.32.7:https       ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:55950       del03s10-in-f14.1:https ESTABLISHED 3247/firefox    
tcp       68      0 localhost:60342         ansh:netbios-ssn        ESTABLISHED 2625/gvfsd-smb-brow
tcp        0      0 192.168.1.7:44974       152.195.11.6:https      ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:36730       13.76.170.95:https      ESTABLISHED 3247/firefox    
tcp        0      0 ansh:netbios-ssn        localhost:60342         ESTABLISHED -               
tcp        0      0 192.168.1.7:52732       del03s10-in-f2.1e:https ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:60314       52.229.174.94:https     ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:53072       207.46.140.70:https     ESTABLISHED 3247/firefox    
tcp        0      0 192.168.1.7:34500       do-11.lastpass.co:https ESTABLISHED 3247/firefox

Siehe die letzte Spalte für Informationen zu PID/Programmnamen.

Q7. Wie kann netstat Netzwerkstatistiken drucken?

Dies kann mit der Befehlszeilenoption -s erfolgen.

netstat -s

In diesem Fall erzeugt das Tool Informationen wie die Gesamtzahl der empfangenen, verworfenen, zugestellten Pakete und vieles mehr. Es folgt eine Beispielausgabe, wenn die Befehlszeilenoption -s verwendet wurde.

Ip:
    592215 total packets received
    41 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    580699 incoming packets delivered
    539118 requests sent out
    627 outgoing packets dropped
    482 dropped because of missing route
    2 fragments failed
Icmp:
    2337 ICMP messages received
    6 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 2337
    3917 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 3917
IcmpMsg:
        InType3: 2337
        OutType3: 3917
Tcp:
    10943 active connections openings
    127 passive connection openings
    38 failed connection attempts
    467 connection resets received
    34 connections established
    443643 segments received
    414589 segments send out
    6929 segments retransmited
    428 bad segments received.
    11713 resets sent

...
...
...

Q8. Wie lässt sich netstat dazu bringen, die Kernel-Routing-Tabelle anzuzeigen?

Dies kann mit der Befehlszeilenoption -r erfolgen.

netstat -r

Das Folgende ist die in meinem Fall erzeugte Ausgabe:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.1.1     0.0.0.0         UG        0 0          0 wlx18a6f713679b
link-local      *               255.255.0.0     U         0 0          0 wlx18a6f713679b
192.168.1.0     *               255.255.255.0   U         0 0          0 wlx18a6f713679b

Beachten Sie, dass netstat -r und route -e dieselbe Ausgabe erzeugen.

Schlussfolgerung

Wie Sie inzwischen verstanden haben, ist der Befehl netstat ein sehr wichtiges Werkzeug. Es bietet viele Funktionen – wir haben hier nur an der Oberfläche gekratzt. Wenn Sie alles, was wir in diesem Tutorial besprochen haben, geübt haben, besuchen Sie die Manpage des Tools, um weitere Informationen zu erhalten.


Linux
  1. Linux-CD-Befehls-Tutorial für Anfänger (8 Beispiele)

  2. Linux-Kommando-Tutorial für Anfänger (5 Beispiele)

  3. Linux-df-Befehls-Tutorial für Anfänger (8 Beispiele)

  4. Linux Date Command Tutorial für Anfänger (8 Beispiele)

  5. Linux-Hostname-Befehls-Tutorial für Anfänger (5 Beispiele)

Linux Who Command Tutorial für Anfänger (8 Beispiele)

Linux nl Command Tutorial für Anfänger (7 Beispiele)

Linux-nm-Befehls-Tutorial für Anfänger (10 Beispiele)

Linux OD Command Tutorial für Anfänger (6 Beispiele)

Linux w Command Tutorial für Anfänger (5 Beispiele)

Linux ss Command Tutorial für Anfänger (8 Beispiele)