The manul configuration of DNS server addresses in the EC2 instance are lost when the instances are restarted.

In this blog post , We will see how to configure Static DNS Server addresses on the Linux EC2 Instances.

Important Note:

Before making any modifications to the Instance’s , Take a backup of instance using AMI or EBS Snapshots.

Assigning Static DNS Servers

We will see , how to configure static DNS server addresses on the Linux EC2 instance of different distributions.

Setting DNS : Amazon Linux , Amazon Linux 2

Login to the Amazon Linux EC2 instance using the SSH client.

make sure you have root or sudo privileges to perform this.

Open the /etc/dhcp/dhclient.conf file

vi /etc/dhcp/dhclient.conf

Add add the below line , Replace xxx.xxx.xxx.xxx with the IP address with the DNS server addresses.

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

Once added Save and Close the file.

Set the PEERDNS=yes (If not) in all the network interfaces attached with the EC2 instance , Such as : /etc/sysconfig/network-scripts/ifcfg-eth0

Once added , Save and close the file.

We need to reboot the instance for the changes to take effect.

After reboot , The DNS addresses will be reflected in /etc/resolv.conf.

Setting DNS : Ubuntu 18.04

On Ubuntu 18.04 EC2 instances , The network interface configurations are handled by netpack.io packages.

Also the systemd-resolved service is enabled to handle DNS queries using stub resolver.

So in order to change the DNS servers , We need to follow the below procedures.

Netplan stores the configurations in /etc/netplan folder.We need a create a file inside this folder and setup DNS configurations there.

Create a file named 99-custom-dns.yaml and add the below contents into the file and don’t forget to replace x.x.x.x with the DNS server addresses

network:
     version: 2
     ethernets:
       eth0:
         nameservers:
              addresses: [x.x.x.x, x.x.x.x]
         dhcp4-overrides:
              use-dns: false

Once the configurations ate added , Save and close the file.

For the changes to take effect , We need to reboot the instance.

After the instance reboot , We should verify whether the DNS records are updated by running the below command.

systemd-resolve --status

For the network interface (eth0) , You should see that the DNS records are updated properly.

Setting DNS : Ubuntu 16.04

Login to the Ubuntu 16.04 EC2 instance using the SSH client.

Make sure you have root or sudo privileges to perform this operation.

Open the /etc/dhcp/dhclient.conf file

vi /etc/dhcp/dhclient.conf

Add add the below line , Replace xxx.xxx.xxx.xxx with the IP address with the DNS server addressess.

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

Once added Save and Close the file.

Set the PEERDNS=yes in all the network interfaces attached with the EC2 instance , Such as : /etc/sysconfig/network-scripts/ifcfg-eth0

Once added , We need to reboot the instance for the changes to take effect.

After reboot , The DNS addresses will be reflected in /etc/resolv.conf.

Setting DNS : RHEL 7.5

Login to the Redhat Linux EC2 instance using the SSH client.

Open the /etc/dhcp/dhclient.conf file

vi /etc/dhcp/dhclient.conf

Add add the below line , Replace xxx.xxx.xxx.xxx with the IP address with the DNS addressess of the Directory services.

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

Once added Save and Close the file.

Set the PEERDNS=yes in all the network interfaces attached with the EC2 instance , Such as : /etc/sysconfig/network-scripts/ifcfg-eth0

Once added , We need to reboot the instance for the changes to take effect.

After reboot , The DNS addresses will be reflected in /etc/resolv.conf.

Conclusion

Now we have learnt to configure Static DNS server addresses on the Linux EC2 Instances across distributions.

Hope you find it helpful.

Please do check out my other publications.

Leave a Reply