Saturday, April 22, 2023

Configure dns setting on linux machine

To configure DNS settings on a Linux machine, you can follow these steps:
1. Open the terminal on your Linux machine.
2. Edit the "/etc/resolv.conf" file using a text editor such as "nano" or "vi".
sudo nano /etc/resolv.conf
3. Add the IP addresses of your DNS servers to the file, one per line, in the following format:
nameserver <DNS_server_IP_address>
For example, if you want to use Google DNS servers, add the following lines:

  nameserver 8.8.8.8
   nameserver 8.8.4.4
   
4. Save the changes to the file and exit the text editor. Note: The "/etc/resolv.conf" file may be overwritten by your network manager, so you may need to configure your DNS settings in your network manager's settings as well.
5. Restart your network service to apply the new DNS settings. You can do this by running the following command:
sudo systemctl restart NetworkManager

If your "/etc/resolv.conf" file is getting overwritten after a reboot or network service restart, it is likely that your Linux distribution is using a dynamic network configuration tool, such as NetworkManager or dhclient, that manages DNS settings. These tools can overwrite the contents of the "/etc/resolv.conf" file with their own DNS configuration settings. To prevent these tools from overwriting the "/etc/resolv.conf" file, you can try one of the following methods: 1. Configure your network manager tool to use the "/etc/resolv.conf" file for DNS settings: For example, in NetworkManager, you can add the following line to the "[main]" section of the "/etc/NetworkManager/NetworkManager.conf" file:
dns=none
This will prevent NetworkManager from overwriting the "/etc/resolv.conf" file with its own settings. 2. Make the "/etc/resolv.conf" file immutable: You can use the "chattr" command to make the "/etc/resolv.conf" file immutable, which will prevent any changes to the file. However, this method can cause issues with certain network configurations.
sudo chattr +i /etc/resolv.conf
To remove the immutable flag, use the following command:

   sudo chattr -i /etc/resolv.conf
   
3. Create a script that runs at boot time to set the desired DNS settings: You can create a script that sets the desired DNS settings in the "/etc/resolv.conf" file and configure your system to run the script at boot time. For example, create a script named "set_dns.sh" with the following contents:

   #!/bin/bash
   echo "nameserver 8.8.8.8" > /etc/resolv.conf
   echo "nameserver 8.8.4.4" >> /etc/resolv.conf
   
Then, make the script executable:
sudo chmod +x set_dns.sh
Finally, add the script to the boot sequence:
sudo update-rc.d set_dns.sh defaults
By using one of these methods, you should be able to prevent your "/etc/resolv.conf" file from being overwritten and ensure that your desired DNS settings are used.