LPIC あずき本v5.0 ch11 ネットワークの基礎 11.4

LPICLinuxネットワーク勉強メモ

出典: 


DNSの設定

DNSの概要

  • IPアドレス

    • TCP/IPネットワークにおいてネットワーク上のコンピュータを識別
  • 人間にとって扱いづらいので、ホスト名でコンピュータを指定できるようにしている
  • 名前解決

    • ホスト名とIPアドレスとの相互変換
    • 正引き

      • ホスト名 -> IPアドレス
    • 逆引き

      • IPアドレス -> ホスト名

        • netstatコマンドでデフォルト有効になっているのはこれ
  • /etc/hosts

    • ホスト名とIPアドレスとの対応情報が保存されているファイル
    • ホストが少数・更新頻度が低いならこれでいい
  • DNS: Domain Name System

    • ホストが多くなったり更新頻度が高くなったりしたら必要になってくる
    • 名前解決サービスを提供
  • ほかにもいっぱいある

    • LDAPとか
  • ホスト名

    • www.example.comとか

      • www: ホスト名
      • example.com: ドメイン名
    • FQDN: Fully Qualified Domain Name(完全修飾ドメイン名)

      • www.example.comにあたるもの。省略なし
    • ドメインは階層構造になっている

      • ルートドメイン
      • トップレベルドメイン

        • com
      • セカンドレベルドメイン

        • example

DNSの設定ファイル

/etc/resolv.conf

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0
  • 直接触っちゃだめ
# This file is managed by man:systemd-resolved(8). Do not edit.
  • systemd-resolved.serviceが絡んでいる(後述)
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
systemctl status systemd-resolved
● systemd-resolved.service - Network Name Resolution
   Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-04-12 16:53:31 JST; 5 days ago
...
  • ローカルの53番ポートでDNSサーバが動いている
nameserver 127.0.0.53
ss -atupn | grep LISTEN
tcp   LISTEN     0       128                               127.0.0.53%lo:53                                                      0.0.0.0:*                       users:(("systemd-resolve",pid=624,fd=13))                                      
tcp   LISTEN     0       128                                     0.0.0.0:22                                                      0.0.0.0:*                       users:(("sshd",pid=28294,fd=3))                                                
tcp   LISTEN     0       5                                     127.0.0.1:631                                                     0.0.0.0:*                       users:(("cupsd",pid=10044,fd=6))                                               
tcp   LISTEN     0       128                                        [::]:22                                                         [::]:*                       users:(("sshd",pid=28294,fd=4))                                                
tcp   LISTEN     0       5                                         [::1]:631                                                        [::]:*                       users:(("cupsd",pid=10044,fd=5))      
  • 抜粋

    • ループバックの53番ポート

      • 53はDNSに割り当てられたwell-known port
127.0.0.53%lo:53    0.0.0.0:*    users:(("systemd-resolve",pid=624,fd=13))
  • 有名どころ

    • 8.8.8.8
    • 8.8.4.4

/etc/nsswitch.conf

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat systemd
group:          compat systemd
shadow:         compat
gshadow:        files

hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis
  • 名前解決の問い合わせ順
hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname
  • 名前解決の方法はさまざま

    • files

      • /etc/hosts
    • mdns4_minimal

      • multicast DNS IPv4
      • 名前解決しようとしたホスト名の最後に「.local」が含まれていないかアドレスの範囲が169.254.0.0/16(IPV4LL/APIPA/RFC3927)でない場合は名前解決を行わない
    • dns

      • DNS
    • ldap

      • LDAP
  • getent hosts

    • 全部加味したホスト名一覧を得る
getent hosts
127.0.0.1       localhost
127.0.1.1       wand-ThinkPad-X240s
127.0.0.1       ip6-localhost ip6-loopback

systemd-resolved

  • systemd採用システムにおいて名前解決に使われているサービス

/etc/systemd/resolved.conf

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
  • systemd-resolvedサービスが使われているシステムでは…

    • /etc/resolv.conf/run/以下ファイルへのシンボリックリンクだったりする
    • systemd-resolved管理下なので直接編集してはいけない
ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 39  2月 24 02:11 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

DNS管理コマンド

hostコマンド

  • DNSサーバを使ってホストやドメインに関する情報を表示する
  • デフォルトではホスト名とIPアドレスの変換を行う
host localhost
localhost has address 127.0.0.1
localhost has IPv6 address ::1
  • 詳細
host -v localhost
Trying "localhost"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31899
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;localhost.			IN	A

;; ANSWER SECTION:
localhost.		0	IN	A	127.0.0.1

Received 43 bytes from 127.0.0.53#53 in 0 ms
Trying "localhost"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16144
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;localhost.			IN	AAAA

