To avoid DNS resolution failures, apply a DNS cache.
When you use a DNS cache to query external DNS resources, the cache locally answers most of the recurring DNS queries. In this scenario, the cache doesn't interact with the DNS resolver over the network. You can query external DNS resources such as the following examples:
You can use the following resolution steps for all versions of Amazon Linux. If you use another distribution, then see the documentation for your distribution:
-
Run the following command to install the dnsmasq server:
sudo yum install -y dnsmasq
-
If your instance runs on Amazon Linux 2023 (AL2023), then proceed to step 3. To create a dedicated system user to run dnsmasq, run the following command:
sudo groupadd -r dnsmasq
sudo useradd -r -g dnsmasq dnsmasq
Note: dnsmasq typically runs as the root user. However, the user changes after startup, and drops root permissions. By default, the user is nobody.
-
Run the following command to create a backup of the dnsmasq.conf file:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.ori
-
To create and open the /etc/dnsmasq.conf configuration file with the vim text editor, run the following command:
sudo vim /etc/dnsmasq.conf
Note: You can use any text editor, such as vi or nano, to modify the /etc/dnsmasq.conf file.
-
In the /etc/dnsmasq.conf file, enter the following content:
# Server Configuration
listen-address=127.0.0.1
port=53
bind-interfaces
user=dnsmasq
group=dnsmasq
pid-file=/var/run/dnsmasq.pid
# Name resolution options
resolv-file=/etc/resolv.dnsmasq
cache-size=500
neg-ttl=60
domain-needed
bogus-priv
Note: The bogus-priv option in dnsmasq causes a reverse DNS lookup to fail for private IP address ranges that aren't in /etc/hosts. This option can also cause a reverse DNS lookup to fail for the Dynamic Host Configuration Protocol (DHCP) leases file. To perform a successful reverse lookup, comment out or remove bogus-priv.
-
Create the /etc/resolv.dnsmasq file and set the Amazon DNS server or the custom domain name servers that you specified in the DHCP options sets. Run the following command:
sudo bash -c "echo 'nameserver 169.254.169.253' > /etc/resolv.dnsmasq"
Note: For more information about DNS server locations, see What is DHCP? You can also adjust the 169.254.169.253 server location when you create an Amazon Machine Image (AMI) from an instance with the dnsmasq cache to launch in another virtual private cloud (VPC) with a different CIDR.
-
To restart the dnsmasq server and set the service to start up on boot, run one of the following commands based on your distribution.
Amazon Linux 1 (AL1):
sudo service dnsmasq restart
sudo chkconfig dnsmasq on
Amazon Linux 2 (AL2) and AL2023:
sudo systemctl restart dnsmasq.service
sudo systemctl enable dnsmasq.service
-
To verify that dnsmasq works correctly, run the following command:
dig aws.amazon.com @127.0.0.1
If the response is similar to the following example, then the dnsmasq cache works as expected:
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.amzn2.13.11 <<>> aws.amazon.com @127.0.0.1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 33958
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;aws.amazon.com. IN A
;; ANSWER SECTION:
aws.amazon.com. 300 IN CNAME tp.8e49140c2-frontier.amazon.com.
tp.8e49140c2-frontier.amazon.com. 9 IN CNAME dr49lng3n1n2s.cloudfront.net.
dr49lng3n1n2s.cloudfront.net. 59 IN A 108.158.61.67
dr49lng3n1n2s.cloudfront.net. 59 IN A 108.158.61.79
dr49lng3n1n2s.cloudfront.net. 59 IN A 108.158.61.96
dr49lng3n1n2s.cloudfront.net. 59 IN A 108.158.61.102
;; Query time: 2 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
...
-
Set the dnsmasq DNS cache as the default DNS resolver.
Note: You must suppress the default DNS resolver that DHCP provides. To do this, change or create the /etc/dhcp/dhclient.conf file.
For AL2, run the following command:
sudo bash -c "echo 'supersede domain-name-servers 127.0.0.1, 169.254.169.253;' >> /etc/dhcp/dhclient.conf"
For AL2023, run the following command:
sudo bash -c 'echo "DNS=127.0.0.1" >> /etc/systemd/resolved.conf'
sudo bash -c 'echo "DNS=169.254.169.253" >> /etc/systemd/resolved.conf'
Note: AL2023 uses systemd-networkd as the default DNS resolver.
-
To apply the change, run the following command based on your Linux distribution:
AL2:
sudo dhclient
AL2023:
sudo systemctl restart systemd-resolved.service
-or-
To restart the network service, run the following command:
sudo systemctl restart network
-or-
To reboot your instance, run the following command:
sudo reboot
-
Run the following command to verify that your instance uses the DNS cache:
dig aws.amazon.com
If the output shows that the replying server is 127.0.0.1, then the DNS cache works as expected.
Example output:
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.amzn2.13.11 <<>> aws.amazon.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29129
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;aws.amazon.com. IN A
;; ANSWER SECTION:
aws.amazon.com. 297 IN CNAME tp.8e49140c2-frontier.amazon.com.
tp.8e49140c2-frontier.amazon.com. 25 IN CNAME dr49lng3n1n2s.cloudfront.net.
dr49lng3n1n2s.cloudfront.net. 41 IN A 108.158.61.102
dr49lng3n1n2s.cloudfront.net. 41 IN A 108.158.61.96
dr49lng3n1n2s.cloudfront.net. 41 IN A 108.158.61.79
dr49lng3n1n2s.cloudfront.net. 41 IN A 108.158.61.67
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) <-------------
...