All Products
Search
Document Center

:How do I use the /etc/resolv.conf file to customize DNS settings on an Alibaba Cloud Linux instance?

Last Updated:Jun 08, 2023

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.

Note
  • 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

  1. Log on to the ECS instance.

    For more information, see Connection methods.

  2. 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.

  3. Run the following command to move the /etc/resolv.conf file to another directory:

    mv /etc/resolv.conf /tmp/resolv.conf.bak
  4. Run the following command to recreate and edit the /etc/resolv.conf file:

    vim /etc/resolv.conf
    1. Press the I key to enter the edit mode. Add the following custom DNS settings to the file:

      nameserver <DNS1>
      nameserver <DNS2>
      Note
      • nameservers is the custom DNS settings.

      • <DNS1> and <DNS2> are your specified DNS IP addresses.

    2. After you complete the modifications, press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.

    3. 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
      Note

      To modify the /etc/resolv.conf file, you can run the chattr -i /etc/resolv.conf command to unlock the file.

  5. Run the following command to restart the systemd-resolved service:

    systemctl restart systemd-resolved
  6. 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

  1. Log on to the ECS instance.

    For more information, see Connection methods.

  2. 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
    1. Press the I key to enter the edit mode. Add the following settings to the file to remove DNS from NetworkManager:

      [main]
      dns=none
    2. After you complete the modifications, press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.

    3. Run the following command for the configuration to take effect:

      systemctl reload NetworkManager
  3. Run the following command to move the /etc/resolv.conf file to another directory:

    mv /etc/resolv.conf /tmp/resolv.conf.bak

  4. Run the following command to recreate and edit the /etc/resolv.conf file:

    vim /etc/resolv.conf
    1. Press the I key to enter the edit mode. Add the following custom DNS settings to the file:

      nameserver <DNS1>
      nameserver <DNS2>
      Note
      • nameservers is the custom DNS settings.

      • <DNS1> and <DNS2> are your specified DNS IP addresses.

    2. After you complete the modifications, press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.

    3. 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
      Note

      To modify the /etc/resolv.conf file, you can run the chattr -i /etc/resolv.conf command to unlock the file.

  5. Run the following command to check whether the custom DNS information in the /etc/resolv.conf file is changed:

    cat /etc/resolv.conf

References

Replace systemd-network settings with network-scripts settings for Alibaba Cloud Linux 2 ECS instances