All Products
Search
Document Center

How do I customize DNS settings on a Linux instance

Last Updated: Dec 31, 2020

Disclaimer: this article may be contributed by the community or involve third-party product information, we recommend that you visit the community or the official website of third-party products for help and support. Third-party products will not be provided by Alibaba Cloud after-sales support. This document is for reference only. Alibaba Cloud makes no guarantee by express or any other means.

Introduction

When an ECS instance is created, the default settings of the DNS server are used. If your DNS server cannot meet your business needs, you can customize the DNS configuration in the ECS or ECS Bare Metal Instance.

Detail

Alibaba Cloud reminds you that:

  • Before you perform operations that may cause risks, such as modifying instance configurations or data, we recommend that you check the disaster recovery and fault tolerance capabilities of the instances to ensure data security.
  • You can modify the configurations and data of instances including but not limited to Elastic Compute Service (ECS) and Relational Database Service (RDS) instances. Before the modification, we recommend that you create snapshots or enable RDS log backup.
  • If you have authorized or submitted sensitive information such as the logon account and password in the Alibaba Cloud Management Console, we recommend that you modify such information in a timely manner.

Linux there are different release versions, and the method of customizing the configuration DNS varies with the release version, you must select one of the following release commands based on the on-site environment: After the DNS configuration is complete, see file locking method to avoid system configuration restoration due to restart.

Alibaba Cloud Linux 2

In Alibaba Cloud Linux 2 instance customize configure DNS method of see how Alibaba Cloud Linux 2 instance by clicking a data point in the "/etc/resolv.conf file to customize configure DNS .

Ubuntu 18/20

Currently, most common releases use the NetworkManager service for network management. However, the network service type starting from Ubuntu 18 is systemd-networkd. The following are the steps to customize DNS configuration on Ubuntu 18 and Ubuntu 20:

Note: the following operations involve the modification of configuration files. Back up configuration files before modification.

  1. Run the following command to edit the /etc/cloud/cloud.cfg configuration file:
    vim /etc/cloud/cloud.cfg
    1. By i key cut change to edit mode, in # Example datasource config content, add the following configuration to close the cloud-init in automatically configure the network parameters to avoid network interface controller configuration file contents are covered .
      network:
      config: disabled
      The following command output is returned.
      Dingtalk_20201029153704.jpg
    2. After you modify the data, press Esc key, enter the : wq and hit enter to save and close the configuration file.
  2. Run the following command to edit the following configuration file:
    vim /etc/netplan/50-cloud-init.yaml
    1. By i key switched to edit mode, at the bottom of the file, add the following configuration, custom configure the DNS information.
      nameservers:
        addresses:[$DNS1, $DNS2]
    2. The modified content is as follows:
      network:
          version: 2
          ethernets:
              eth0:
                  dhcp4: true
                  match:
                      macaddress: 00:16:3e:13:42:71
                  set-name: eth0
                  nameservers:
                    addresses: [$DNS1, $DNS2]
      Description
      • eth0: configuration of the network interface controller name, this paper takes the eth0 network interface controller as an example, the on-site actual environment shall prevail.
      • dhcp4: the default mode is DHCP.
      • match: The MAC address. In this example, the MAC address is 00:16:3e:13:42:71. The configuration is subject to the actual environment.
      • NameServer: the custom DNS information. Enter your custom DNS IP address as needed.
    3. After you modify the data, press Esc key, enter the : wq and hit enter to save and close the configuration file.
  3. Run the following command to apply network interface controller configuration:
    netplan apply
  4. Run the following commands to establish a soft connection to the following file to avoid configuration restoration caused by updates to the /etc/resolv.conf configuration file.
    sudo ln -snf /run/systemd/resolve/resolv.conf /etc/resolv.conf

CentOS 6/7/8

In the CentOS system, network configuration and DNS configuration are managed by two different services, and it is difficult to maintain them separately.

  • Network service
    • Network is a Network service in CentOS 6 and CentOS 7.
    • The network service in CentOS 8 is NetworkManager.service.
  • DNS service
    Up to CentOS 8, systemd-resolved is currently used as the DNS management service.

To facilitate maintenance, you can manage the network and DNS configurations through the network service by following these steps. That is, DNS is also configured in the network interface controller configuration file.