;; ANSWER SECTION:
localhost.		0	IN	AAAA	::1

Received 55 bytes from 127.0.0.53#53 in 0 ms
Trying "localhost"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27054
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;localhost.			IN	MX

Received 27 bytes from 127.0.0.53#53 in 0 ms
  • 逆引き
host 127.0.0.1
1.0.0.127.in-addr.arpa domain name pointer localhost.

digコマンド

  • DNS interrogating の意?たぶん
dig [オプション] [@DNSサーバ名] ホストまたはドメイン名 [検索タイプ]
  • 正引き

    • デフォルトAレコード

      • Address
      • IPv4アドレス
dig @8.8.8.8 google.co.jp
# dig @8.8.8.8 google.co.jp a # おなじ
; <<>> DiG 9.11.3-1ubuntu1.5-Ubuntu <<>> @8.8.8.8 google.co.jp
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44089
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
google.co.jp.		68	IN	A	216.58.197.195

;; Query time: 37 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Apr 17 21:29:54 JST 2019
;; MSG SIZE  rcvd: 57
  • IPv6

    • IPv4は32ビット
    • IPv6は128ビット
    • Aレコードの4倍長だからAAAAレコード
dig @8.8.8.8 google.co.jp aaaa
; <<>> DiG 9.11.3-1ubuntu1.5-Ubuntu <<>> @8.8.8.8 google.co.jp aaaa
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22624
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.co.jp.			IN	AAAA

;; ANSWER SECTION:
google.co.jp.		299	IN	AAAA	2404:6800:4004:80a::2003

;; Query time: 43 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Apr 17 21:33:58 JST 2019
;; MSG SIZE  rcvd: 69
  • メールサーバ
dig @8.8.8.8 google.co.jp mx
; <<>> DiG 9.11.3-1ubuntu1.5-Ubuntu <<>> @8.8.8.8 google.co.jp mx
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53306
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.co.jp.			IN	MX

;; ANSWER SECTION:
google.co.jp.		599	IN	MX	20 alt1.aspmx.l.google.com.
google.co.jp.		599	IN	MX	10 aspmx.l.google.com.
google.co.jp.		599	IN	MX	40 alt3.aspmx.l.google.com.
google.co.jp.		599	IN	MX	30 alt2.aspmx.l.google.com.
google.co.jp.		599	IN	MX	50 alt4.aspmx.l.google.com.

;; Query time: 42 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Apr 17 21:34:20 JST 2019
;; MSG SIZE  rcvd: 159
  • ネームサーバ
dig @8.8.8.8 google.co.jp ns
; <<>> DiG 9.11.3-1ubuntu1.5-Ubuntu <<>> @8.8.8.8 google.co.jp ns
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51256
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.co.jp.			IN	NS

;; ANSWER SECTION:
google.co.jp.		21599	IN	NS	ns3.google.com.
google.co.jp.		21599	IN	NS	ns4.google.com.
google.co.jp.		21599	IN	NS	ns1.google.com.
google.co.jp.		21599	IN	NS	ns2.google.com.

;; Query time: 41 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Apr 17 21:34:47 JST 2019
;; MSG SIZE  rcvd: 123
  • ぜんぶ
dig @8.8.8.8 google.co.jp any
; <<>> DiG 9.11.3-1ubuntu1.5-Ubuntu <<>> @8.8.8.8 google.co.jp any
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57342
;; flags: qr rd ra; QUERY: 1, ANSWER: 14, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.co.jp.			IN	ANY

;; ANSWER SECTION:
google.co.jp.		299	IN	A	172.217.161.35
google.co.jp.		299	IN	AAAA	2404:6800:4004:80a::2003
google.co.jp.		599	IN	MX	20 alt1.aspmx.l.google.com.
google.co.jp.		599	IN	MX	50 alt4.aspmx.l.google.com.
google.co.jp.		299	IN	TXT	"v=spf1 -all"
google.co.jp.		59	IN	SOA	ns1.google.com. dns-admin.google.com. 243963248 900 900 1800 60
google.co.jp.		21599	IN	CAA	0 issue "pki.goog"
google.co.jp.		599	IN	MX	30 alt2.aspmx.l.google.com.
google.co.jp.		599	IN	MX	10 aspmx.l.google.com.
google.co.jp.		21599	IN	NS	ns3.google.com.
google.co.jp.		21599	IN	NS	ns2.google.com.
google.co.jp.		21599	IN	NS	ns4.google.com.
google.co.jp.		21599	IN	NS	ns1.google.com.
google.co.jp.		599	IN	MX	40 alt3.aspmx.l.google.com.

;; Query time: 40 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Apr 17 21:35:08 JST 2019
;; MSG SIZE  rcvd: 372
  • -xオプション

    • 逆引き