How to change DNS servers?

I imagine you already found a way to do this, but for posterity and for anyone else who finds this thread looking specifically for a CLI / terminal method to control DNS only to come away without an answer (like I did for use in a script) this is the method I have settled on:

  1. nmcli con to list your connections and pick out the NAME value of the one you want to change DNS settings for
  2. (optional) nmcli conn modify "NAME" ipv4.ignore-auto-dns yes to bypass the default DNS resolvers specified by your router / gateway
  3. (optional) nmcli conn show "NAME" | grep dns before and after the next step is handy to check it’s worked before actually reconnecting to apply the new DNS resolvers, just in case you lose Internet connectivity by applying “wrong” settings and struggle to research a way to fix it
  4. nmcli conn modify "NAME" ipv4.dns "94.140.14.14 94.140.15.15" to set your preferred DNS resolvers
  5. nmcli con down "NAME" followed by nmcli con up "NAME" to reconnect with your specified DNS resolvers

You can use nmcli conn modify "NAME" ipv4.dns "" (empty quotes after ipv4.dns) followed by the down/up commands in step 5 to remove manually set DNS resolvers.

7 Likes