How can I configure an Amazon EC2 instance with static DNS server entries that persist when the instance is rebooted? When I manually update my resolv.conf file with custom DNS server addresses, the updates are lost when the EC2 instance is restarted.

Default behavior for an EC2 instance associated with a virtual private cloud (VPC) is to request a DNS server address at startup using the Dynamic Host Configuration Protocol (DHCP). The VPC responds to DHCP requests with the address of an internal DNS server. The DNS server addresses returned in the DHCP response are written to the local /etc/resolv.conf file and are used for DNS name resolution requests. Any manual modifications to the resolv.conf file are overwritten when the instance is restarted.

To configure an EC2 instance running Linux to use static DNS server entries, use a text editor such as vim to edit the file /etc/dhcp/dhclient.conf and add the following line to the end of the file:

supersede domain-name-servers xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx;

Note: If this file does not exist, you must create it.

Where xxx.xxx.xxx.xxx is the IP address of a DNS server that you want the instance to use. Because this file is read-only, you must switch to a user account with sufficient privileges to make changes to the file before attempting to edit the file. This can typically be accomplished by preceding the command with sudo and providing the appropriate password to temporarily gain root privileges. As a result of this modification, the resolv.conf file will be updated to contain only the DNS servers that you specified when the instance is rebooted.

You can also use the supersede statement to override any options specified in the dhclient.conf file with a locally configured value.  For example, if you wanted to replace the domain search and domain name values returned by dhcp, you could add the following lines to the bottom of dhclient.conf:

supersede domain-search "example.com";
supersede domain-name "example.com";

Note: Be sure to reboot the instance so the changes take effect.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.

Published: 2015-11-06

Updated: 2017-06-30