3

How do I switch DNS by command-line?

I mean like, changing between Google DNS or OpenDNS and whatever the system default is.

4
  • possible duplicate of What is the proper way to change the DNS IP?
    – Alvar
    Nov 13, 2013 at 11:34
  • 1
    IF you look like 3 answers down you will find a CLI version on how to switch DNS server.
    – Alvar
    Nov 13, 2013 at 11:34
  • 2
    From which only the answer given by Azendale is working since 12.04. None of the answers give us a safe method to switch DNS servers from one to the other. Therefore I believe this question here is not a dupe.
    – Takkat
    Nov 13, 2013 at 11:42
  • @Takkat Maybe you mean switch back and forth between DNS servers? To "switch DNS servers from one to the other" once is not different from "change the DNS IP". There's no indication that the asker would want to switch back, so the other question seems sufficient.
    – Steven K
    Nov 16, 2013 at 21:06

1 Answer 1

1

Here's a little script to achieve that, replace the IPs with your DNS servers and add this script to your path

#!/bin/bash
if [ $1 == "1" ]; then
echo "nameserver 10.1.0.1" > /etc/resolv.conf 
fi
if [ $1 == "2" ]; then
echo "nameserver 10.1.0.2" > /etc/resolv.conf
fi
1
  • Would you mind providing instructions on 'adding this script to your path' and an example usage of the script for clarity? This is great, thanks!
    – earthmeLon
    May 3, 2016 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .