tcpdump などで DNS の問い合わせと応答をキャプチャして名前解決の通信について整理しています。

外部サーバーに依頼

  • AWS EC2 Ubuntu 20.04
  • IP:10.3.0.183
sudo tcpdump -tnl -i eth0 udp port 53

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes

Google が管理しているオープンフルサービスリゾルバ 8.8.8.8 に依頼します。

dig +short @8.8.8.8 example.org A

93.184.216.34

@8.8.8.8 に名前解決を依頼します。

sudo tcpdump -tnl -i eth0 udp port 53

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes

IP 10.3.0.183.53118 > 8.8.8.8.53: 45372+ [1au] A? example.org. (52) // --- (1)
IP 8.8.8.8.53 > 10.3.0.183.53118: 45372$ 1/0/1 A 93.184.216.34 (56) // --- (2)

(1) 10.3.0.183 の 53118 番ポート から 8.8.8.8 の 53 番ポートに問い合わせ (2) 8.8.8.8 の 53 番ポートから 10.3.0.183 の 53118 番ポートに応答

  • 53118 番ポートは OS が自動採番する
  • 例では dig がスタブリゾルバに該当する

ポート番号:

  • 0 〜 1023(システムポート、ウェルノウンポート)
  • 1024 〜 49151(ユーザーポート、レジスターポート)
  • 49152 〜 65535(ダイナミックポート、プライベートポート)

AWS VPC フルリゾルバーに依頼

AWS の VPC 内からの名前解決は AWS VPC が提供するフルリゾルバー(ホストアドレス +2 例: 10.2.0.0/16 なら 10.2.0.2/16 )が使用されます(@8.8.8.8 のように依頼する DNS サーバを指定しない場合)。

dig example.orgのように外部ドメインに対しても VPC が提供するフルリゾルバーが参照されます(ただし@8.8.8.8 のように外部サーバを指定しない場合)。

ref. AWS Route53

  • VPCは 10.2.0.0/16 と 10.3.0.0/16
  • インスタンスのプライベートIP
    • 10.2.2.159
    • 10.3.0.183 <– こちらから試している
sudo tcpdump -tnl -i eth0 udp port 53

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
dig ip-10-2-2-159.ap-northeast-1.compute.internal

; <<>> DiG 9.16.1-Ubuntu <<>> ip-10-2-2-159.ap-northeast-1.compute.internal
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51631
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;ip-10-2-2-159.ap-northeast-1.compute.internal. IN A

;; ANSWER SECTION:
ip-10-2-2-159.ap-northeast-1.compute.internal. 60 IN A 10.2.2.159

;; Query time: 3 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Apr 09 02:13:29 UTC 2022
;; MSG SIZE  rcvd: 90
sudo tcpdump -tnl -i eth0 udp port 53

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
IP 10.3.0.183.48790 > 10.3.0.2.53: 61285+ [1au] A? ip-10-2-2-159.ap-northeast-1.compute.internal. (74) ---(1)
IP 10.3.0.2.53 > 10.3.0.183.48790: 61285 1/0/1 A 10.2.2.159 (90) ---(2)
  1. dig スタブリゾルバ(10.3.0.183.48790)からフルリゾルバ(10.3.0.2.53: 61285)に依頼
  2. フルリゾルバ(10.3.0.2.53: 61285)からスタブリゾルバ(10.3.0.183.48790)に返信