To achieve customization and modification of DNS configuration for a Linux instance, you need to adjust the relevant configurations according to different versions and network management methods.
Note:
The following operations involve modifying configuration files. It is recommended to back up the configuration file before making any changes.
Network Management with NetworkManager
How to Tell If the Network Configuration Is Managed with NetworkManager?
Method 1: Check the status of NetworkManager.
sudo systemctl status NetworkManager
If the output shows that NetworkManager is running, it means the network is being managed with NetworkManager.
Example:
● NetworkManager.service - Network Manager Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2023-11-21 12:34:56 UTC; 1 day 12h ago
Method 2: Check the network interface configuration file.
1. Check the network interface configuration file: The traditional network interface configuration file is located in the /etc/sysconfig/network-scripts/
directory, and the file name is usually ifcfg-<interface-name>.
2. Check if the network interface is managed with NetworkManager: If the network interface is managed with NetworkManager
, the configuration file usually contains the parameter NM_CONTROLLED
with a value of yes
.
3. Check if the network interface is managed with the traditional network scripts: If the network interface is managed with the traditional network scripts, the configuration file usually contains the parameter NM_CONTROLLED
with a value of no
.
Customizing DNS Configuration in NetworkManager
TencentOS Server 3.X/TencentOS Server 4.X
If the system manages the network with NetworkManager by default and sets PEERDNS=no
in the /etc/sysconfig/network
file only, the customized DNS configuration will be overwritten during operations involving a restart.
It is recommended to modify /etc/NetworkManager/conf.d/90-dns-none.conf
. This way of modification has been verified that it will not overwrite the customized DNS configuration in scenarios such as restarting or switching subnet or VPC.
You simply need to add the following configuration to /etc/NetworkManager/conf.d/90-dns-none.conf
:
Network Management Not with NetworkManager
Network Management with Traditional Network Scripts
RHEL/CentOS7
1. The system manages network configuration with the traditional network scripts. These scripts are stored in the /etc/sysconfig/network-scripts/ directory, while the DNS configuration is located in the /etc/resolv.conf file. To prevent the /etc/resolv.conf file from being overwritten with the DNS IP addresses obtained via DHCP, you need to modify the /etc/sysconfig/network file and add the following line:
2. Restart the network service to have the changes take effect.
TencentOS Server 2.X
TencentOS Server 2.X series uses network to manage the network configuration by default. If TencentOS Server 2.X needs to switch subnet or switch VPC, it is recommended to install the NetworkManager package and use NetworkManager to manage the network configuration instead. If you do not use NetworkManager to manage the network, you can lock the file editing permissions.
Managing network configuration with NetworkManager.
1.1 Install NetworkManager.
yum install NetworkManager
1.2 Add the following two lines to the /etc/NetworkManager/conf.d/90-dns-none.conf
file:
Lock the file editing permissions to prevent the file resolv.conf from being overwritten.
Run the following command:
chattr +i /etc/resolv.conf
Network Management with Systemd-networkd
Ubuntu 18/20/22
Most common distributions use NetworkManager for network management currently. You may see Managing Network Configuration with NetworkManager for the steps. However, starting from Ubuntu 18 (including Ubuntu 18 and 20), for network service type systemd-networkd, the steps for customizing the DNS configuration are as follows: 1. Edit /etc/cloud/cloud.cfg
to disable automatic network parameter configuration in cloud-init to prevent the network interface configuration file from being overwritten.
network:
config: disabled
2. Edit the configuration file /etc/netplan/50-cloud-init.yaml
to add the custom DNS configuration.
nameservers:
addresses: [172.10.XX.XX,172.6.XX.XX]
After the modification, the following information will be displayed:
network:
version: 2
ethernets:
eth0:
dhcp4: true
match:
macaddress: 00:11:3e:13:42:7b
set-name: eth0
nameservers:
addresses: [172.10.XX.XX,172.6.XX.XX]
3. Run the following command to have the service take effect.
4. Run the following command to create a symbolic link to the file /run/systemd/resolve/resolv.conf
to prevent configuration rollback due to updates in the configuration file /etc/resolv.conf
.
sudo ln -snf /run/systemd/resolve/resolv.conf /etc/resolv.conf
Network Management with Networking
Debian 9/Debian 10/Debian 11/Ubuntu 16
For Debian 9, Debian 10, Debian 11, and Ubuntu 16 systems, the network service used is networking, and the steps for customizing the DNS configuration are as follows:
1. Edit the /etc/network/interfaces
network interface configuration file.
If the network interface configuration file in some higher version systems is configured as source /etc/network/interfaces.d/*
, the corresponding modifications need to be made in the file /etc/network/interfaces.d/50-cloud-init.cfg
. Simply edit /etc/network/interfaces
to add the following information:
dns-nameservers [172.10.XX.XX 172.6.XX.XX]
After the modification, the following information will be displayed:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
dns-nameservers [172.10.XX.XX 172.6.XX.XX]
2. Restart the network to have the service take effect.
systemctl restart networking.service
Network Management with Wicked
SUSE Linux 12/SUSE Linux 15/OpenSUSE 15
Most common distributions use the NetworkManager service for network management currently, but starting from SUSE Linux 12 (including SUSE Linux 12, SUSE Linux 15, and OpenSUSE 15), SUSE Linux uses Wicked for network management. The steps for customizing the DNS configuration are as follows:
1. Edit the configuration file /etc/resolv.conf
.
nameserver [223.1.XX.XX]
nameserver [223.2.XX.XX]
2. Edit the configuration file /etc/sysconfig/network/config
and delete the value of the parameter NETCONFIG_MODULES_ORDER
, keeping the double quotation marks (""), as shown below.
NETCONFIG_MODULES_ORDER=""
3. Restart the network to have the service take effect.
Note:
In a Linux system, if the file /etc/sysconfig/network
only sets PEERDNS=no
, the customized DNS configuration may be overwritten when the VPC and subnet are switched.
For the devices in a non-DHCP environment, see the section on DNS initialization (non-DHCP scenarios) for initialization operations in Cloud-Init & Cloudbase-Init.
Was this page helpful?