(upbeat electronic music)
- Hello, I'm Pooja, a Cloud Support Engineer
here at the AWS office in Bangalore, India.
Sometimes customers ask me how to assign
a custom DNS server to their EC2 instance
that persist across reboots,
because any manual entry in /etc/resolv.conf
is overwritten during reboots.
Let me walk you through this process.
By default, at instance start-up,
the EC2 instance solicits the DNS server from the VPC
which services the request
using the configured DHCP Options Set.
Note that the DNS server set in the VPC's DHCP Options Set
is configured in the /etc/resolv.conf
of all the EC2 instances in that VPC by default.
If the VPC in question is the default VPC of the Region,
the DNS server IP address is 172.31.0.2.
Therefore,
if your use case requires you to have
a custom and static DNS server
across all the EC2 instances in that VPC,
creating a new DHCP Options Set with the required DNS server
and associating it with the VPC is a more feasible option.
However, bear in mind that any changes
to the VPC's DHCP Options Set
will be reflected on every resource in that VPC.
Therefore, thoroughly analyze your use case
before considering any such change.
To use the custom DNS server for only a particular instance,
a manual entry in /etc/resolv.conf won't work,
as that file is overwritten
by the DHCP Options Set DNS server on reboot.
To be sure that
the custom DNS server persists across reboots,
use the supersede clause in /etc/dhcp/dhclient.conf.
Note that if this file does not exist, you must create it.
This suggested solution
is tested on Amazon Linux 2017.09,
RHEL 7.4, and Ubuntu 16.04.
Note that this file needs to be created in RHEL 7.4.
Let's take the example of Amazon Linux,
and let me show you how you can configure
the custom DNS server in the dhclient.conf.
Now, let us ssh into the Amazon Linux EC2 instance.
Let me get to the root permissions, sudo su.
Now, if we analyze,
or if we see the content of /etc/resolv.conf,
we will see that the configured DNS server is 172.31.0.2.
That is because the VPC that I am in
is the default VPC of this region.
Now, if I do an nslookup on Amazon.com,
we will see that the DNS server being used
is the one that is configured in /etc/resolv.conf.
Supposingly, you want to edit this DNS server
and have a custom DNS server of your own,
you would need to edit the /etc/dhcp/dhclient.conf.
Let us go into that directory.
You would see that Amazon Linux has a dhclient.conf file.
You can choose any favorite editor of yours,
I'm using Kwrite.
Within this file, I would need to add the supersede clause.
For this purpose,
I'm going to choose any publicly available DNS server.
So right now I'm going with 8.8.8.8 followed by a semicolon.
Let me save and exit from the file.
Now, we would need to reboot the instance
for the change to take effect.
Let us give a few moments for the instance to come back.
Let's ssh back to the instance.
Now, if you see the content of /etc/resolv.conf,
we'll be able to see that
the DNS server IP address has changed
to the one that we edited in /etc/dhcp/dhclient.conf file.
Also, if we nslookup to any domain name,
any website, say Amazon.com,
we'll see that the DNS server being used is 8.8.8.8.
We can see that the instance
which normally would revert
to the VPC's DNS server on reboot
is now configured to use the customer DNS server.
This setting persists across any subsequent reboots.
Thanks for watching and happy cloud computing
from all of us here at AWS.
(upbeat electronic music)