How to configure custom DNS on a Linux device

This guide will show you how to edit, add, or remove the DNS settings on your Linux device.


We will cover the following:

  1. Configure custom DNS on Linux
  2. Alternative solution to configure custom DNS (with Raspberry Pi)



Configure custom DNS on Linux

  1. Open the terminal with the CTRL + ALT + T combination and type in the following commands:

    sudo rm -r /etc/resolv.conf

    sudo nano /etc/resolv.conf

     

  2. You will be asked for your root password after each command line. Type it in and press Enter.

  3. When the text editor opens, you will have to type in these lines:


    nameserver 162.252.172.57

    nameserver 149.154.159.92

     

    LinxDNS.png

  4. Close and save the file by clicking Ctrl + X and pressing Y.

  5. After that, please type in one last command in the terminal and press Enter:

    sudo chattr +i /etc/resolv.conf


    linux_change_dns_2.png

    In case the chattr +i command does not work, try using the following command instead:

    sudo chattr -f +i /etc/resolv.conf

     

  6. Restart your Linux device.


Alternative solution to configure custom DNS (with Raspberry Pi)

  1. Open the terminal with the CTRL + ALT + T combination and type in the following commands:

    sudo nano /etc/dhcpcd.conf

  2. You will be asked for your root password. Type it in and press Enter.

  3. Find the section related to your VPN network interface (Typically, it’s going to be named tun0 or tap0 for OpenVPN connections or wg0 for Wireguard connections). It might look something like this:

    interface tun0

    NOTE: If this section does not exist, you can create it.

  4. Add or modify the static domain_name_servers line to specify the DNS servers you want to use. For example, to use Surfshark’s public DNS servers, modify it like so:

    interface tun0
    static domain_name_servers=162.252.172.57 149.154.159.92

  5. Save the changes by pressing CTRL + O, then press Enter. To exit, press CTRL + X.

    NOTE: Changes to /etc/dhcpcd.conf are generally more persistent and less likely to be overridden by automatic processes, so you do not need to apply the chattr +i command on it.

  6. Restart the network service to apply the new DNS settings using this command:

    sudo service dhcpcd restart

  7. Alternatively, you can reboot your Linux system.



You may also be interested in:

Was this article helpful?
Thank you for your feedback!