Hey there! Do you find yourself constantly waiting for pages to load on your Ubuntu system? Do websites take forever to open, making you want to pull your hair out in frustration? I totally get it – slow internet is the worst!
But don‘t worry, I‘ve got your back. In this guide, I‘ll show you how to seriously improve your web browsing speeds by setting up DNS caching with dnsmasq. By caching DNS locally, you can slash those annoying loading delays and make your internet fly!
Ready to wave goodbye to sluggish internet on Ubuntu? Let‘s get started…this is gonna be awesome!
Why DNS Caching is a Game-Changer for Web Performance
To understand how DNS caching helps speed up your web access, we first need to quickly cover how the DNS system works.
See, browsers don‘t actually use domain names when connecting to websites. They need the website‘s underlying IP address, like 172.217.14.206 for google.com.
So each time you attempt to access a site, a DNS lookup happens in the background to resolve the domain to an IP. This query gets sent to a DNS server provided by your ISP.
The problem is these DNS lookups can take 100ms or more to complete. For a site with lots of assets, you could be waiting several seconds in total for the DNS step to finish before the page even begins loading!
Just check out this DNS lookup time for google.com from a test system:
dig google.com
;; Query time: 120 msec
120 milliseconds…and that‘s just to resolve ONE domain!
Research shows the average web page now requires around 90 DNS queries to fully load. So with an average 100ms per lookup, DNS alone could account for 9000ms (9 seconds) of delay!
No wonder pages feel slow to load sometimes!
This is where DNS caching comes to the rescue…
With DNS caching enabled, a local DNS server keeps a record of all the lookups it makes over time. Often cached for a few hours or longer.
So when you request google.com again, it already knows the IP address from its cache. No need to ask the external servers!
Instead of a 120ms lookup, a DNS cache typically returns results under 5ms. Pages start rendering almost instantly rather than making you twiddle your thumbs waiting on DNS.
According to Cloudflare, enabling DNS caching can speed up web browsing by 2x to 10x for most users!
Now that‘s an upgrade I think we can all get excited about! π
Meet dnsmasq – A Lightning Fast DNS Caching Server
There are a few different DNS caching software options out there, but one of the most popular for Linux is the dnsmasq package.
dnsmasq is lightweight, easy to configure, and seamlessly integrates into your existing network. It runs locally as a DNS forwarding server, where it can intercept requests and return cached responses blazingly fast.
Internally, dnsmasq uses an in-memory cache for super-speedy lookups. It‘s designed to be highly optimized for performance on embedded devices like routers and firewalls.
And the best part? dnsmasq is 100% free and open source.
Notable users of dnsmasq include Android, Chrome OS, PlayStation 4, Steam Link, and more. If it‘s good enough for them, it‘s probably good enough for our purposes too!
Some key advantages of dnsmasq:
- Simple to install, just a few minutes
- Minimal memory and CPU footprint
- Caches DNS queries from any local app/device
- Reduces bandwidth usage by preventing redundant lookups
- Adds crucial redundancy if external DNS fails
- Seamlessly improves speeds without any application changes
- Extensive configuration options for power users
With dnsmasq handling our DNS resolution, we can kiss sluggish page loads goodbye! Now let‘s get it setup…
Step 1 – Installing dnsmasq on Ubuntu
Thankfully, the dnsmasq package is included in Ubuntu‘s main software repositories. That makes installing it a breeze.
Simply open your terminal and run:
sudo apt update
sudo apt install dnsmasq
Enter your account password when prompted. After a minute or two, dnsmasq will be installed and started automatically.
Let‘s verify it is up and running with systemctl:
systemctl status dnsmasq
You should see output confirming dnsmasq is active and running:
β dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-04-01 11:18:02 UTC; 3min 37s ago
Docs: man:dnsmasq(8)
Main PID: 47701 (dnsmasq)
Tasks: 1 (limit: 4561)
Memory: 1.3M
CPU: 44ms
CGroup: /system.slice/dnsmasq.service
ββ47701 /usr/sbin/dnsmasq -x /var/run/dnsmasq/dnsmasq.pid -u dnsmasq -7 --local-service --trust-anchor=.,19036,8,2,49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5
Apr 01 11:18:02 ubuntu systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
Apr 01 11:18:02 ubuntu systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.
Awesome! Our dnsmasq server is now installed and running on Ubuntu. Time for some configuration…
Step 2 – Configuring dnsmasq to Enable DNS Caching
The dnsmasq config file is located at /etc/dnsmasq.conf. We‘ll edit this file to set up dnsmasq as a simple caching nameserver.
Open the config file in your favorite editor (I‘ll use nano):
sudo nano /etc/dnsmasq.conf
Let‘s uncomment and set some key parameters:
# Listen on the local loopback interface only
listen-address=127.0.0.1
# Set the DNS cache size
cache-size=1500
# Log queries for debugging
log-queries
# Bind dnsmasq to port 53
port=53
This binds dnsmasq to the standard DNS port 53 on localhost. It also enables query logging for troubleshooting, and sets the DNS cache size to 1500 entries (adjust as needed).
The full config file should look like this:
# Listen on the local loopback interface only
listen-address=127.0.0.1
# Set the DNS cache size
cache-size=1500
# Log queries for debugging
log-queries
# Bind dnsmasq to port 53
port=53
Once finished, save and close the file.
These are the core settings we need to start caching DNS queries locally. Pretty simple right?
You can add other configuration options later for more customization. The dnsmasq man pages describe all available parameters.
Now we just need to set Ubuntu to use our local dnsmasq server…
Step 3 – Configure Ubuntu‘s DNS Settings
By default, Ubuntu is setup to use your router or ISP‘s external DNS servers.
We need to point it to the local dnsmasq server instead.
Edit the /etc/resolv.conf file:
sudo nano /etc/resolv.conf
Add your dnsmasq server‘s IP address on the first line. This is usually 127.0.0.1 referring to localhost:
nameserver 127.0.0.1
# External fallback DNS servers (optional)
nameserver 1.1.1.1
nameserver 8.8.8.8
Setting 127.0.0.1 as the first nameserver entry ensures Ubuntu queries the dnsmasq service on your machine before trying external servers.
Save changes and exit the file after finishing editing.
Almost there, just one more step…
Step 4 – Restart dnsmasq to Load New Config
For the dnsmasq configuration changes to take effect, we need to restart the service:
sudo systemctl restart dnsmasq
That‘s it…our dnsmasq DNS caching server is now online and ready to speed up all your web browsing from Ubuntu!
Verifying DNS Caching Works Like a Charm!
Let‘s validate everything is working as expected by using the dig debugging tool.
Try looking up a domain:
dig google.com
;; Query time: 15 msec
Notice the "Query time" at the bottom – 15ms for the initial lookup.
Now run dig again:
dig google.com
;; Query time: 1 msec
Aha! This time around it returned from cache almost instantly in 1ms instead of 15ms.
So dnsmasq successfully saved the DNS record in its cache and served the subsequent request 10x faster than the external DNS. Booyah!
We can also tail the dnsmasq log in real-time to watch DNS queries in action:
tail -f /var/log/dnsmasq.log
Feb 14 17:49:35 dnsmasq[2418]: query[A] google.com from 127.0.0.1
Feb 14 17:49:35 dnsmasq[2418]: forwarded google.com to 1.1.1.1
Feb 14 17:49:35 dnsmasq[2418]: reply google.com is 172.217.14.206
Feb 14 17:49:37 dnsmasq[2418]: reply google.com is 172.217.14.206
The initial dig lookup was forwarded to the external 1.1.1.1 server. But subsequent queries are answered directly from cache!
Tuning dnsmasq‘s Performance Like a Pro
dnsmasq works great out of the box, but we can optimize things further:
1. Increase the cache size
The default cache size is only 150 entries. Bump this up to something larger like 5000 entries. This allows more DNS records to be cached for faster access.
2. Lower the cache TTL
TTL determines how long a DNS record stays cached. The default is typically 24 hours. Lowering to 1 hour forces records to expire sooner.
3. Enable DNSSEC validation
DNSSEC adds enhanced security but has a slight performance hit. Worth considering if security is critical.
4. Add additional config options
Refer to the dnsmasq docs for all available options. For example, you can add domain blacklists, setup DHCP, and more.
Start with the basics, then tweak the advanced settings over time for the best performance.
The key is finding the right balance for your specific environment and browsing habits. Don‘t be afraid to experiment!
Wrap Up
Phew, we covered a lot of ground here! Here‘s a quick recap of all we learned:
- DNS resolution is often the slowest part of loading websites
- Caching DNS queries locally with dnsmasq dramatically speeds up web browsing by serving from memory
- dnsmasq is lightweight, fast, and easy to setup on Ubuntu
- Configure dnsmasq, update Ubuntu‘s resolv.conf, restart dnsmasq, and away you go!
- Validation with dig shows cached results return 10-15x faster than uncached external queries
- Further optimization is possible by tuning dnsmasq‘s advanced options
Whew, I don‘t know about you, but I‘m excited to see the speed boost after getting dnsmasq enabled!
No more twiddling thumbs waiting on DNS lookups to complete. Web pages will now load with blazing speed thanks to the power of DNS caching.
Hopefully this guide gave you a good understanding of how DNS caching works and how to get it setup it on Ubuntu. Just shout if you have any other questions! I‘m always happy to help a friend out.
Now get out there and browse the web at lightning speed! π