GNU/Linux >> Linux の 問題 >  >> Linux

UNIX / Linux:10 の Netstat コマンドの例

Netstat コマンドは、ネットワーク接続、ルーティング テーブル、インターフェース統計、マスカレード接続、マルチキャスト メンバーシップなど、さまざまなネットワーク関連情報を表示します。

この記事では、10 の実用的な UNIX netstat コマンドを確認します。 例。

1.すべてのポートの一覧表示 (リスニング ポートと非リスニング ポートの両方)

netstat -a を使用してすべてのポートを一覧表示

# 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

netstat -at を使用してすべての TCP ポートを一覧表示

# 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

netstat -au を使用してすべての udp ポートを一覧表示します

# 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.リスニング状態にあるソケットを一覧表示

netstat -l を使用してリスニング ポートのみを一覧表示する

# 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                 *:*

netstat -lt を使用してリッスンしている TCP ポートのみを一覧表示

# 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

netstat -lu を使用してリッスンしている UDP ポートのみを一覧表示

# 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                  *:*

netstat -lx を使用してリッスンしている UNIX ポートのみを一覧表示する

# 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.各プロトコルの統計を表示

netstat -s を使用してすべてのポートの統計を表示

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

netstat -st (または) -su を使用して、TCP (または) UDP ポートの統計を表示します

# netstat -st

# netstat -su

4. netstat -p を使用して netstat 出力に PID とプログラム名を表示する

netstat -p オプションは、他の netstat オプションと組み合わせることができます。これにより、「PID/プログラム名」が netstat 出力に追加されます。これは、デバッグ中に特定のポートで実行されているプログラムを特定するのに非常に役立ちます。

# 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. netstat 出力でホスト、ポート、およびユーザー名を解決しない

ホスト名、ポート名、ユーザー名を表示したくない場合は、netstat -n オプションを使用してください。これは、ホスト名、ポート名、ユーザー名を解決する代わりに、数字で表示されます。

netstat はルックアップを実行しないため、これにより出力も高速化されます。

# netstat -an

これら 3 つの項目 (ポート、ホスト、またはユーザー) のいずれか 1 つだけを解決したくない場合は、次のコマンドを使用します。

# netsat -a --numeric-ports

# netsat -a --numeric-hosts

# netsat -a --numeric-users

6. netstat 情報を継続的に出力

netstat は、数秒ごとに継続的に情報を出力します。

# 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.システム内のサポートされていないアドレス ファミリーを見つける

netstat --verbose

最終的には、このようなものになります。

	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. netstat -r を使用してカーネル ルーティング情報を表示します

# 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

注: ホスト名を解決せずにルートを数値形式で表示するには、netstat -rn を使用します。

9.プログラムが実行されているポートを調べる

# 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  -

特定のポートを使用しているプロセスを見つけます:

# netstat -an | grep ':80'

10.ネットワーク インターフェースのリストを表示

# 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

netstat -ie:を使用して、インターフェースに関する拡張情報を表示します (ifconfig と同様)。

# 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. 5 UNIX / Linux の traceroute コマンドの例

  2. 10 Linux / UNIX での Xargs コマンドの例

  3. Linux での rm コマンドの例

  1. Linux での ps コマンドの例

  2. Linux での netstat コマンドの例

  3. w Linux でのコマンド例

  1. LinuxNetstatコマンドの10の基本的な例

  2. Linuxnetstatコマンド

  3. UNIX/Linux での tail コマンドの例