Hack 98. Netstat コマンド

netstat コマンドはネットワークに関する情報を表示します。
たとえば、ネットワーク接続数・ルーティングテーブル・インターフェイス情報などです。
次に netstat コマンドの使用方法の例をいくつか示します。

netstat を使用してアクティブ接続とドメインソケットを表示します。

# netstat -an

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address  Foreign Address  State
tcp        0      0 0.0.0.0:5666   0.0.0.0:*        LISTEN
tcp        0      0 0.0.0.0:111    0.0.0.0:*        LISTEN
tcp        0      0 0.0.0.0:4086   0.0.0.0:*        LISTEN
skipped..

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags    Type    State      I-Node  Path
unix  2      [ ACC ]  STREAM  LISTENING   7894   /tmp/.font-unix/fs7100
unix  2      [ ACC ]  STREAM  LISTENING   9662   /tmp/.gdm_socket
unix  2      [ ACC ]  STREAM  LISTENING  10897   @/tmp/fam-root-

プロセス ID とプログラム名も表示する

どのプログラムが指定したネットワーク接続に関連しているかを調べるのに役立ちます。

# netstat -tap 

Active Internet connections (servers and established)
Proto Recv-Q Send-Q  Local Address                Foreign Address             State       PID/Program name
tcp        0      0  *:nrpe                       *:*                         LISTEN      16277/xinetd
tcp        0      0  localhost.localdomain:smtp   *:*                         LISTEN       7263/sendmail: acce
tcp       34      0  localhost.localdomain:54221  localhost.localdomain:4089  CLOSE_WAIT  29881/httpd
tcp        0   3216  dev-db:ssh                   cpe-76-94-215-154.soca:4682 ESTABLISHED 11717/sshd: ramesh

ルーティングテーブルを表示する

# netstat --route
Kernel IP routing table
Destination  Gateway      Genmask        Flags MSS Window irtt Iface
192.168.1.0  *            255.255.255.0  U       0 0         0 eth0
162.244.0.0  *            255.255.0.0    U       0 0         0 eth0
default      192.168.1.1  0.0.0.0        UG      0 0         0 eth0

RAW ネットワーク統計を表示する

# netstat --statistics --raw 

Ip:
    11080343 total packets received
    0 forwarded
    1 with unknown protocol
    0 incoming packets discarded
    11037744 incoming packets delivered
    11199763 requests sent out

Icmp:
    577135 ICMP messages received
    64 input ICMP message failed.
    ICMP input histogram:
    destination unreachable: 537
    timeout in transit: 65
    source quenches: 2
    echo requests: 576476
    echo replies: 12
    timestamp request: 3
    address mask request: 3
    581558 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
    destination unreachable: 5079
    echo replies: 576476
    timestamp replies: 3

その他の netstat コマンド
o # netstat --tcp --numeric マシンへそしてマシンからの TCP コネクションのリスト
o # netstat --tcp --listening --programs TCP ポートを表示します。サーバはそのポートをリッスンしていて、
その他にもプログラムは指定したポートをリッスンしています。
o # netstat -rnC ルーティングキャッシュを表示します。