How can I avoid DNS resolution failures with an Amazon EC2 Linux instance?

6 minute read

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

Short description

To decrease CPU and network usage and avoid DNS resolution failures, apply a DNS cache.

If you use a DNS cache to query external DNS resources, then the cache answers most of the recurring DNS queries locally. When the cache does this, it doesn't interact with the DNS resolver over the network. You can query external DNS resources such as the following examples:

  • Amazon Relational Database Service (Amazon RDS)
  • Amazon ElastiCache
  • Amazon Simple Storage Service (Amazon S3)

The following procedure applies to all versions of Amazon Linux. If you use another distribution, then select the documentation for your distribution from the following list:


Set up a local DNS cache with dnsmasq

To set up a local DNS cache, use dnsmasq (a DHCP and cache DNS server). For an overview of dnsmasq, see the dnsmasq man page.

  1. Install the dnsmasq server:

    sudo yum install -y dnsmasq
  2. Create a dedicated system user to run dnsmasq:

    sudo groupadd -r dnsmasqsudo useradd -r -g dnsmasq dnsmasq

    Note: dnsmasq typically runs as the root user, but it changes to another user after startup (by default, the user is nobody). This drops root privileges.

  3. Create a copy of the dnsmasq.conf file:

    sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

    Then, open the configuration file with a text editor (for example, vim):

    sudo vim /etc/dnsmasq.conf
  4. Edit the /etc/dnsmasq.conf file so that it's similar to the following example:

    # Server Configurationlisten-address=
    # Name resolution options

    Note: The bogus-priv option in dnsmasq causes a reverse DNS lookup to fail for private IP ranges that aren't in /etc/hosts or the DHCP leases file. If you want to perform a successful reverse lookup, then comment out or remove bogus-priv. For more information, see -b, --bogus-priv on the dnsmasq man page.

  5. Create the /etc/resolv.dnsmasq file, and then set the Amazon DNS server or the custom domain-name-servers that you specified on DHCP options sets:

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

    Note: For more information about DNS server locations, see What is DHCP? In some cases, you must adjust the file /etc/resolv.dnsmasq to use the name server for that network. Adjust the file when you create an AMI from an instance with the dnsmasq cache to launch in another VPC with a different CIDR. Or, adjust the file when you specify a custom DNS server in your DHCP options.

  6. Restart the dnsmasq server, and set the service to start up on boot:
    Amazon Linux 1

    sudo service dnsmasq restart
    sudo chkconfig dnsmasq on

    Amazon Linux 2 and Amazon Linux 2023

    sudo systemctl restart dnsmasq.service
    sudo systemctl enable dnsmasq.service
  7. To verify that dnsmasq works correctly, use the dig command:

    dig @

    If the response is similar to the following example, then the dnsmasq cache works correctly:

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.56.amzn1 <<>> @;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25122
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
    ;            IN    A
    ;; ANSWER SECTION:        41    IN    A
    ;; Query time: 1 msec
    ;; SERVER:
  8. 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 more information, see My private Amazon EC2 instance is running Amazon Linux, Ubuntu, or RHEL. How do I assign a static DNS server to the EC2 instance that persists during reboot?

    Then, configure the default DNS resolver as a fallback option:

    sudo bash -c "echo 'supersede domain-name-servers,;' >> /etc/dhcp/dhclient.conf"
  9. To apply the change, run the dhclient command, restart the network service, or reboot your instance:
    Note: For Amazon Linux 2023, run sudo systemctl restart systemd-networkd.service instead of dhclient.

    sudo dhclient


    sudo systemctl restart network


    sudo reboot

    To verify that your instance uses the DNS cache, run the dig command:


    If the response indicates that the replying server is, then the DNS cache works correctly:

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.56.amzn1 <<>>;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1028
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
    ;            IN    A
    ;; ANSWER SECTION:        55    IN    A
    ;; Query time: 0 msec
    ;; SERVER: <<<-------

Automate dnsmasq

To automate the installation and configuration of dnsmasq as a DNS resolver on Amazon Linux, use one of the following options:

If you want to automate the dnsmasq installation on other Linux distributions, then use either file to make the necessary customization.

Because both files use the Amazon DNS server alternative address of, both files can run on VPC instances.

To run either file at launch time, pass the contents of the file in the user data field. To perform the actions on an instance, you can run the Bash script as a standalone script or with an AWS Systems Manager run command.

To run the Bash script as a standalone script, complete the following steps:

  1. Download the script on your instance, and make it runnable:

    wget +x
  2. Run the following command as a root user, or use sudo:

    sudo ./

Related information

Amazon EC2 instance IP addressing

AWS OFFICIALUpdated a month ago

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

replied 8 months ago

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

profile pictureAWS
replied 8 months ago

Any update on updating the document?

replied a month ago
profile pictureAWS
replied a month ago