Linux / UNIX set the DNS from the command line

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
$ sudo vi /etc/resolv.conf
Modify or enter nameserver as follows:

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

Output: has address has address has address mail is handled by 10 mail is handled by 10 mail is handled by 10 mail is handled by 10

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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 17 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
17 comments… add one
  • me Dec 19, 2007 @ 21:01

    you’re easily sliding to the point where your tutorials will be as complex as ‘how to start your computer? press the power button!’


  • Ash Dec 19, 2007 @ 21:08


  • Robert de Bock Dec 20, 2007 @ 10:49

    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.

    • musolff92 Jul 2, 2011 @ 3:25

      well that’s true, but it has to resolve the dns name before it can continue the ping. So ping works too.

      • marazzi. Feb 6, 2016 @ 20:55

        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.

  • user Dec 20, 2007 @ 16:33


    Non-authoritative answer: canonical name =

  • Phil Jan 22, 2008 @ 13:44

    Using nslookup you can query different name servers.

    nslookup XXX.XXX.XXX.XXX

    where XXX.XXX.XXX.XXX is the ip adress or name of the name server you are trying to query.

    Doing :
    nslookup localhost
    will query your local dns server if you have one.

  • Sajain Geevar Jul 8, 2008 @ 8:33

    How can I specify something like for site say ‘’ go to this ip say ‘’?

  • 🐧 nixCraft Jul 8, 2008 @ 19:17

    You need to set named or bind9 software to point to specific IP.

  • FM Sep 20, 2008 @ 4:51


  • Ashis Sep 4, 2011 @ 14:14

    i m very much crazy abt Linux

  • FuzzyGhost Jun 19, 2012 @ 14:53

    How does one set this permanently though? /etc/resolv.conf will erase changes upon reboot/shutdown.

    • Andy Oct 8, 2012 @ 18:20

      From resolv.conf:

      # No nameservers found; try putting DNS servers into your
      # ifcfg files in /etc/sysconfig/network-scripts like so:

  • Dr Chaos Oct 4, 2012 @ 1:42

    THANK YOU!!!!!!!
    fixed my network after visiting 8 sites. This one did it for me :)
    Ubuntu 12 server, with KDE installed on top.

  • Sellmy Mar 13, 2013 @ 0:53

    Good link.Luckily stumbled after visiting 1st 3 results on google search. Glad i checked it out .

  • Adi Kwok Mar 29, 2014 @ 7:20

    Finally, i found this clear answer,

    Thank you, nixcraft team

  • asd Jan 23, 2015 @ 11:26

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum