When you create an Elastic Compute Service (ECS) instance, the default settings are used for the Domain Name System (DNS) server. In most cases, no additional configurations are required. If the DNS server cannot meet your needs, you can customize DNS settings in the ECS instance. This topic describes how to customize DNS settings on an Alibaba Cloud Linux 2 or 3 instance.
Background information
After you customize DNS settings in the /etc/resolv.conf
file, you must lock the file to prevent the /etc/resolv.conf
file from being dynamically updated due to network reboots or operating system reboots.
In Alibaba Cloud Linux 2, the default network configuration service is systemd-networkd. In Alibaba Cloud Linux 3, the default network configuration service is NetworkManager.
In CentOS and other operating systems, the default network configuration service is network-scripts. For information about how to change the network configuration service of an Alibaba Cloud Linux instance to network-scripts, see the related documentation.
Procedure
Alibaba Cloud Linux 2
Log on to the ECS instance.
For more information, see Connection methods.
Run the following command to check whether the
/etc/resolv.conf
file is the symbolic link that points to the systemd-resolved service configuration file:ls -l /etc/resolv.conf
A command output similar to the following one indicates that the
/etc/resolv.conf
file is the symbolic link that points to the systemd-resolved service configuration file.Run the following command to move the
/etc/resolv.conf
file to another directory:mv /etc/resolv.conf /tmp/resolv.conf.bak
Run the following command to recreate and edit the
/etc/resolv.conf
file:vim /etc/resolv.conf
Press the
I
key to enter the edit mode. Add the following custom DNS settings to the file:nameserver <DNS1> nameserver <DNS2>
Notenameservers is the custom DNS settings.
<DNS1> and <DNS2> are your specified DNS IP addresses.
After you complete the modifications, press the
Esc
key, enter:wq
, and then press the Enter key to save and close the configuration file.Run the following command to lock the
/etc/resolv.conf
configuration file to prevent the/etc/resolv.conf
file from being dynamically updated due to network reboots or operating system reboots:chattr +i /etc/resolv.conf
NoteTo modify the
/etc/resolv.conf
file, you can run thechattr -i /etc/resolv.conf
command to unlock the file.
Run the following command to restart the systemd-resolved service:
systemctl restart systemd-resolved
Run the following command to check whether the custom DNS information in the
/etc/resolv.conf
file is changed:cat /etc/resolv.conf
Alibaba Cloud Linux 3
Log on to the ECS instance.
For more information, see Connection methods.
Run the following command to create the
/etc/NetworkManager/conf.d/90-dns-none.conf
file:vim /etc/NetworkManager/conf.d/90-dns-none.conf
Press the
I
key to enter the edit mode. Add the following settings to the file to remove DNS from NetworkManager:[main] dns=none
After you complete the modifications, press the
Esc
key, enter:wq
, and then press the Enter key to save and close the configuration file.Run the following command for the configuration to take effect:
systemctl reload NetworkManager
Run the following command to move the
/etc/resolv.conf
file to another directory:mv /etc/resolv.conf /tmp/resolv.conf.bak
Run the following command to recreate and edit the
/etc/resolv.conf
file:vim /etc/resolv.conf
Press the
I
key to enter the edit mode. Add the following custom DNS settings to the file:nameserver <DNS1> nameserver <DNS2>
Notenameservers is the custom DNS settings.
<DNS1> and <DNS2> are your specified DNS IP addresses.
After you complete the modifications, press the
Esc
key, enter:wq
, and then press the Enter key to save and close the configuration file.Run the following command to lock the
/etc/resolv.conf
configuration file to prevent the content of the/etc/resolv.conf
file from being dynamically updated due to network reboots or operating system reboots:chattr +i /etc/resolv.conf
NoteTo modify the
/etc/resolv.conf
file, you can run thechattr -i /etc/resolv.conf
command to unlock the file.
Run the following command to check whether the custom DNS information in the
/etc/resolv.conf
file is changed:cat /etc/resolv.conf