Friday, March 15, 2013

Local DNS cachin Linux


Linux Local DNS caching using dnsmasq

Source: http://www.webupd8.org/2009/12/faster-browsing-in-linux-with-local-dns.html


FASTER BROWSING IN LINUX WITH LOCAL DNS CACHE


A local DNS cache can help for faster browsing since you’re caching the DNS request instead of attempting that request multiple times. The internet speed will not get any faster, but the browsing speed will improve, because on each website there are usually quite a few DNS requests for which the local DNS cache will be used, bringing the query time to almost 0. You can find more info about DNS, on Wikipedia.

To see how fast your current domain name servers (DNS) are, open a terminal and paste this:
dig yahoo.com

(Or dig google.com or whatever domain)

You should see something like this:
; <<>> DiG 9.6.1-P1 <<>> yahoo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42045
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;yahoo.com.   IN A

;; ANSWER SECTION:
yahoo.com.  20142 IN A 69.147.114.224
yahoo.com.  20142 IN A 209.131.36.159
yahoo.com.  20142 IN A 209.191.93.53

;; Query time: 50 msec
;; SERVER: 208.67.220.220#53(208.67.220.220)
;; WHEN: Wed Dec  9 13:21:48 2009
;; MSG SIZE  rcvd: 75

Notice the "Query time" in bold. It's usually somewhere near 50 msec. (it depends on your domain name servers).

Run this one more time. If the query time decreases to less than 5 msec, it means your internet service provider DNS already uses some caching method and you do not need to follow this how-to. If the response time is almost the same and you are using a cable (broadband) internet connection, you can use this guide to cache the DNS for faster internet browsing.

Firstly, I would like to thank embraceubuntu for this how-to, I've just made it more newbie-friendly. So the credits go to him.

