DHCP オプションセットのドメイン名を変更すると EC2 のホスト名も変更されるのか確認してみた

2022.03.24

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

DHCP オプションセットのドメイン名(Domain Name)を変更すると既存の EC2 インスタンスのデフォルト設定のホスト名は変更されるのか?という素朴な疑問を確認してみました。

DNS サーバー指定は変更することありますが、ドメイン名の方を深く考えたことがなかったので確認してみます。

確認結果

DHCP オプションセットのドメイン名を変更すると、既存の EC2インスタンスのホスト名(ドメイン名部分)は変更される。

# Before
$ hostname
ip-10-0-1-42.ap-northeast-1.compute.internal

# ----------------------------------------------------------
# DHCP オプションセットの変更と、EC2インスタンスの停止開始を実施
# ----------------------------------------------------------

# After
$ hostname
ip-10-0-1-42.hoge.example.com

検証環境

Amazon Linux 2のみで確認しています。

項目
OS Amazon Linux 2
AMI amzn2-ami-kernel-5.10-hvm-2.0.20220316.0-x86_64-gp2

Route 53 Resolver で名前解決できる設定としています。

項目 備考
enableDnsHostnames true VPC がパブリック IP アドレスを持つインスタンスへのパブリック DNS ホスト名の割り当てをサポートするかどうかを決定します。
enableDnsSupport true VPC が Amazon 提供の DNS サーバーを介した DNS 解決策をサポートするかどうかを決定します。

詳細: VPC の DNS 属性を変更する - Amazon Virtual Private Cloud

DHCP オプションセットのドメイン名とは

VPC で起動する EC2 のパブリック IPv4 アドレスに付加されるドメイン名とのことです。

Domain name: The domain name that will be appended to the public IPv4 addresses of EC2 instances launched into the VPC.

ドメイン名は東京リージョン(ap-northeast-1)であればap-northeast-1.compute.internalというドメイン名になります。

In the default options set, for VPCs in AWS Region us-east-1, the value is ec2.internal. For VPCs in other Regions, the value is region.compute.internal (for example, ap-northeast-1.compute.internal).

引用: Work with DHCP options sets - Amazon Virtual Private Cloud

ところで、バージニア北部(us-east-1)だけはec2.internalなんですね。それと、プライベートではなくパブリックなの?という新たな疑問が生まれました。

DHCP オプションセットのデフォルト設定

EC2 インスタンスのホスト名はhoge.ap-northeast-1.compute.internalとなることがドキュメントからわかりました。既存の EC2インスタンスが存在している状態で DHCP オプションセットのドメイン名を変更したときの影響がわかりませんでした。地味な確認作業の記録です。

EC2 の確認

EC2 インスタンスを起動しました。マネジメントコンソールからip-10-0-1-42.ap-northeast-1.compute.internalと確認できるのは、ホスト名のタイプと、プライベート IPv4 DNS名です。パブリック IPv4 DNS は別のドメイン名が付与されています。やはりプライベート IPv4 の名前なのでは?

対象
EC2 のプライベート IPv4 DNS ap-northeast-1.compute.internal
EC2 のパブリック IPv4 DNS ap-northeast-1.compute.amazonaws.com

EC2 インスタンスにログインしてホスト名を確認するとip-10-0-1-42.ap-northeast-1.compute.internal表示されました。

$ hostname
ip-10-0-1-42.ap-northeast-1.compute.internal

resolv.confを確認するとsearchのドメイン名にap-northeast-1.compute.internalが指定されています。

$ cat /etc/resolv.conf
; generated by /usr/sbin/dhclient-script
search ap-northeast-1.compute.internal
options timeout:2 attempts:5
nameserver 10.0.0.2

nslookupコマンドでホスト名を引いてみます。ドメイン名を省略しても名前解決できています。

$ nslookup ip-10-0-1-42
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   ip-10-0-1-42.ap-northeast-1.compute.internal
Address: 10.0.1.42

$ nslookup ip-10-0-1-42.ap-northeast-1.compute.internal
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   ip-10-0-1-42.ap-northeast-1.compute.internal
Address: 10.0.1.42

DHCP の設定を確認したいので先にネットワークインタフェースを確認します。

$ ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
    inet 10.0.1.42/24 brd 10.0.1.255 scope global dynamic eth0
       valid_lft 3464sec preferred_lft 3464sec

eth0のDHCPの設定情報を確認します。domain-nameap-northeast-1.compute.internalが設定されています。DHCP のリース期間を調べたことなかったのですが1時間(3600秒)なんですね。

$ cat /var/lib/dhclient/dhclient--eth0.lease
lease {
  interface "eth0";
  fixed-address 10.0.1.42;
  option subnet-mask 255.255.255.0;
  option routers 10.0.1.1;
  option dhcp-lease-time 3600;
  option dhcp-message-type 5;
  option domain-name-servers 10.0.0.2;
  option dhcp-server-identifier 10.0.1.1;
  option interface-mtu 9001;
  option broadcast-address 10.0.1.255;
  option host-name "ip-10-0-1-42";
  option domain-name "ap-northeast-1.compute.internal";
  renew 3 2022/03/23 02:36:40;
  rebind 3 2022/03/23 03:05:44;
  expire 3 2022/03/23 03:13:14;
}

Internal ELB の確認

EC2 のパブリックIPv4アドレスが対象と書いてありましたが、Internal ELB なら DHCP オプションセットのドメイン名の影響受けるのかな?と思って作ってみました。そして気がついたのですが Internal ELB でも付与されるドメイン名は ELB のもので関係ありませんでした。

紛らわしいですがap-northeast-1以降を比較してみてください。

対象
EC2 のプライベート IPv4 DNS ap-northeast-1.compute.internal
EC2 のパブリック IPv4 DNS ap-northeast-1.compute.amazonaws.com
ELB のドメイン名 ap-northeast-1.elb.amazonaws.com

作成してしまったので名前解決だけできることを確認しました。Internal ELBなのでサブネット違いのプライベートIPアドレスが返ってきました。

$ nslookup internal-DomainNameCheckInternalALB-1896622359.ap-northeast-1.elb.amazonaws.com
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   internal-DomainNameCheckInternalALB-1896622359.ap-northeast-1.elb.amazonaws.com
Address: 10.0.18.64
Name:   internal-DomainNameCheckInternalALB-1896622359.ap-northeast-1.elb.amazonaws.com
Address: 10.0.17.128

DHCP オプションセットを変更

既存の DHCP オプションセットの変更はできないため、ドメイン名をhoge.example.comとした DHCP オプションセットを新規作成しました。

VPC のオプションセットを変更して反映させました。

DHPC オプションセット変更後

EC2 インスタンスのホスト名に変化はあるのか?ここからが本題です。

EC2 の確認

リース期間が1時間でしたので放置して更新を待ってみました。更新されたタイミングでoption doman-nameが変更されていました。

$ cat /var/lib/dhclient/dhclient--eth0.lease
lease {
  interface "eth0";
  fixed-address 10.0.1.42;
  option subnet-mask 255.255.255.0;
  option routers 10.0.1.1;
  option dhcp-lease-time 3600;
  option dhcp-message-type 5;
  option domain-name-servers 10.0.0.2;
  option dhcp-server-identifier 10.0.1.1;
  option interface-mtu 9001;
  option broadcast-address 10.0.1.255;
  option host-name "ip-10-0-1-42";
  option domain-name "ap-northeast-1.compute.internal";
  renew 3 2022/03/23 03:00:14;
  rebind 3 2022/03/23 03:29:10;
  expire 3 2022/03/23 03:36:40;
}
lease {
  interface "eth0";
  fixed-address 10.0.1.42;
  option subnet-mask 255.255.255.0;
  option routers 10.0.1.1;
  option dhcp-lease-time 3600;
  option dhcp-message-type 5;
  option dhcp-server-identifier 10.0.1.1;
  option interface-mtu 9001;
  option broadcast-address 10.0.1.255;
  option host-name "ip-10-0-1-42";
  option domain-name "hoge.example.com";
  renew 3 2022/03/23 03:26:49;
  rebind 3 2022/03/23 03:52:44;
  expire 3 2022/03/23 04:00:14;
}

ドキュメントの記載通り、数時間以内に設定値が反映されました。

If you associate a new set of DHCP options with the VPC, any existing instances and all new instances that you launch in that VPC use the new options. You don't need to restart or relaunch your instances. Instances automatically pick up the changes within a few hours, depending on how frequently they renew their DHCP leases. If you prefer, you can explicitly renew the lease using the operating system on the instance.

引用: Work with DHCP options sets - Amazon Virtual Private Cloud

resolv.confを確認してみるとhoge.example.comが追加されていました。この挙動はOSや、バージョンによって異なる可能性があります。

$ cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /usr/sbin/dhclient-script
search hoge.example.com ap-northeast-1.compute.internal
nameserver 10.0.0.2

ホスト名を確認してみると変化はありません。

$ hostname
ip-10-0-1-42.ap-northeast-1.compute.internal

hoge.example.comドメイン指定すると名前解決できません。

$ nslookup ip-10-0-1-42.ap-northeast-1.compute.internal
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   ip-10-0-1-42.ap-northeast-1.compute.internal
Address: 10.0.1.42

$ nslookup ip-10-0-1-42.hoge.example.com
Server:         10.0.0.2
Address:        10.0.0.2#53

** server can't find ip-10-0-1-42.hoge.example.com: NXDOMAIN

それもそのはずでカスタムDNSドメイン名を使うなら Route 53 の Private hosted zones の設定が必要になります。

To access the resources in your VPC using custom DNS domain names, such as example.com, instead of using private IPv4 addresses or AWS-provided private DNS hostnames, you can create a private hosted zone in Route 53. A private hosted zone is a container that holds information about how you want to route traffic for a domain and its subdomains within one or more VPCs without exposing your resources to the internet.

引用: DNS attributes for your VPC - Amazon Virtual Private Cloud

Private hosted zone を作成しました。

レコード追加しました。

hoge.example.comで名前解決できるようになりました。

$ nslookup ip-10-0-1-42.hoge.example.com
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   ip-10-0-1-42.hoge.example.com
Address: 10.0.1.42

search の指定順序から hoge.example.com が優先されるため、nslookup ip-10-0-1-42は以下の様にhoge.example.comの名前で返ってきます。

$ nslookup ip-10-0-1-42
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   ip-10-0-1-42.hoge.example.com
Address: 10.0.1.42

resolv.conを手動で編集して、search の指定順序を変更します。ap-northeast-1.compute.internalが優先されるはずです。

$ cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /usr/sbin/dhclient-script
; search hoge.example.com ap-northeast-1.compute.internal
search ap-northeast-1.compute.internal hoge.example.com
nameserver 10.0.0.2

期待した通りap-northeast-1.compute.internalで返ってきます。

$ nslookup ip-10-0-1-42
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   ip-10-0-1-42.ap-northeast-1.compute.internal
Address: 10.0.1.42

ここまでの作業で以下のことを確認できました。

DHCP オプションセット変更後、起動したまま放置していた EC2インスタンスは

  • EC2 のホスト名(ip-10-0-1-42.ap-northeast-1.compute.internal)は変更されない
  • resolv.confの search の指定は DHCP オプションセットで指定したドメイン名が優先される変更が入る

再起動してみた

ホスト名は変更されませんでした。

$ hostname
ip-10-0-1-42.ap-northeast-1.compute.internal

手動で書き換えていたresolv.confは DHCP からの設定に置き換わり戻りました。

$ cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /usr/sbin/dhclient-script
search hoge.example.com ap-northeast-1.compute.internal
nameserver 10.0.0.2

停止・開始してみた

ホスト名が変更されました。

$ hostname
ip-10-0-1-42.hoge.example.com

