Using netsh to set multiple DNS server addresses in Windows

During my recent two days of torment caused by a flaky Java application, I had to change the preferred and alternate DNS server entries for one of my network cards. Ordinarily that would be simple, but with an unresponsive Explorer interface refusing to open any network connection dialogs I needed to do it from the command line.

Enter the network shell (netsh) – a fantastic command line utility that has sneaked into recent versions of Windows and seems to have more and more functionality added with each new release.

After entering the netsh shell, interface ip got me to the TCP/IP interface settings; then show dns gave me the details of the current DNS servers; set dns "Local Area Connection" ipaddress allowed me to set the preferred DNS server and add dns "Local Area Connection" ipaddress index=2 set the alternate DNS server (that was the difficult one to work out – I had tried to set dns with a list of IP addresses but that does not work!); finally, exit the network shell and type ipconfig -all to check settings the normal way.

I love the command prompt!

Comments

30 responses to “Using netsh to set multiple DNS server addresses in Windows”

  1. chuck avatar

    Mark,

    Thanks for the post – it was definitely helpful for me :)

    I found your blog by typing “netsh multiple dns” in good old google and yours popped up as the top entry and had the information I needed. Thanks again!

    Chuck

  2. Mark avatar

    Chuck, Glad to hear you found this useful! You can also find something similar in one of the comments on the Windows IT Pro article “cool things to do with netsh“.

  3. Lester avatar
    Lester

    I was looking on the internet on how to add secondary DNS. I didn’t find this info on Microsoft’s site. I’m sure its there somewhere. Anyways, I found your website and was successful. Thanks for posting it.

  4. Gregory Fenton avatar
    Gregory Fenton

    Thanks a lot, I have a network card that does not appear in “Network Connections”.. the info here allowed me to add a custom DNS server for opennic (http://www.opennic.unrated.net or the much cooler http://www.opennic.glue) using the command

    netsh add dns 83.217.93.246 index=2

    I’d suggest you try it – there’s a whole new world out there – never mind .com, how about your own website ending in .indy, .geek, .parody and many many more.. http://www.opennic.unrated.net/tlds.html

    It may sound like an advert, but it’s not.. check them out :)

    And thsnks again for the netsh help

  5. Anonymous avatar
    Anonymous

    Hello,

    the only way it worked on my Windows XP prof. was to set alternate dns server first and then to add primary server with index=1 :

    netsh interface ip set dns “Local Area Connection” static (secondaryserveraddress)

    netsh interface ip add dns “Local Area Connection” (primaryserveraddress) index=1

    loobrush

  6. Anonymous avatar
    Anonymous

    On Windows XP to set a primary and static DNS server use…

    netsh interface ip set dns local static xxx.xxx.xxx.xxx
    netsh interface ip add dns local xxx.xxx.xxx.xxx

  7. Sanjay M avatar

    I alternate between DNS and static IP between my home and office, and this entry was really cool, thanks a lot Mark! I’d written 2 batch files which did this, but didn’t know about the index=2 part for alternate dns and found it here.

    Just for completion, here’s how to set up dhcp:

    netsh interface ip set dns “Local Area Connection” dhcp

  8. Anonymous avatar
    Anonymous

    Never managed to get this working, so not sure if the functionality has been removed by an update or something, but the best way to add additional DNS (or WINS) addresses is to first set your primary address:

    netsh interface ip set dns name=”Local Area Connection” static IPADDRESS

    then for every additional DNS server:

    netsh interface ip add dns name=”Local Area Connection” IPADDRESS

  9. Anonymous avatar
    Anonymous

    For simplified chinese PCs, the correct command is usually:

    set dns “本地连接” static 192.168.60.2

    (本地连接 = Local Area Connection)

  10. Arty avatar
    Arty

    Awesome. Exactly what I was trying to do. You have saved me a lot of hassle.

    Thanks!

  11. mak avatar
    mak

    thanks a million helped me out now i don hav to manually change my setting
    I wrote batch files to make it simpler
    Thanks again

  12. […] Other links that may be useful include the Windows command line reference and my own post on using netsh to set multiple DNS server addresses. […]

  13. cw1972 avatar

    Thank you for this, the index=2 was just what I needed, the help netsh gives does not make it obvious that this is for secondary dns.

    I too found your blog by typing “add secondary dns using netsh” into google.

    Many thanks :)

  14. Deb avatar
    Deb

    for Alternate DNS we need to mention
    netsh add dns 83.217.93.246 index=2
    This really works.

  15.  avatar
    Anonymous

    Thnank you very much for sharing this info..

  16. Larry Gurley avatar
    Larry Gurley

    After running netsh interface dump I looked for the line that began with set dns and add dns and prepended netsh interface ip. Looked like this. I’m sure this will wrap but it was two commands.

    netsh interface ip set dns name=”Local Area Connection” source=static addr=10.69.1.50 register=PRIMARY

    netsh interface ip add dns name=”Local Area Connection” addr=10.69.1.51 index=2

  17. Sergey Jamharyam avatar

    I tried to set 3 dns with netsh, how can help me? When I put 3 dns settings no one couldn’t set.
    I’ll put answer on http://www.infoexpanse.com for other finders as me:)

  18. Axxel avatar
    Axxel

    Many THX!!! Unbelivable that the online-help don’t mention this :-(

  19. Anders Olsson avatar
    Anders Olsson

    Thanks for sharing, man!

  20. boulder avatar
    boulder

    In windows 2003 Sp2 the working command lines are:
    netsh interface ip set dns name=”sat” source=static 10.0.0.1
    netsh interface ip add dns name=”sat” 10.0.0.2
    netsh interface ip add dns name=”sat” 10.0.0.3

  21. Overcast avatar
    Overcast

    Like wise :) Same Search – index=2. It cut many minutes off a time-critical server migration. Thanks!

    Google search for “netsh int ip set dns secondary” did it.

  22. aaa avatar
    aaa

    thanks a lot for this tip

  23. Jitesh avatar
    Jitesh

    Grt… Good Work

  24. frogola avatar

    Thanks man, still useful after all these years!

  25. Mart avatar
    Mart

    Yep. One bit of effort on your part to create the blog has helped an awful lot of people over a long time. Thanks

  26. Mark77 avatar
    Mark77

    To get this to work across all systems (w2k8, w2k3, r2 and x64) I had to run my netsh command like the following (we rename our adapters Primary-VLANXXX and Backup):

    Hint: I used psexec to push this out in a batch file and run it with system privleges

    netsh interface ip delete dns name=”Primary” addr=all (Purges existing DNS)
    netsh interface ip add dns name=”Primary” 10.x.x.1 index=1 (Adds Primary DNS)
    netsh interface ip add dns name=”Primary” 10.x.x.2 index=2 (Adds Secondard DNS)
    ipconfig /flushdns (flush local DNS cache)
    ipconfig /registerdns (register ip on new DNS)
    nbtstat -RR (refresh netbios)

  27. Mick avatar
    Mick

    Great stuff here that’s helped a lot.

    One thing we don’t have is a standard name for the network interface that we need to update. Is there any way to have this apply without having to specifically define the network interface? I guess this means, is there a way to identify all interfaces that are configured with existing settings and update them? At the moment, the manualy process is to run “netsh interface ip show interfaces” and then update the script below:

    netsh interface ip set dns “Local Area Connection” static 10.0.0.1
    netsh interface ip add dns “Local Area Connection” 10.0.0.2 index=2
    netsh interface ip set wins “Local Area Connection” static 10.0.0.1
    netsh interface ip add wins “Local Area Connection” 10.0.0.2 index=2
    ipconfig /flushdns
    ipconfig /registerdns
    nbtstat -RR

    Any thoughts?

  28. Paul avatar
    Paul

    Still working in 2024 with Windows 11, used to add a 3rd DNS server using index=3

  29. Mark Wilson avatar

    Yay! Great to hear the old tricks still work

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.