When you create an Elastic Compute Service (ECS) instance, the default settings are used for the DNS server. In most cases, no additional configurations required. If the DNS server cannot meet your needs, you can customize DNS settings in the ECS instance. This topic describes how to customize the DNS settings of a Linux instance.
Step 1: Customize DNS settings
Multiple Linux distributions are available for use, and the methods used to customize DNS settings may vary between different distributions. Use the distribution-specific commands to perform this operation. After DNS settings are customized, perform the procedures in Step 2 to ensure that configurations are not restored when the system is restarted.
Alibaba Cloud Linux
Ubuntu
CentOS
Debian
SUSE Linux
OpenSUSE
Alibaba Cloud Linux 2 or 3
For information about how to customize the DNS settings of an instance that runs Alibaba Cloud Linux 2 or 3, see How do I use the /etc/resolv.conf file to customize DNS configuration in an Alibaba Cloud Linux 2 instance?
Ubuntu 18 or 20
Most common Linux distributions use NetworkManager to manage network settings. However, Ubuntu 18 and later use systemd-networkd instead. If your instance runs Ubuntu 18 or 20, perform the following steps to customize DNS settings.
The operations in the following steps will modify instance configuration files. We recommend that you back up the files before you modify them.
Run the following command to edit the
/etc/cloud/cloud.cfg
configuration file:vim /etc/cloud/cloud.cfg
Press the
I
key to enter the edit mode. Insert the following content above# Example datasource config
to disable the parameters used for automatic network configuration in cloud-init. This ensures that the network interface controller (NIC) configuration file is not overwritten.network: config: disabled
A command output similar to the following one indicates that the file is modified.
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 edit the
/etc/netplan/50-cloud-init.yaml
configuration file:vim /etc/netplan/50-cloud-init.yaml
Press the
I
key to enter the edit mode. At the end of the file, add the following custom DNS settings:nameservers: addresses:[223.5.XX.XX,223.6.XX.XX]
Check the following content:
network: version: 2 ethernets: eth0: dhcp4: true match: macaddress: 00:16:3e:13:42:71 set-name: eth0 nameservers: addresses: [223.5.XX.XX,223.6.XX.XX]
Noteeth0: the eth0 NIC. You can specify other NICs based on your needs.
dhcp4: indicates that the network settings of your instance default to Dynamic Host Configuration Protocol (DHCP) mode.
match: the MAC address. In this example, a MAC address of
00:16:3e:13:42:71
is used. You can specify MAC addresses based on your needs.nameservers: the custom DNS settings. In this example,
223.5.XX.XX
and223.6.XX.XX
are used as the default DNS IP addresses. You can specify DNS IP addresses based on your needs.
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 apply the configurations to the NIC:
netplan apply
To ensure that configurations are not restored due to updates of the
/etc/resolv.conf
file, run the following command to set up a symbolic link to the/run/systemd/resolve/resolv.conf
file:sudo ln -snf /run/systemd/resolve/resolv.conf /etc/resolv.conf
CentOS 6, 7, or 8
In CentOS, network settings and DNS settings are separately managed by different services. These services need to be maintained individually.
Network services (services used to manage network settings)
In CentOS 6 and CentOS 7, the Network service is used to manage network settings.
In CentOS 8, NetworkManager.service is used to manage network settings.
CentOS 8 and earlier use the systemd-resolved service to manage DNS settings.
To simplify network management, you can perform the following steps to add DNS settings to the NIC configuration file and then use the network service to manage both network and DNS settings.
The operations in the following steps will modify instance configuration files. We recommend that you back up the files before you modify them.
Repeat Step 1 in the Ubuntu 18 or 20 section to disable the parameters used for automatic network configuration in cloud-init to ensure that the NIC configuration file is not overwritten.
Run the following command to edit the
/etc/sysconfig/network-scripts/ifcfg-eth0
configuration file:vim /etc/sysconfig/network-scripts/ifcfg-eth0
Press the
I
key to enter the edit mode. At the end of the file, add the following custom DNS settings:PEERDNS=no DNS1=[223.5.XX.XX] DNS2=[223.6.XX.XX]
Check the following content:
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes STARTMODE=auto TYPE=Ethernet USERCTL=no PEERDNS=no DNS1=[223.5.XX.XX] DNS2=[223.6.XX.XX]
NotePEERDNS=no: indicates that DHCP-provided DNS servers are not used.
DNS1 and DNS2: the custom DNS settings. In this example,
223.5.XX.XX
and223.6.XX.XX
are used as the default DNS IP addresses. You can specify DNS IP addresses based on your needs.
After you complete the modifications, press the
Esc
key, enter:wq
, and then press the Enter key to save and close the configuration file.
Different commands are used in CentOS 6, 7, and 8 to restart network services. Perform the following steps to restart the network service based on the CentOS version of your instance:
CentOS 6: Run the
service network restart
command.CentOS 7: Run the
systemctl restart network
command.CentOS 8:
Run the
nmcli c reload
command to reload all NIC configurations.Run the
nmcli d connect eth0
command to activate the eth0 NIC.
Debian 9/Debian 10/Ubuntu 16
Debian 9, Debian 10, and Ubuntu 16 use the networking service to manage network settings. If your instance runs Debian 9, Debian 10, or Ubuntu 16, perform the following steps to customize DNS settings.
The operations in the following steps will modify instance configuration files. We recommend that you back up the files before you modify them.
Run the following command to edit the
/etc/network/interfaces
configuration file:vim /etc/network/interfaces
Press the
I
key to enter the edit mode. At the end of the file, add the following custom DNS settings:dns-nameservers [223.5.XX.XX 223.6.XX.XX]
Check the following content:
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp dns-nameservers [223.5.XX.XX 223.6.XX.XX]
Noteauto eth0: indicates the eth0 NIC. You can use other NICs based on your needs.
iface eth0 inet dhcp: indicates that the network settings of the instance default to DHCP mode.
dns-nameservers: the custom DNS settings. In this example,
223.5.XX.XX
and223.6.XX.XX
are used as the default DNS IP addresses. You can specify DNS IP addresses based on your needs.
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 restart the network service:
systemctl restart networking.service
SUSE Linux 12/SUSE Linux 15/OpenSUSE 15
Most common Linux distributions use NetworkManager to manage network settings. However, SUSE Linux 12 and later use Wicked instead. If your instance runs SUSE Linux 12, SUSE Linux 15, or openSUSE 15, perform the following steps to customize DNS settings:
Run the following command to edit the
/etc/resolv.conf
configuration file:vim /etc/resolv.conf
Press the
I
key to enter the edit mode. Setnameserver
to your specified DNS IP addresses.nameserver [223.5.XX.XX] nameserver [223.6.XX.XX]
Notenameservers: the custom DNS settings. In this example,
223.5.XX.XX
and223.6.XX.XX
are used as the default DNS IP addresses. You can specify DNS IP addresses based on your needs.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 edit the
/etc/sysconfig/network/config
configuration file:vim /etc/sysconfig/network/config
Press the
I
key to enter the edit mode. Delete the value ofNETCONFIG_MODULES_ORDER
and retain the double quotation marks ("").NETCONFIG_MODULES_ORDER=""
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 restart the network service:
service network restart
Step 2 (Optional): Lock files
After you complete Step 1, you can restart the instance to check whether DNS settings are preserved. Some images may contain more recent versions of cloud-init. These versions of cloud-init will cause network settings to be reset when cloud-init is restarted. If this occurs, you can perform the following operations to lock the configuration files to ensure that the files are not reset on system restart.
For more information about cloud-init, see official cloud-init documentation.
Lock configuration files. To lock the
/etc/resolv.conf
configuration file, run the following command:chattr +i /etc/resolv.conf
Unlock the configuration files.
If you want to modify locked configuration files, unlock them. To unlock the /etc/resolv.conf file, run the following command:
chattr -i /etc/resolv.conf
Run the following command to check whether a domain name can be properly resolved:
nslookup <$Domain>
Note<$Domain> : the domain name. Specify a domain name based on your needs.