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

UNIX / Linux:10 Netstat-Befehlsbeispiele

Der Befehl Netstat zeigt verschiedene netzwerkbezogene Informationen wie Netzwerkverbindungen, Routing-Tabellen, Schnittstellenstatistiken, Masquerade-Verbindungen, Multicast-Mitgliedschaften usw. an,

Lassen Sie uns in diesem Artikel 10 praktische Unix-netstat-Befehle besprechen Beispiele.

1. Alle Ports auflisten (sowohl lauschende als auch nicht lauschende Ports)

Alle Ports mit netstat -a auflisten

# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
udp        0      0 *:bootpc                *:*                                

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     6135     /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     5140     /var/run/acpid.socket

Alle TCP-Ports mit netstat -at auflisten

# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
tcp        0      0 localhost:ipp           *:*                     LISTEN
tcp        0      0 *:smtp                  *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN

Alle UDP-Ports mit netstat -au auflisten

# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 *:bootpc                *:*
udp        0      0 *:49119                 *:*
udp        0      0 *:mdns                  *:*

2. Listen Sie Sockets auf, die sich im Listening-Zustand befinden

Nur lauschende Ports mit netstat -l auflisten

# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:ipp           *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN
udp        0      0 *:49119                 *:*

Nur lauschende TCP-Ports mit netstat -lt auflisten

# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
tcp        0      0 *:smtp                  *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN

Nur lauschende UDP-Ports mit netstat -lu auflisten

# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 *:49119                 *:*
udp        0      0 *:mdns                  *:*

Listen Sie nur die lauschenden UNIX-Ports mit netstat -lx auf

# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     6294     private/maildrop
unix  2      [ ACC ]     STREAM     LISTENING     6203     public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     6302     private/ifmail
unix  2      [ ACC ]     STREAM     LISTENING     6306     private/bsmtp

3. Statistiken für jedes Protokoll anzeigen

Statistiken für alle Ports mit netstat -s anzeigen

# netstat -s
Ip:
    11150 total packets received
    1 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    11149 incoming packets delivered
    11635 requests sent out
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
Tcp:
    582 active connections openings
    2 failed connection attempts
    25 connection resets received
Udp:
    1183 packets received
    4 packets to unknown port received.
.....

Statistiken für TCP- (oder) UDP-Ports mit netstat -st (oder) -su anzeigen

# netstat -st

# netstat -su

4. Zeigen Sie PID- und Programmnamen in der netstat-Ausgabe mit netstat -p

an

Die Option netstat -p kann mit jeder anderen netstat-Option kombiniert werden. Dadurch wird der netstat-Ausgabe der „PID/Programmname“ hinzugefügt. Dies ist beim Debuggen sehr nützlich, um festzustellen, welches Programm auf einem bestimmten Port läuft.

# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        1      0 ramesh-laptop.loc:47212 192.168.185.75:www        CLOSE_WAIT  2109/firefox
tcp        0      0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox

5. Host, Port und Benutzername in der netstat-Ausgabe nicht auflösen

Wenn Sie nicht möchten, dass der Name des Hosts, Ports oder Benutzers angezeigt wird, verwenden Sie die Option netstat -n. Dies wird in Zahlen angezeigt, anstatt den Hostnamen, den Portnamen und den Benutzernamen aufzulösen.

Dies beschleunigt auch die Ausgabe, da netstat keine Suche durchführt.

# netstat -an

Wenn Sie nicht möchten, dass nur eines dieser drei Elemente ( Ports oder Hosts oder Benutzer ) aufgelöst wird, verwenden Sie die folgenden Befehle.

# netsat -a --numeric-ports

# netsat -a --numeric-hosts

# netsat -a --numeric-users

6. Netstat-Informationen kontinuierlich drucken

netstat druckt kontinuierlich alle paar Sekunden Informationen.

# netstat -c
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED
tcp        1      1 ramesh-laptop.loc:52564 101.11.169.230:www      CLOSING
tcp        0      0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED
tcp        1      1 ramesh-laptop.loc:42367 101.101.34.101:www      CLOSING
^C

7. Finden Sie die nicht unterstützenden Adressfamilien in Ihrem System

netstat --verbose

Am Ende wirst du so etwas haben.

	netstat: no support for `AF IPX' on this system.
	netstat: no support for `AF AX25' on this system.
	netstat: no support for `AF X25' on this system.
	netstat: no support for `AF NETROM' on this system.

8. Zeigen Sie die Kernel-Routing-Informationen mit netstat -r

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

Hinweis: Verwenden Sie netstat -rn, um Routen im numerischen Format anzuzeigen, ohne nach Hostnamen aufzulösen.

9. Finden Sie heraus, auf welchem ​​Port ein Programm läuft

# netstat -ap | grep ssh
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        1      0 dev-db:ssh           101.174.100.22:39213        CLOSE_WAIT  -
tcp        1      0 dev-db:ssh           101.174.100.22:57643        CLOSE_WAIT  -

Finden Sie heraus, welcher Prozess einen bestimmten Port verwendet:

# netstat -an | grep ':80'

10. Liste der Netzwerkschnittstellen anzeigen

# netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0         0      0      0 0             0      0      0      0 BMU
eth2       1500 0     26196      0      0 0         26883      6      0      0 BMRU
lo        16436 0         4      0      0 0             4      0      0      0 LRU

Zeigen Sie erweiterte Informationen zu den Schnittstellen (ähnlich ifconfig) mit netstat -ie:

an
# netstat -ie
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 00:10:40:11:11:11
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Memory:f6ae0000-f6b00000

Linux
  1. 10 grundlegende Beispiele für den Linux-Netstat-Befehl

  2. Linux-netstat-Befehl

  3. 10 Xargs-Befehlsbeispiele in Linux / UNIX

  4. rm-Befehlsbeispiele unter Linux

  5. ps-Befehlsbeispiele in Linux

wc Linux-Befehl mit Beispielen

Netstat-Befehl in Linux - 28 Befehle mit Beispielen

10 Beispiele für iftop-Befehle unter Linux

id-Befehlsbeispiele in Linux

df-Befehlsbeispiele in Linux

du-Befehlsbeispiele in Linux