Note: the following operations involve the modification of configuration files. Back up configuration files before modification.

  1. See ubuntu18/20 close cloud-init in automatically configure the network parameters to avoid network interface controller configuration file contents are covered .
  2. Run the following command to edit the following configuration file:
    vim /etc/sysconfig/network-scripts/ifcfg-eth0
    1. By i key cut change to the editing mode, in the bottom of the file, add the following configuration add the following custom DNS configuration
      PEERDNS=no
      DNS1=[$DNS1]
      DNS2=[$DNS1]
      The modified content is as follows:
      BOOTPROTO=dhcp
      DEVICE=eth0
      ONBOOT=yes
      STARTMODE=auto
      TYPE=Ethernet
      USERCTL=no
      PEERDNS=no
      DNS1=[$DNS1]
      DNS2=[$DNS1]
      Description
      • PEERDNS=no: specifies not to use the DNS server provided by DHCP.
      • DNS1 and DNS2: custom configured DNS information. Enter your custom DNS IP addresses as needed.
    2. After you modify the data, press Esc key, enter the : wq and hit enter to save and close the configuration file.
  3. The commands used to restart the network services in CentOS 6, CentOS 7, and CentOS 8 are different. Perform the following steps to restart the network services based on actual conditions.
    • CentOS 6: service network restart.
    • CentOS 7: systemctl restart network.
    • CentOS 8:
      • reload all network interface controller configured nmcli c reload.
      • Activation of the network interface controller nmcli d connect eth0.

Debian 9/Debian 10/Ubuntu 16

The network service used by Debian 10, Ubuntu 16 is networking. The following are the steps to customize the DNS configuration for Debian 9, Debian 10, and Ubuntu 16:

Note: the following operations involve the modification of configuration files. Back up configuration files before modification.

  1. Run the following command to edit the /etc/network/interfaces network interface controller configuration file:
    vim /etc/network/interfaces
    1. By i key switched to edit mode, at the bottom of the file, add the following configuration, custom configure the DNS information.
      dns-nameservers [$DNS1 $DNS2]
    2. The modified content is as follows:
      auto lo
      iface lo inet loopback
      auto eth0
      iface eth0 inet dhcp
      dns-nameservers [$DNS1 $DNS2]
      Description
      • auto eth0: configuration of the network interface controller name, this paper takes the eth0 network interface controller as an example, the on-site actual environment shall prevail.
      • iface eth0 inet dhcp: by default, the network configuration of ECS instances is DHCP mode.
      • dns-nameservers: Enter the DNS information.
    3. After you modify the data, press Esc key, enter the : wq and hit enter to save and close the configuration file.
  2. Run the following command to restart the network service:
    systemctl restart networking.service

SUSE Linux 12/SUSE Linux 15/OPENSUSE 15

At present, most common distributions use the NetworkManager service for network management. However, since SUSE Linux 12, SUSE Linux has used the named network management tool for network management. Perform the following steps to configure a custom DNS in SUSE Linux 12, SUSE Linux 15, and OPENSUSE:

  1. Run the following command to modify the configuration file resolv.conf:
    vim /etc/resolv.conf
    1. Press the i key to switch to the edit mode, and change the value of the nameserver parameter to your custom DNS IP address.
      nameserver [$DNS1] nameserver [$DNS2]
      Note: you must change the nameserver parameter value of the two parameters to a custom DNS IP address.
    2. After you modify the data, press Esc key, enter the : wq and hit enter to save and close the configuration file.
  2. Run the following command to edit the /etc/sysconfig/network/config configuration file:
    vim /etc/sysconfig/network/config
    1. By i key to switch to edit mode, NETCONFIG_MODULES_ORDER parameter delete retained double quotation marks symbols ("") as follows:
      NETCONFIG_MODULES_ORDER=""
    2. After you modify the data, press Esc key, enter the : wq and hit enter to save and close the configuration file.
  3. Run the following command to restart the network connection service:
    service network restart

File locking modes

After the DNS configuration is complete, you can restart the server to check whether the configuration is successful. cloud-init of later versions are installed on some custom images. The network configuration is automatically reset cloud-init you restart some servers. To avoid the resetting of the configuration file, use the following method to lock the configuration file:

Note: for more information about cloud-init, see cloud-init documentation.

  • Lock profile:
    Take the file /etc/resolv.conf as an example. Run the following command to lock the file.
    chattr +i /etc/resolv.conf
  • Unlock configuration file:
    • If you need to modify a locked file again, run the following command to unlock the file:
      chattr -i /etc/resolv.conf
    • Run the following command to verify that the domain name can be resolved normally.
      nslookup [$Domain]
      Note:[$Domain] is the Domain name to be tested.

Application scope

  • Elastic Compute Service