DNSMasq Local Network

From DD-WRT Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 02:50, 13 February 2007 (edit)
AbraXxl (Talk | contribs)

← Previous diff
Revision as of 01:37, 15 February 2007 (edit) (undo)
Whiteboy (Talk | contribs)

Next diff →
Line 167: Line 167:
[[Category:English documentation]] [[Category:English documentation]]
 +[[Category:Advanced tutorials]]

Revision as of 01:37, 15 February 2007

So far I've learned of 3 ways to configure the DD-WRT, so that it will perform DNS for your local network. I don't claim to know which way is preferred by the developer(s) of the DD-WRT.


1) Editing the /etc/hosts file

This is probably the first place one attempts getting this to work if you have a *NIX background. And it does work here with a **caveat** , once you've editted your /etc/hosts, you need to restart/reload DNSMasq. So for example if you populate your /etc/hosts with the startup_rc script like:

'echo    somename' >> /etc/hosts

from the startup script. You also need to put:

killall dnsmasq 
dnsmasq --conf-file /tmp/dnsmasq.conf 

in your startup script. So your script ends up looking like:

'echo    somename' >> /etc/hosts

killall dnsmasq 
dnsmasq --conf-file /tmp/dnsmasq.conf 

Lastly, if you want the router to append your domain on DNS request, put "expand-hosts" into the DNSMasq other options

2) Additional DNS Options

Administration > Management

NOTE: Before v23 SP1 Final it was Administration->Services->DNSMasq

You can enter DNS info about machines in this format:


For example:


Or fully qualified:


Then things I don't like about this way are:

1) You can't have two (or more) names for the same machine in the same entry like in /etc/hosts. So instead you are forced to do:


2) I haven't been able to figure out how to have it append the domain name, so you end up needing both the short name and the fully qualified name (like above)

The official DNSMasq documentation is at http://www.thekelleys.org.uk/dnsmasq/doc.html. There is a sample configuration file at http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq.conf.example.

-comment- for v23, the trailing slash, as seen in the examples, caused it not to work. As soon as it was removed, the errors seen when re-starting dnsmasq (in ssh) went away and the service started resolving hostnames

3) Creating static leases

Static leases from Administration->Services->DHCP Server

You just hit "add", and type them in. You need to "save" settings at the bottom of the page (Duh). The thing I don't like about this method is:

1) If you do NOT want to tie MAC addresses with IPs then you have to use a fake MAC address on this form, because the info doesn't save without a MAC address

2) If you need to enter many of these, it get tedious

4) dnsmasq wrapper

This solution works for me, as i like to have a larger /etc/hosts file ant therefor the startup command solutions is not ideal. By the DD-WRT overwrites the host file, when you static leases. What it does:

1) The Script is a wrapper around dnsmasq with a little help of mount -o bind.

2) Once it is hooked and DD-WRT wants to restart dnsmasq the script is executed instead, and adds some lines to the generated /etc/hosts afterwards it executes the real dnsmasq. Finally it reinstalls the wrapper.

You have to place the script somewhere and put it in to list of commands executed at bootup. Then adjust the variables at the setup section. Let dnsmasq and tmp untouched. The hostfile is an file in /etc/hosts format. But the generation of /etc/hosts could be adjusted as you like. like. Tinker a bit in the Section "$0" = "$dnsmasq".

To do the magic i added dumb versions of readlink and pgrep to the script. They are not fast and could by buggy.

#- setup

#- dirty version of readlink for dd-wrt
readlink() {
        if [ -n "$1" ]; then
                 ls -l "$1" | sed 's/^.* -> \(.*\)$/\1/g' | grep -v lrwxrwxrwx

#- dirty version pgrep; -f -v not implemented
pgrep() {
        local i
        local exe
        local pid
        for i in /proc/*/exe; do
                exe="`readlink "$i"`"
                if [ "$exe" == "$1" ]; then
                        echo -n "$pid "
        if [ -n "$pid" ]; then

#- debug
        #set -x

if [ "$0" = "$dnsmasq" ]; then
        if [ -f "$hostfile" ]; then
                cat "$hostfile" >> /etc/hosts

cat > "$tmp" <<EOF
                while umount "$dnsmasq"; do :; done
                $0 $@ &
                sleep 1
                while umount "$dnsmasq"; do :; done
                #mount -o bind "$0" "$dnsmasq"
                mount -o bind "$wrapper" "$dnsmasq"
                rm -f "$tmp"
        exec $SHELL "$tmp"
        echo "$0: wrapper installed."
        while umount "$dnsmasq"; do :; done
        #mount -o bind "$0" "$dnsmasq"
        mount -o bind "$wrapper" "$dnsmasq"
        pid="`pgrep "$dnsmasq"`"        #- pid von dnsmasq feststellen
        if [ -n "$pid" ]; then
                pid="${pid%% *}"        #- nehme erstes elt aus pid
                cmdline="`tr '\000' ' ' < /proc/$pid/cmdline`"
                kill $pid
                exec $cmdline