コマンドリファレンス - ネットワークの状態を表示 netstat

 クラウディア
1. netstat ネットワークの状態を表示
2.  ネットワークの一覧を表示

1. netstat ネットワークの状態を表示

 ネットワークの状態(オープン中のサーバポートや接続中のアドレス・ポート番号)を表示します。  このコマンドは非常に使いこなすのが難しくて、はっきり言えばわたしも使いこなしていないのです。  単に

netstat
 と入力すると、以下のように表示されます。

> netstat
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
tcp4       0      0  ns.local.ssh           pc01.local.1146        ESTABLISHED
tcp4       0      0  localhost.953          *.*                    LISTEN
tcp6       0      0  localhost.953          *.*                    LISTEN
tcp4       0      0  localhost.domain       *.*                    LISTEN

	・・・

udp4       0      0  ns.local.ntp           *.*
Active UNIX domain sockets
Address  Type   Recv-Q Send-Q    Inode     Conn     Refs  Nextref Addr
c514bdc0 stream      0      0        0 c514be00        0        0

	・・・

c514afc0 dgram       0      0 c5148680        0 c514be80        0 /var/run/log
 詳しい説明は省略しますが、上記で簡単に説明すると、pc01.local というクライアントが、ssh でログインして作業中であるということがわかります。  ポート番号 953 を使うサーバプログラムが、IPv4 と IPv6 で接続待ち状態であることがわかります。  DNS サーバや NTP サーバが動作中であるということがわかります。

2. ネットワークの一覧を表示


netstat [-AaLnSW] [-f protocol_family | -p protocol] [-M core] [-N system]
 IPv4 の状態を見るには、-f オプションを使用します。  -f はアドレスファミリーを指定します。  -p でプロトコルと組み合わせることもできます。
ファミリ 意味   設定可能なプロトコル  
inet IPv4 bdg, divert, icmp, igmp, ip, ipsec, pim, tcp, udp
inet6IPv6 bdg, icmp6, ip6, ipsec6, rip6, tcp, udp

 例)


> netstat -f inet
Active Internet connections
Proto Recv-Q Send-Q Local Address          Foreign Address        (state)
tcp4       0      0 vm.ssh                 172.20.10.2.52089      ESTABLISHED
udp4       0      0 vm.domain              *.*
udp4       0      0 localhost.domain       *.*
udp4       0      0 localhost.ntp          *.*
udp4       0      0 vm.ntp                 *.*
> netstat -f inet -p tcp
Active Internet connections
Proto Recv-Q Send-Q Local Address          Foreign Address        (state)
tcp4       0      0 vm.ssh                 172.20.10.2.52089      ESTABLISHED