Page 1 of 1

Arch: Adblocking with dnsmasq and hostsblock

Posted: 29 Dec 2015, 04:56
by stasheck
Another thing "I just did".

1. Install hostsblock from AUR
(to be described in more detail, if someone needs that).

2. 'systemctl start hostsblock' (it's one-shot service, you need to re-run it every time you want new file generated).

3. Marvel at how slow your name resolution has become after /etc/hosts is now 15 MB in size.

4. Special souce: create /usr/local/bin/hosts_to_tmpfs.sh:

Code: Select all

#!/bin/bash

mount -t tmpfs -o size=20M,mode=700 tmpfs /mnt/tmpfs
systemctl start hostsblock
cp /etc/hosts /mnt/tmpfs/hosts
mv /etc/hosts /etc/hosts.`/usr/bin/date +\%Y-\%m-\%d`
ln -s /mnt/tmpfs/hosts /etc/hosts
chmod 755 /mnt/tmpfs
5. Reload dnsmasq: systemctl restart dnsmasq

6. Resolution snappy again, 10s of ms instead of few 100s.


So what I'm doing is a copy of /etc/hosts in RAM (small RAM disk, 20 MB in size). Keep in mind that this is "quick and dirty" script, no error checking, no nothing. I'm OK with that since I'm running it manually each time, and in worst case I lose name resolution, but not access to shell, but you were warned.