Fire Engine

消防士→ITエンジニア→研究者

インフラエンジニアになって覚えたLinuxコマンド その2(ネットワーク系コマンド編)

こんにちは、つるべーです。
今回の記事は以前の続きで、インフラ業務の中で覚えたLinuxコマンドについてです。

blog.tsurubee.tech

今回はネットワーク系のコマンドに絞ってまとめていきます。

環境

目次

ping

ICMPを使ってネットワーク上のホストの接続を確認する

$ ping www.example.com
PING www.example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=1 ttl=63 time=123 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=2 ttl=63 time=123 ms
64 bytes from 93.184.216.34 (93.184.216.34): icmp_seq=3 ttl=63 time=123 ms
^C
--- www.example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 123.096/123.361/123.551/0.345 ms

dig

DNSサーバに問い合わせてドメイン名(またはIPアドレス)を調べる。

dig [@DNSサーバ] ドメイン [検索タイプ]

DNSサーバは指定しない場合は、/etc/resolv.confに記述されたDNSサーバを使用する。
検索タイプはデフォルトではAレコード
-x AAA.BBB.CCC.DDDで逆引き問い合わせもできる。

$ dig google.com

; <<>> DiG 9.10.6 <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50236
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.com.            IN  A

;; ANSWER SECTION:
google.com.     279 IN  A   216.58.197.174

;; Query time: 28 msec
;; SERVER: 192.168.11.1#53(192.168.11.1)
;; WHEN: Fri May 04 23:08:38 JST 2018
;; MSG SIZE  rcvd: 55

+shortオプションをつけると目的の情報だけを表示する

$ dig +short -x 216.58.197.174
nrt12s02-in-f174.1e100.net.
nrt12s02-in-f14.1e100.net.

whois

whoisを利用してドメイン情報を取得する

$ whois gihyo.jp
% IANA WHOIS server
% for more information on IANA, visit http://www.iana.org
% This query returned 1 object

refer:        whois.jprs.jp

domain:       JP

organisation: Japan Registry Services Co., Ltd.
address:      Chiyoda First Bldg. East 13F,
address:      3-8-1 Nishi-Kanda
address:      Chiyoda-ku Tokyo 101-0065
address:      Japan

(省略)

tcpdump

ネットワークインターフェイスを通過するパケットを観察する。
tcpdumpと打つだけでもパケットが流れる様子が観察できるが、大量に流れてくるため通常は以下のように条件を絞って観察する。

tcpdump -i [interface name] port [port number]

実際の実行例は下のような感じ。

$ tcpdump -i eth1 port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 262144 bytes
02:09:24.936395 IP 192.168.10.1.50796 > nginx.http: Flags [F.], seq 2273804563, ack 848006729, win 4117, options [nop,nop,TS val 715484926 ecr 28938], length 0
02:09:24.936422 IP 192.168.10.1.50797 > nginx.http: Flags [F.], seq 1504651190, ack 642584441, win 4117, options [nop,nop,TS val 715484926 ecr 28938], length 0
02:09:24.936425 IP 192.168.10.1.50798 > nginx.http: Flags [F.], seq 2028803151, ack 3632381970, win 4117, options [nop,nop,TS val 715484926 ecr 28938], length 0
02:09:24.936428 IP 192.168.10.1.50799 > nginx.http: Flags [SEW], seq 3820785142, win 65535, options [mss 1460,nop,wscale 5,nop,nop,TS val 715484926 ecr 0,sackOK,eol], length 0
02:09:24.936447 IP nginx.http > 192.168.10.1.50799: Flags [S.E], seq 2665692678, ack 3820785143, win 28960, options [mss 1460,sackOK,TS val 43425 ecr 715484926,nop,wscale 6], length 0

(省略)

これを見るだけでもパケット届いてるなーとかまではわかるが、出力結果をちゃんと見るには下の記事がすごく参考になった。

qiita.com

-w ファイル名でファイル出力もでき、出力したファイルをWiresharkで見ると良さそう。

ip

ipコマンドは様々なネットワーク関連の設定を確認/変更できるコマンドである。
CentOS7では、ifconfig、route、netstatなどといったコマンドが入っているnet-toolsパッケージが廃止予定であり、iproute2パッケージに含まれる「ip」「ss」などのコマンドを使用することが推奨されているようです。これについては以下の記事に非常にわかりやすくまとめられています。

enakai00.hatenablog.com

ネットワークデバイスの設定を確認

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:5f:94:78 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
       valid_lft 83407sec preferred_lft 83407sec
    inet6 fe80::5054:ff:fe5f:9478/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:2e:38:d9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.11/24 brd 192.168.10.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe2e:38d9/64 scope link
       valid_lft forever preferred_lft forever

aaddrの略。ifconfigコマンドに相当するコマンド。

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::5054:ff:fe5f:9478  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:5f:94:78  txqueuelen 1000  (Ethernet)
        RX packets 32746  bytes 17741551 (16.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22008  bytes 2394181 (2.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.11  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::a00:27ff:fe2e:38d9  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:2e:38:d9  txqueuelen 1000  (Ethernet)
        RX packets 161  bytes 26250 (25.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 3146 (3.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ルーティングの確認

$ ip r
default via 10.0.2.2 dev eth0 proto static metric 100
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100
192.168.10.0/24 dev eth1 proto kernel scope link src 192.168.10.11 metric 100

rrouteの略。routeコマンドに相当するコマンド。

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.10.0    0.0.0.0         255.255.255.0   U     100    0        0 eth1

-nオプション:ホストの名前解決をせずにIPアドレスを表示

上で表示したネットワークデバイスやルーティングの設定は、adddelなどを使って設定情報を追加、削除できる。

ss

ソケットの状態を表示する。

$ ss -tan
State      Recv-Q Send-Q                                            Local Address:Port                                                           Peer Address:Port
LISTEN     0      128                                                           *:111                                                                       *:*
LISTEN     0      128                                                           *:80                                                                        *:*
LISTEN     0      128                                                           *:22                                                                        *:*
LISTEN     0      100                                                   127.0.0.1:25                                                                        *:*
ESTAB      0      0                                                     10.0.2.15:22                                                                 10.0.2.2:50759
LISTEN     0      128                                                          :::111                                                                      :::*
LISTEN     0      128                                                          :::22                                                                       :::*
LISTEN     0      100                                                         ::1:25                                                                       :::*

-tオプション:TCPソケットを表示
-aオプション:接続待ち(LISTEN)も含めて表示
-nオプション:ポート番号を表示(デフォルトではサービス名)
ssコマンドはnetstatコマンドに相当するコマンド。

$ netstat -tan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 10.0.2.15:22            10.0.2.2:50759          ESTABLISHED
tcp6       0      0 :::111                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN

参考

[改訂第3版]Linuxコマンドポケットリファレンス

[改訂第3版]Linuxコマンドポケットリファレンス