KISS

Be Among the 20% of the Best!

Faster DNS on OS X

| comments

There is one operation that’s completed very often when programs connect to servers on the internet — that’s resolving DNS names (finding out the server’s IP address by its name). Typically, you use a DNS server of your ISP. It’s relatively close to your network, so it may be fast. This short post is about setting up a local caching DNS server on OS X for even faster DNS responses.

Setting up dnsmasq

There is the canonical BIND DNS server, but I’ll use the lighter dnsmasq, which uses only ~470 KB of memory on my mac. Installing it on OS X is very easy:

1
$ brew install dnsmasq

Now, as suggested in the output caveats, copy the config file:

1
$ cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf

and update the #listen-address= line to listen-addres=127.0.0.1 for dnsmasq to listen on localhost only. Then launch the service with

1
2
3
4
5
$ sudo brew services start dnsmasq
==> Tapping homebrew/services
[…skipped…]
Tapped 0 formulae (32 files, 46K)
==> Successfully started `dnsmasq` (label: homebrew.mxcl.dnsmasq)

The last setup step is to use our new and shiny DNS server. First, you can check the current DNS servers:

1
2
$ networksetup -getdnsservers Wi-Fi
There aren't any DNS Servers set on Wi-Fi.

Here it means we’re using whatever servers provided by the Wi-Fi router. To check those, you can cat /etc/resolv.conf. Now set the DNS server:

1
$ sudo networksetup -setdnsservers Wi-Fi '127.0.0.1' 'secondary DNS'

NOTE: since dnsmasq doesn’t know any addresses at the beginning, it needs to know whom to ask => you need to specify a secondary DNS. There may be another way in dnsmasq.conf.

Testing the DNS

Let’s see:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ dig github.com

; <<>> DiG 9.8.3-P1 <<>> github.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23954
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

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

;; ANSWER SECTION:
github.com.             89      IN      A       192.30.252.122

;; Query time: 79 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Jun 22 21:12:49 2016
;; MSG SIZE  rcvd: 44

$ dig github.com


;; ANSWER SECTION:
github.com.             85      IN      A       192.30.252.122

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Jun 22 21:12:53 2016
;; MSG SIZE  rcvd: 44

Awesome! IMHO the pages are loaded noticeably faster in a browser. To get more quantifiable results, you can use namebench:

1
$ brew cask install namebench

Comments