LoginSignup
1

More than 3 years have passed since last update.

ネットワーク系のLinuxコマンド

Last updated at Posted at 2019-12-09

ネットワーク系のコマンドに関して無知だったので使ったものに関して調べた。

1.ping

ネットワークに繋がっているかどうか調べたり、特定のホストが起動していうか調べるときに使う。
相手に「返事して」というメッセージを送り、相手が生きていれば「生きてるよ」と返信してくれる。

$ ping google.com
PING google.com (216.58.197.174): 56 data bytes
64 bytes from 216.58.197.174: icmp_seq=0 ttl=54 time=18.673 ms
64 bytes from 216.58.197.174: icmp_seq=1 ttl=54 time=18.657 ms
64 bytes from 216.58.197.174: icmp_seq=2 ttl=54 time=19.664 ms
64 bytes from 216.58.197.174: icmp_seq=3 ttl=54 time=17.743 ms

Ctrl + c

--- google.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 17.743/18.684/19.664/0.679 ms

上記の実行結果の解説

4 packets transmitted <= 送信パケット数
4 packets received <= 受信パケット数
0.0% packet loss <= パケット損失率
round-trip min/avg/max/stddev = 17.743/18.684/19.664/0.679 ms <= 送信にかかった最短時間, 最長時間, 平均時間, 平均偏差

2.traceroute

ネットワークの経路を表示する。ネットワークのルーティングの設定が正しいか確認する場合などに利用する。

$ traceroute dns.google
traceroute to dns.google (8.8.4.4), 64 hops max, 52 byte packets
 1  buffalo.setup (192.168.11.1)  1.169 ms  0.911 ms  0.728 ms <= 家のルーターっぽい
 2  192.168.10.231 (192.168.10.231)  1.321 ms  1.918 ms  1.285 ms
 3  fkc-mx480-bbbas05.qtnet.ad.jp (218.40.227.148)  2.624 ms  3.535 ms  2.993 ms <= QTネットっぽい
 4  * 211.9.102.37 (211.9.102.37)  3.830 ms
    211.9.102.33 (211.9.102.33)  3.186 ms
 5  61.203.192.241 (61.203.192.241)  2.853 ms  3.376 ms
    61.203.192.249 (61.203.192.249)  3.569 ms
 6  61.203.193.122 (61.203.193.122)  19.343 ms
    61.203.193.126 (61.203.193.126)  18.152 ms
    61.203.193.122 (61.203.193.122)  18.438 ms
 7  61.203.192.177 (61.203.192.177)  102.486 ms  57.606 ms  19.654 ms
 8  108.170.242.129 (108.170.242.129)  19.595 ms  20.212 ms
    108.170.242.193 (108.170.242.193)  56.511 ms
 9  66.249.95.155 (66.249.95.155)  24.191 ms
    108.170.233.77 (108.170.233.77)  19.702 ms
    72.14.233.221 (72.14.233.221)  19.364 ms
10  dns.google (8.8.4.4)  19.461 ms  19.845 ms  18.701 ms <= 無事に到着している

3.host

ドメイン名からIPアドレスを調べたり、逆にIPアドレスからドメイン名を調べることができる。

ドメインからIPアドレスを聞いてみる。

$ host dns.google
dns.google has address 8.8.4.4
dns.google has address 8.8.8.8
dns.google has IPv6 address 2001:4860:4860::8844
dns.google has IPv6 address 2001:4860:4860::8888

IPアドレスからドメインを聞いてみる。

$ host 8.8.8.8
8.8.8.8.in-addr.arpa domain name pointer dns.google.

4.dig

DNSサーバーからホストに関する情報を取得する。

dig goole.com
; <<>> DiG 9.10.6 <<>> goole.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25027   <= NOERRORなので正常な応答が返って来ていることがわかる
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:                         <= 問い合わせ内容
;goole.com.         IN  A

;; ANSWER SECTION:                           <=問い合わせに対する回答
goole.com.      1403    IN  A   217.160.0.201  <= 問い合わせたホストのIPアドレス

;; Query time: 61 msec <= 問い合わせにかかった時間
;; SERVER: 192.168.11.1#53(192.168.11.1)      <= 問い合わせを行ったサーバーのIPアドレス
;; WHEN: Tue Dec 10 00:53:07 JST 2019
;; MSG SIZE  rcvd: 54

利用するDNSサーバーをしていることもできる。

dig @dns.google google.com
 <<>> DiG 9.10.6 <<>> @dns.google google.com
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58341
;; 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.     299 IN  A   216.58.197.174

;; Query time: 86 msec
;; SERVER: 8.8.4.4#53(8.8.4.4)   <= 利用するDNSサーバーが変わった
;; WHEN: Tue Dec 10 01:23:50 JST 2019
;; MSG SIZE  rcvd: 55

5.ifconfig

イーサネットなどのネットワークインターフェイスに割り振られているIPアドレスを確認したり、IPアドレスを割り当てたりするコマンド

$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384  <= Loop back ローカルホスト 127.0.0.1
    options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
    inet 127.0.0.1 netmask 0xff000000
    inet6 ::1 prefixlen 128
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
    nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280 <= IPv6とIPv4トンネリングを行う時のインターフェイス
stf0: flags=0<> mtu 1280                <= IPv6パケットをIPv4ネットワークにルーティングするためのインターフェイス
XHC20: flags=0<> mtu 0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 <= Ethernet0 
    ether 4c:32:75:97:**:**                                                <= Macアドレス
    inet6 fe80::410:c692:d02f:d432%en0 prefixlen 64 secured scopeid 0x5
    inet 192.168.11.3 netmask 0xffffff00 broadcast 192.168.11.255  <= IPアドレス
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
    ether 0e:32:75:97:ab:2f
    media: autoselect
    status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
    ether fa:49:90:3f:a8:bb
    inet6 fe80::f849:90ff:fe3f:a8bb%awdl0 prefixlen 64 scopeid 0x7
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    ether 6a:00:02:ea:6b:10
    media: autoselect <full-duplex>
    status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    ether 6a:00:02:ea:6b:11
    media: autoselect <full-duplex>
    status: inactive
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 <= 仮想インターフェイスを外部ネットワークに接続する??
    options=63<RXCSUM,TXCSUM,TSO4,TSO6>
    ether 6a:00:02:ea:6b:10
    Configuration:
        id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
        maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
        root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
        ipfilter disabled flags 0x2
    member: en1 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 8 priority 0 path cost 0
    member: en2 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 9 priority 0 path cost 0
    nd6 options=201<PERFORMNUD,DAD>
    media: <unknown type>
    status: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
    inet6 fe80::a83c:cb87:4ddf:f50b%utun0 prefixlen 64 scopeid 0xc
    nd6 options=201<PERFORMNUD,DAD>
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
    inet6 fe80::55d3:d20b:a62c:e19c%utun1 prefixlen 64 scopeid 0xd
    nd6 options=201<PERFORMNUD,DAD>

6.telnet

ネットワークサービスが動いているか確認したいときに利用する。

↓自前でwebサーバーを立てた。

$ telnet 127.0.0.1 8887
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /

HTTP/1.1 200 OK <= レスポンス
content-length: 7
accept-ranges: bytes
connection: keep-alive
content-type: text/html; charset=utf-8

HELLO <= htmlファイルの中身

Connection closed by foreign host.

まとめ

webサーバー生きてるの?とかどこで止まってんの?とかポート空いてんの?とかなったときにすごい困ったので、これからも色々調べてみる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1