Before we get started, please note that there is an easier method on doing this (by only installing (well, actually you need to edit /etc/bind/named.conf.options with your ISP DNS) resolvconf and bind9: sudo apt-get install resolvconf bind9) but in my tests, using resolvconf and bind9, the first DNS query time was 200-300 msec (maybe it needs some tweaking but I couldn't figure out why there is such a big query time the first time) and then since it was cached, it went to 0. But the method I am going to explain will get an initial query time equal to your default DNS (~50 msec for me, as opposed to 200-300 msec which I got by using resolvconf and bind9).

Let's get started!

Manually configuring the local DNS cache



1. Install DNSMasq:
sudo apt-get install dnsmasq


2. Configure dnsmasq.conf

Press Alt + F2 and type:

gksu gedit /etc/dnsmasq.conf


Now search for "listen-address" (it's on line 90 on my Ubuntu Karmic installation), remove the "#" character in front of "listen-address" and add "127.0.0.1" after the "=" (all without the quotes). Basically, this is how the "listen-address" line should look like after editing it:
listen-address=127.0.0.1


(Optional) You can also edit the cache size if you want. Search for this in the same file: "#cache-size=150" (it's on line 432 on my Ubuntu Karmic installation), remove the "#" character in front of the line (this uncomments it) and change "150" with the size you want for you DNS cache. This is how the line should look after editing it:
cache-size=500

Obviously, "500" can be any number you want.

Don't forget to save the changes!

3. Edit dhclient.conf

Press Alt + F2 and type:
gksu gedit /etc/dhcp3/dhclient.conf

For newer Ubuntu versions (tested on Oneiric), dhclient.conf has moved, so use the following command instead:
gksu gedit /etc/dhcp/dhclient.conf


And modify the "prepend domain-name-servers" (it's on line 20 on my computer) to look like this:
prepend domain-name-servers 127.0.0.1;



4. Edit resolv.conf

Press Alt + F2 and paste this:
gksu gedit /etc/resolv.conf


Initially, this is how the resolv.conf file looks like:
nameserver ISP_DNS1
nameserver ISP_DNS2

Where ISP_DNS1 and ISP_DNS2 are your ISP domain name servers (or 8.8.4.4, etc if you are using Google DNS and so on).

Put this as the first line in your resolv.conf file:
nameserver 127.0.0.1

Which means this is how your resolv.conf file will look like:
nameserver 127.0.0.1
nameserver ISP_DNS1
nameserver ISP_DNS2

Again, ISP_DNS1 and ISP_DNS2 are your ISP domain name servers.

As an example, this is how my resolv.conf file looks like (using local DNS cache, a Google DNS and an OpenDNS DNS):
nameserver 127.0.0.1
nameserver 8.8.4.4
nameserver 208.67.220.220


4.1 If you are using a DSL connection, you need to make sure the ppp client will not overwrite your /etc/resolv.conf file. To do this, press Alt + F2, and paste this:
gksu gedit /etc/ppp/peers/provider

Search for "usepeerdns" and replace it with "#usepeerdns" (we used "#" to comment that line so it's ignored).

5. Restart your networking and dnsmasq:

-Networking:
sudo /etc/init.d/networking restart


-DNS:
sudo /etc/init.d/dnsmasq restart

Please note that you can use this last command at any time you want to restart your DNS cache (flush DNS, clear the cache - call it whatever you want) without restarting the computer.

6. Testing

To see the peformance improvement, open a terminal and type:
dig yahoo.com

The first time, it should be the same like in the beginning of the post (~50 msec. for me). Now type it again! You should see something like this:
dig yahoo.com

; <<>> DiG 9.6.1-P2 <<>> yahoo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57501
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;yahoo.com.   IN A

;; ANSWER SECTION:
yahoo.com.  20982 IN A 209.131.36.159
yahoo.com.  20982 IN A 69.147.114.224
yahoo.com.  20982 IN A 209.191.93.53

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Dec  9 14:43:41 2009
;; MSG SIZE  rcvd: 75


0 msec. query time, because the domains are now cached.


Note: Using the method above, the DNS cache will be cleared once you reboot your computer. For persistent DNS caching (on the hard disk), see this excellent how-to on Ubuntu Forums.

35 comments:

  1. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.

    Hadoop Training in Chennai

    ReplyDelete
  2. Great site for these post and i am seeing the most of contents have useful for my Carrier. Thanks to such a useful information.Any information are commands like to share him.

    Informatica training in chennai

    ReplyDelete
  3. Very nice post here and thanks for latest smartphone applications it .I always like and such a super colors of phone for these post.Excellent and very cool idea and great models and different kinds of the more information's.

    AWS Training in Chennai

    ReplyDelete
  4. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Housekeeping Services in Chennai

    ReplyDelete
  5. Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    Digital Marketing Company in Chennai

    ReplyDelete
  6. Your thinking toward the respective issue is awesome also the idea behind the blog is very interesting which would bring a new evolution in respective field. Keep update more information.
    Architectural Firms in Chennai
    Architects in Chennai

    ReplyDelete


  7. Its very useful to me. Wonderful blog.. Thanks for sharing informative Post.

    Installment loans
    Payday loans
    Title loans

    ReplyDelete
  8. Someone essentially lend a hand to make severely posts I would state. That is the very first time I frequented your website page and thus far? I surprised with the analysis you made to create this particular submit incredible. Fantastic job!
    Logistics Software
    Warehouse Management Software
    Logistics management software
    Human resources management software
    Fleet Management Software

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post..Interesting post! Thanks for writing it. What's wrong with this kind of post exactly? It follows your previous guideline for post length as well as clarity..
    Software Testing Training in Chennai
    SEO Training in Chennai
    Informatica Training in Chennai
    Digital Marketing Training in Chennai

    ReplyDelete
  11. I have really happy to these reading your post. This product control and maintenance of our health.The daily routine can assist you weight lose quickly and safely.My life is completely reworked once I followed this diet.I feeling nice concerning myself.

    Herbalife in Chennai
    Herbalife Nutrition Products
    Nutrition centers in Chennai
    Weight Loss in Chennai
    Weight Gain in Chennai

    ReplyDelete

  12. BIG DATA Technologies provides you with a state of the art software which combines modern GPU technology (Graphic Processing Units) with the best practices in today’s Big Data platforms, providing up to 100x faster insights from data.
    Bigdata Training in Chennai OMR

    ReplyDelete
  13. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's. 
    best rpa training in bangalore
    rpa training in bangalore | rpa course in bangalore
    RPA training in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  14. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  15. Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
    AWS Training in Bangalore

    AWS Training in pune

    ReplyDelete
  16. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  17. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Python Training in Electronic City

    ReplyDelete
  18. Thank you for allowing me to read it,It is very useful for me. And thanks for sharing the nice article, keep posting or updating new article!!
    Machine Learning Course

    ReplyDelete
  19. Attend The Analytics Course in Bangalore From ExcelR. Practical Analytics Course in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Analytics Course in Bangalore.
    ExcelR Analytics Course in Bangalore

    ReplyDelete
  20. I truly like your style of blogging. I added it to my preferred's blog webpage list and will return soon…

    PMP

    ReplyDelete
  21. I am stunned by the information that you have on this blog. It shows how well you fathom this subject.

    hrdf claimable courses

    ReplyDelete
  22. This is quite charming post you shared, I like the post, an obligation of appreciation is all together for sharing..
    360DigiTMG data analytics course malaysia

    ReplyDelete
  23. As always your articles do inspire me. Every single detail you have posted was great. ExcelR Machine Learning Course

    ReplyDelete
  24. Stunning! Such an astonishing and supportive post this is. I incredibly love it. It's so acceptable thus wonderful. I am simply astounded.
    difference between analysis and analytics

    ReplyDelete
  25. I am another customer of this site so here I saw various articles and posts posted by this site,I curious more energy for some of them trust you will give more information further.
    training provider in malaysia

    ReplyDelete
  26. I really like your writing style, great date, thank you for posting.
    data scientist course in noida

    ReplyDelete
  27. Started out in 2012, Data Science Central is one of the industry’s leading and fastest growing Internet
    community for data practitioners. Whether it is data science or machine learning or deep learning or
    big data, Data Science Central is a one-stop shop that covers a wide range of data science topics that
    includes technology, tools, data visualisation, code, and job opportunities. Also, many industry experts
    contribute to the community forum for discussion or questions.

    DATASCIENCETraining in OMR Chennai

    ReplyDelete
  28. Great post. keep sharing such a worthy information.
    Google Ads Online Course

    ReplyDelete
  29. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

    IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

    corporate training in chennai corporate training in chennai

    corporate training companies in india corporate training companies in india

    corporate training companies in chennai corporate training companies in chennai

    I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai Project Centers in Chennai

    ReplyDelete
  30. Jump into an immense learning experience of DevOps Training in Chennai from Infycle Technologies, the best software training institute in Chennai. Also, an excellent place to get other demanded technical courses like Data Science, Big Data, BlockChain, Full Stack and Mean Stack Development, Digital Marketing with Graphic Design, etc. with 100+ Live Practical Sessions. For more queries for your career, call us on +91-7504633633, +91-7502633633.

    ReplyDelete
  31. I curious more interest in some of them hope you will give more information on this topics in your next articles.
    data science institutes in hyderabad

    ReplyDelete

I would be glad to know if this post helped you.