YAMAGUCHI::weblog

海水パンツとゴーグルで、巨万の富を築きました。カリブの怪物、フリーアルバイター瞳です。

Mac OS XでDNSキャッシュを消す

はじめに

家の中でDNS立ててからさらにいろいろとネットワーク環境をいじっていたのですが、その際に各マシンへのIP Addressの割り当て変更してたらDNSキャッシュが残ってたせいでホスト名でアクセスできなくなってて、DNSキャッシュ消そうと思ったけどできなくて困った。

状況

foo.example.comのIP Addressを192.168.12.100から192.168.1.3へ変更して、DNSを再起動したけどホスト名で上手くアクセスできない状況。

方法

dscacheutilを使う

ググってたらこれらのサイトに行き着いた。

やってみる前はこんな感じ。hostではちゃんとDNSから引っ張ってこれてるのに、pingやsshでは違うIP Addressを参照してて接続できない。

$ host foo
foo.example.com has address 192.168.1.3
$ ping foo
PING foo.example.com (192.168.12.100): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
--- foo.example.com ping statistics ---
5 packets transmitted, 0 packets received, 100.0% packet loss

そこでdscacheutilを使ってみた。

$ dscacheutil -flushcache
$ ping foo.example.com
PING foo.example.com (192.168.1.3): 56 data bytes
64 bytes from 192.168.1.3: icmp_seq=0 ttl=64 time=0.173 ms
64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.413 ms
^C
--- foo.example.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.173/0.293/0.413/0.120 ms

おお!ちゃんと見に行ってますよ。じゃあこのdscacheutilっていうのはなんだ?manしてみた。

NAME
     dscacheutil -- gather information, statistics and initiate queries to the Directory Service cache.

SYNOPSIS
     dscacheutil -h
     dscacheutil -q category [-a key value]
     dscacheutil -cachedump [-buckets] [-entries [category]]
     dscacheutil -configuration
     dscacheutil -flushcache
     dscacheutil -statistics

DESCRIPTION
     dscacheutil does various operations against the Directory Service cache including gathering statistics,
     initiating lookups, inspection, cache flush, etc.  This tool replaces most of the functionality of the
     lookupd tool previously available in the OS.

Mac OS X 10.4以前ではlookupdだったみたいですね。説明書きにあるようにdscacheutilを使うとキャッシュの統計情報とかも取得できます。どれも参照系ばかりのようですね。

$ dscacheutil -statistics
Overall Statistics:
    Average Call Time     - 0.000444
    Cache Hits            - 7745
    Cache Misses          - 15579
    Total External Calls  - 9550

Statistics by procedure:

             Procedure   Cache Hits   Cache Misses   External Calls
    ------------------   ----------   ------------   --------------
              getpwnam          235             27              262
              getpwuid         1308             33             1341
              getgrnam           67             32               99
              getgrgid            2              1                3
         getservbyname         6131             79               17
              getfsent            0              0               13
          getnetbyaddr            1              2                3
         gethostbyname            1           7555               30
         gethostbyaddr            0            255              255
    gethostbyname_service            0              0             7526
           _flushcache            0              0                1

辿りつくまでの作業ログ

hostsファイルを確認してみる

/private/etc/hostsに書いてたかな?と思って一応確認してみた。やっぱりない。

$ cat /private/etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost

255.255.255.255	broadcasthost
::1             localhost 
fe80::1%lo0	localhost
DNSキャッシュをflushする

OSで持ってるDNSキャッシュをクリアすれば良くない?と思ったのでした。

$ ifconfig flushall
ifconfig: interface flushall does not exist
arpを見てみる

次にarp見てみましょうということで見てみたけど、別にどうにもなってない。しかもhostコマンドではちゃんと引っ張ってこれている。

$ arp -d 192.168.12.100
delete: cannot locate 192.168.12.100

$ arp -d enzo.ymotonpgoo.dip.jp
arp: enzo.ymotonpgoo.dip.jp: Unknown host

$ arp -a
? (192.168.1.1) at 0:1b:8b:65:35:f0 on en0 ifscope [ethernet]
? (192.168.1.1) at 0:1b:8b:65:35:f0 on en1 ifscope [ethernet]
bar.example.com.1.168.192.in-addr.arpa (192.168.1.2) at 0:16:d3:20:97:3a on en0 ifscope [ethernet]
foo.example.com.1.168.192.in-addr.arpa (192.168.1.3) at 40:61:86:cb:e5:2b on en0 ifscope [ethernet]
? (192.168.1.5) at 0:24:a5:ae:1e:5 on en0 ifscope [ethernet]
? (192.168.1.5) at 0:24:a5:ae:1e:5 on en1 ifscope [ethernet]
? (192.168.1.255) at (incomplete) on en0 ifscope [ethernet]

$ host foo
foo.example.com has address 192.168.1.3

この状態でpingを打ってみると...

$ ping foo.example.com
PING foo.example.com (192.168.12.100): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
--- foo.example.com ping statistics ---
5 packets transmitted, 0 packets received, 100.0% packet loss

おかしいなあ。