To force the use of a custom DNS server when obtaining an IP address from DHCP in the ifcfg config file, you can follow these steps:

  1. Open the ifcfg config file for the specific network interface you want to configure. The file is usually located in the /etc/sysconfig/network-scripts/ directory and has a name like ifcfg-eth0 or ifcfg-ens33.

  2. Edit the file using a text editor such as vi or nano.

  3. Look for the line starting with BOOTPROTO and change its value to BOOTPROTO=dhcp. This ensures that the IP address is still obtained from DHCP.

  4. Add the following lines to the file to specify the custom DNS server(s):

    DNS1='custom_dns_server_1'
    DNS2='custom_dns_server_2'
    

    Replace 'custom_dns_server_1' and 'custom_dns_server_2' with the IP addresses of your desired DNS servers. You can add additional lines (DNS3, DNS4, etc.) if needed.

  5. Save and exit the file.

  6. To apply the changes, restart the network service by running the following command:

    sudo systemctl restart network
    

    Alternatively, you can also restart the specific network interface by running:

    sudo ifdown <interface_name>
    sudo ifup <interface_name>
    

    Replace <interface_name> with the name of the network interface you modified (e.g., eth0 or ens33).

After restarting the network service or the specific network interface, the custom DNS servers should be used instead of the ones provided by DHCP.

Force Custom DNS Server with DHCP on Linux