search に登録されるドメイン名もhoge.example.comのみとなりました。

$ cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /usr/sbin/dhclient-script
search hoge.example.com
nameserver 10.0.0.2
$ nslookup ip-10-0-1-42
Server:         10.0.0.2
Address:        10.0.0.2#53

Non-authoritative answer:
Name:   ip-10-0-1-42.hoge.example.com
Address: 10.0.1.42

再起動と、停止・開始では大きな違いがありました。だけど、マネジメントコンソールから確認できるホスト名には変化ありませんでした。

プライベート IPv4 アドレス(Aレコード)のみが返されるとあるので、Aレコードを引けていないのかと思ったのですが、Private hosted zone を設定しレコード追加し引けているのではこの件ではないようです。

IP address-based naming (IPBN): The legacy naming scheme where, when you launch an instance, the private IPv4 address of the instance is included in the hostname of the instance. The IP address-based name exists for the life of the EC2 instance. When used as the Private DNS hostname, it will only return the private IPv4 address (A record).

引用: Amazon EC2 instance hostname types - Amazon Elastic Compute Cloud

新規に起動した EC2 インスタンス

DHCP オプションセット変更後の VPC に新たに EC2 インスタンスを起動して違いを確認します。

新規 EC2 の確認

マネジメントコンソール上、ホスト名はip-10-0-1-138.ap-northeast-1.compute.internalと表示されています。

resolv.confhoge.example.comのみ登録されています。デフォルトのドメイン名は登録されていません。

$ cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /usr/sbin/dhclient-script
search hoge.example.com
nameserver 10.0.0.2

ホスト名はhoge.example.comで表示されます。

$ hostname
ip-10-0-1-138.hoge.example.com

当然、レコードを登録していないため名前は引けませんでした。

$ nslookup ip-10-0-1-138
Server:		10.0.0.2
Address:	10.0.0.2#53

** server can't find ip-10-0-1-138: NXDOMAIN

既存 EC2 インスタンスを停止・開始後と状況です。

まとめ

DHCP オプションセットでドメイン名を変更すると、既存 EC2 インスタンスのホスト名がデフォルトのドメイン名(region..compute.internal)から指定したドメイン名に変更される。再起動では反映されなく、停止・開始するとホスト名の変更になる。

マネジメントコンソール上のパブリック IPv4 DNS、プライベート IP DNS名の表示はregion..compute.internalの表記しか確認できなかった。

Internal ELB のドメイン名とは関係ありませんでした。

対象
EC2 のプライベート IPv4 DNS ap-northeast-1.compute.internal
EC2 のパブリック IPv4 DNS ap-northeast-1.compute.amazonaws.com
ELB のドメイン名 ap-northeast-1.elb.amazonaws.com

おわりに

DHCP オプションセットの変更は VPC 全体に適用されるため、VPC 内の既存リソースにも影響があります。変更する際は事前に影響範囲を確認された方がよろしいかと思います。

AD on EC2 をはじめとする DNSサーバーを VPC内にたてると DNS サーバーの指定を DHCP オプションセットから行うことがあります。 ドメイン名を変更しようと思ったことがなかったので確認してみました。パブリック IPv4に付与されるドメイン名ではないのでは?は疑問のまま終わったのでドキュメントのフィードバックを出しました。更新か連絡があれば嬉しいです。

補足

DHCP オプションセットで DNSサーバーを未指定で作成し VPCに割り当てた場合

name-server設定が入ってこないため名前解決ができなくなります。DHCP オプションセット変更後、新たに起動したEC2で確認できました。既存の EC2は停止・開始しても以前のDNSサーバー(Route 53 Resolver)の値を保持していました。実際に変更する場合は影響範囲の調査は時間かかりそうです。

$ cat /etc/resolv.conf
options timeout:2 attempts:5
; generated by /usr/sbin/dhclient-script
search hoge.example.com

デフォルトの DHCP オプションセットを改めて確認してみると、ドメイン名、DNSサーバーが明示的に指定されていることに気づきました。

参考