Linux / UNIX set the DNS from the command line

Posted on in Categories , , , , , , , , last updated December 20, 2007

Q. I just got Linux installed on my system. I’m able to connect to internet via IP address only. I’m not able to find out information about setting up new DNS sever ip under Linux. How do I configure primary and secondary DNS via terminal / shell prompt?

A. Under Linux / UNIX / BSD operating system, you need to edit the /etc/resolv.conf file and add the line:

nameserver {IP-OF-THE-DNS-1}
nameserver {IP-OF-THEISP-DNS-SERVER-2}

Login as the root, enter:
# vi /etc/resolv.conf
OR
$ sudo vi /etc/resolv.conf
Modify or enter nameserver as follows:
nameserver 208.67.222.222
nameserver 208.67.220.220

Save and close the file. To test DNS configuration type any one of the following command:
$ host google.com
$ dig google.com
$ ping google.com
$ nslookup your-domain.com

Output:

google.com has address 72.14.207.99
google.com has address 64.233.187.99
google.com has address 64.233.167.99
google.com mail is handled by 10 smtp4.google.com.
google.com mail is handled by 10 smtp1.google.com.
google.com mail is handled by 10 smtp2.google.com.
google.com mail is handled by 10 smtp3.google.com.

If you see valid output such as actual IP address or able to ping to remote server via hostname, it means that the dns is working for you. Also make sure you have valid default gateway setup, if you see the time out error.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

17 comment

  1. Ping is a tool to send ICMP echo requests to machines, not to check if resolving works. nslookup, dig and host can be used to check resolving, as you perfectly describe in this article.

      1. Not exactly so @musolff92. If you are blocking ICMP’s then the ping will not hit you back, but you still be resolving names, or if you don’t specify a default gateway, pings will fail yet end might be working. The ping is good to test connectivity issues before this step.

    1. From resolv.conf:

      # No nameservers found; try putting DNS servers into your
      # ifcfg files in /etc/sysconfig/network-scripts like so:
      #
      # DNS1=xxx.xxx.xxx.xxx
      # DNS2=xxx.xxx.xxx.xxx
      # DOMAIN=lab.foo.com bar.foo.com

  2. why do you edit resolve.conf directly?
    Boot, and see if your changes are still there.

    This file gets overwritten.
    You need to use /etc/resolvconf/resolv.conf.d/head for this or set dns in /etc/network/interfaces
    You can even use dhcpclient conf file for this

Leave a Comment