Skip to content

How do I avoid DNS resolution failures in my Amazon EC2 Linux instance?

7 minute read
0

I want to avoid DNS resolution failures in my Amazon Elastic Compute Cloud (Amazon EC2) Linux instances.

Short description

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. The cache doesn't interact with the DNS resolver over the network. You can query external DNS resources such as Amazon Relational Database Service (Amazon RDS), Amazon ElastiCache, and Amazon Simple Storage Service (Amazon S3).

The following resolution steps work for all versions of Amazon Linux. If you use another distribution, then see the documentation for your distribution:

Resolution

Note: The following resolution steps use 169.254.169.253 as the DNS resolver IP address. If you use a custom DNS resolver, then replace 169.254.169.253 with your DNS resolver IP address.

Set up a local DNS cache

Complete the following steps to set up dnsmasq:

  1. Run the following command to install the dnsmasq server:

    sudo yum install -y dnsmasq
  2. If your EC2 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.

  3. Run the following command to create a backup of the dnsmasq.conf file:

    sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.ori
  4. 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.

  5. 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 reverse lookup, comment out or remove bogus-priv. If SSH over Session Manager, capability of AWS Systems Manager, doesn't work after you activate dnsmasq, then comment out domain-needed. This option blocks single-label names that Session Manager might require.

  6. Run the following command to create the /etc/resolv.dnsmasq file and set the name server:

    sudo bash -c "echo 'nameserver 169.254.169.253' > /etc/resolv.dnsmasq"

    Note: In the preceding command, you set the Amazon DNS server or the custom domain name servers that you specified in the DHCP options sets. For more information about DNS server locations, see What is DHCP?

  7. You can create an Amazon Machine Image (AMI) from the instance with the dnsmasq cache and launch in another virtual private cloud (VPC) with a different CIDR. In this scenario, you can change the 169.254.169.253 server location.

  8. To restart the dnsmasq server and set the service to start up on boot, run the following commands:

    sudo systemctl restart dnsmasq.service
    sudo systemctl enable dnsmasq.service
  9. To confirm 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)
    ...
  10. To suppress the default DNS resolver that DHCP provides, update or create the /etc/dhcp/dhclient.conf file.

  11. To set the dnsmasq DNS cache as the default DNS resolver, run the following command based on your distribution.
    AL2023:

    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-resolved as the default DNS resolver service, and systemd-networkd for network interface management.
    Amazon Linux 2 (AL2):

    sudo bash -c "echo 'supersede domain-name-servers 127.0.0.1, 169.254.169.253;' >> /etc/dhcp/dhclient.conf"
  12. To apply the change, take one of the following actions.
    To restart the DNS service, run the following command based on your Linux distribution.
    AL2023:

    sudo systemctl restart systemd-resolved.service

    AL2:

    sudo dhclient

    -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
  13. Run the following command to confirm 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)   <-------------
    ...

For more information, see dnsmasq on the thekelleys.org.uk website.

Note: For AL2023, you can use the DNS caching in systemd-resolved instead of dnsmasq. By default, DNS caching in systemd-resolved is deactivated.

Automate dnsmasq for new instances

To automatically install and configure dnsmasq as a DNS resolver on Amazon Linux instances, use the AutomateDnsmasq.sh bash script or AutomateDnsmasq.cloudinit directives. To download the files, see AutomateDnsmasq.sh or AutomateDnsmasq.cloudinit on the GitHub website.

To automate the dnsmasq installation on other Linux distributions, update one of the preceding files based on your distribution requirements. For example, update the package manager commands, the DNS resolver configuration method, and the user or group creation requirements.

Note: If you use the 169.254.169.253 Amazon DNS server, then the files can run on VPC instances.

To run either file at launch, enter the file contents in the instance's User data. Or, use Run Command, a capability of AWS Systems Manager, to run the bash script. It's a best practice to use the directives only for first-boot initialization.

To run the Bash script as a standalone script, run the following command to download the script on your instance.

wget https://raw.githubusercontent.com/awslabs/aws-support-tools/master/EC2/AutomateDnsmasq/AutomateDnsmasq.sh
chmod +x AutomateDnsmasq.sh

Note: If the instance is in a private subnet, then make sure that the instance can access the script.

Then, run the following command as a root user to run the script:

sudo ./AutomateDnsmasq.sh

Related information

Amazon EC2 instance IP addressing

AWS OFFICIALUpdated 5 months ago
7 Comments

These instructions don't work for Amazon Linux 2023. The command dhclient is not present.

replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
MODERATOR

replied 3 years ago

Any update on updating the document?

replied 3 years ago

AWS

replied 2 years ago

This article was reviewed and updated on 2026-06-19.

EXPERT

replied 5 months ago

Is dnsmasq really necessary since AL2023 has systemd-resolved installed just disabled? Any known issues with just

sudo rm /usr/lib/systemd/resolved.conf.d/resolved-disable-stub-listener.conf

sudo systemctl restart systemd-resolved

AWS

replied 3 months ago

Heads-up on the domain-needed line in the template: on a RHEL 9 fleet we found it breaks SSH port-forwarding over AWS Systems Manager Session Manager (aws ssm start-session / AWS-StartPortForwardingSession). The symptom is that sessions that worked before dnsmasq stop connecting after the config is applied; everything else (dig, application lookups) looks fine, so it takes a while to trace back to dnsmasq. Commenting out domain-needed (keeping cache-size, neg-ttl, bogus-priv) fixed it immediately.

Suggestion: add a note next to domain-needed similar to the existing bogus-priv caveat — e.g., "If SSH over Session Manager stops working after enabling dnsmasq, comment out domain-needed; it refuses to forward single-label names that the SSM tooling can rely on." Happy to share more detail if useful.

AWS

replied 2 months ago