Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
DNS cache with dnsmasq and dhcpcd supplied DNS
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
alligator421
Apprentice
Apprentice


Joined: 30 Jul 2003
Posts: 191

PostPosted: Tue Oct 12, 2004 8:59 am    Post subject: DNS cache with dnsmasq and dhcpcd supplied DNS Reply with quote

Who might get interested ?
People who get their nameservers by dhcp (with dhcpcd) and wanting to speed up DNS lookup.
Summary
By default, resolver is resolving IP adress by locating DNS servers in /etc/resolv.conf and forwarding request to it.
Problem is that dhcp client is overwriting this file with supplied DNS from internet provider.
So we want to stop the overwriting of /etc/resolv.conf.
Put in it our DNS local adress.
Launch dns cache service, dnsmasq and forwarding unknown domain name lookups to DNS of provider.
Steps
Code:

emerge dnsmaq


Stop dhcpcd from overwriting resolver file by putting the -R option.
Code:
 
~ $ cat /etc/conf.d/net|egrep -v ^#|egrep  -v ^$
iface_eth0="dhcp"
dhcpcd_eth0="-n -D -R"
~ $


Put local adress (127.0.0.1) in resolver file.
Code:

~ $ cat /etc/resolv.conf
nameserver 127.0.0.1
~ $


Edit the dnsmasq start-up script to get dns from provider and put them in a new file /etc/resolv.conf.dnsmasq
Your mileage may vary from /var/lib/dhcpc, it is set as <ConfigDir> in dhcpcd, man dhcpcd
Change start function of script /etc/init.d/dnsmaq
Note: this step is not really mandatory but useful if your provider changes their DNS without notice :)
Code:

 start() {
        ebegin "Starting dnsmasq"
       
   if [ -s /var/lib/dhcpc/dhcpcd-eth0.info ];then
        PRIMARY=`cat /var/lib/dhcpc/dhcpcd-eth0.info|egrep ^DNS=|cut -b 5-|egrep -o "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"`
        SECONDARY=`cat /var/lib/dhcpc/dhcpcd-eth0.info|egrep ^DNS=|cut -b 5-|egrep -o ",[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"|cut -b 2-`
        printf "nameserver ${PRIMARY}\nnameserver ${SECONDARY}\n" > /etc/resolv.conf.dnsmasq
        fi

        /usr/sbin/dnsmasq -x /var/run/dnsmasq.pid ${DNSMASQ_OPTS}
        eend $?
}


Configure dnsmasq.
Especially, nameservers from where to forward requests when u got a miss from dnsmasq (now on /etc/resolv.conf.dnsmasq)
Set also dnsmasq to only listen to local request.
Code:

~ $ cat /etc/dnsmasq.conf |egrep -v ^#|egrep  -v ^$
domain-needed
bogus-priv
filterwin2k
resolv-file=/etc/resolv.conf.dnsmasq
user=nobody
group=nobody
interface=lo
~ $


Launch dnsmasq at boot or default runlevel and start service
Code:

~ $ rc-update add dnsmaq default
~ $ /etc/init.d/dnsmasq start
 * Starting dnsmasq...                                                                                                      [ ok ]
 ~ $
Back to top
View user's profile Send private message
lokelo
Tux's lil' helper
Tux's lil' helper


Joined: 16 Dec 2002
Posts: 101
Location: Maryland, USA

PostPosted: Fri Oct 22, 2004 7:38 pm    Post subject: Reply with quote

This was just what i was looking for. Now I can actually ping hosts on my local network. Thanks alot
Back to top
View user's profile Send private message
alligator421
Apprentice
Apprentice


Joined: 30 Jul 2003
Posts: 191

PostPosted: Sat Oct 23, 2004 1:52 pm    Post subject: Reply with quote

I'm happy that it helped you.
Back to top
View user's profile Send private message
MarkDavies
n00b
n00b


Joined: 10 Sep 2004
Posts: 19

PostPosted: Sun Apr 10, 2005 12:51 pm    Post subject: Reply with quote

Thanks for the information. It worked a treat.

I had one problem though. My ADSL/Ethernet (Dlink DSL-300T) modem renews my lease once its connected to the internet. I originally get 192.168.1.2 and then I get given my internet IP.
I found a thread that explains another way to grab the DNS entries supplied by dhcpcd. I've knocked up a few scripts that I thought might be of interest to you.

Firstly, you'll need to revert your /etc/init.d/dnsmaq script.

OK, I've based my scripts on the one mentioned in the thread

Code:
# ls -l /var/lib/dhcpc/
total 16
lrwxrwxrwx  1 root root   18 Apr  7 21:49 dhcpcd-eth0.exe -> dhcpcd-generic.exe
-rw-r--r--  1 root root  369 Apr  5 19:19 dhcpcd-eth0.info
-rwxr-xr-x  1 root root 1464 Apr  7 21:57 dhcpcd-generic.exe
-rwxr-xr-x  1 root root  223 Apr  7 21:51 dhcpcd.exe


dhcpcd.exe just works out the interface that dhcpcd has obtained information about (i.e. eth0, eth1, etc.) and calls the dhcpcd-<interface>.exe script if it exists. You could just replace the dhcpcd.exe with dhcpcd-generic.exe script if you want. I've split the scripts up so that custom ones can easily be written. You can symbilically link to the dhcpcd-generic.exe script for each interface that dhcpcd listens on.

dhcpcd-generic.exe script works out the interface that dhcpcd has obtained information about (i.e. eth0, eth1, etc.), grabs the DNS entries supplied and creates a /etc/resolv-<interface>.conf file. If a /etc/resolv-<interface>-extra.conf file exists then its contents are appended to the newly created /etc/resolv-<interface>.conf file.

Code:
# ls -l /etc/resolv*
-rw-r--r--  1 root root  51 Mar 17 23:06 /etc/resolv-eth0-extra.conf
-rw-r--r--  1 root root 142 Apr  7 21:58 /etc/resolv-eth0.conf
-rw-r--r--  1 root root  21 Apr  9 12:49 /etc/resolv.conf


Code:
# cat /etc/resolv.conf
nameserver 127.0.0.1


Code:
# cat /etc/resolv-eth0-extra.conf
nameserver 212.158.192.3
nameserver 212.158.192.2


Code:
# cat /etc/resolv-eth0.conf
# Autogenerated by ./dhcpcd-eth0.exe on Thu Apr 7 21:58:12 GMT 2005
nameserver 83.146.21.6
nameserver 212.158.192.3
nameserver 212.158.192.2


You will need to modify the dnsmasq configuration to use the /etc/resolv-<interface>.conf file.

OK, here are the two scripts....

Code:
#cat /var/lib/dhcpc/dhcpcd.exe
#!/bin/sh
DIR=`dirname $0`
EXE=`basename $0`
IF=`echo $1 | sed  -e 's/^.*-\([0-9a-z]*\)\..*$/\1/g'`

if [ -e "$DIR/dhcpcd-$IF.exe" ];
then
    logger -t \($EXE\) Running $DIR/dhcpcd-$IF.exe
    $DIR/dhcpcd-$IF.exe "$@"
fi


Code:
# cat /var/lib/dhcpc/dhcpcd-generic.exe
#!/bin/sh
echo "(dhcpcd)  Interface $2, see $1" | logger
#
# man 8 dhcpcd states parameter
# - 1 is <HostInfoFilePath>
# - 2 is <up|down|new>
# - 3 is -d if debug
#
IF=`echo $1 | sed  -e 's/^.*-\([0-9a-z]*\)\..*$/\1/g'`
RES=/etc/resolv-$IF.conf # destination file
TMP=/tmp/resolv-$IF.conf.$$ # temporary file
EXTRA=/etc/resolv-$IF-extra.conf # extra DNS entries

case $2 in
  up|new) # We want to check for dns changes when
          # starting up or renewing the lease
    if [ -e "$1" ] # File exists ?
    then
      . "$1"       # source it
    fi
    if [ "$DNS" != "" ] # Variable DNS declared?
    then
      [ -e $TMP ] && rm $TMP    # remove it if it exists
      echo \# Autogenerated by $0 on `date` >>$TMP
      for i in `echo $DNS | sed 's/,/ /g'` # process dns server addresses
      do
        echo nameserver $i >>$TMP          # create the resolv.conf line
        echo "(dhcpcd)  nameserver = $i" | logger
      done
      if [ -e "$EXTRA" ]  # if extra DNS file exists
      then
        echo "(dhcpcd)  Adding entries in $EXTRA" | logger
        cat $EXTRA >>$TMP
      fi
      # if destination file does not exist or
      # destination file differs
      if [ ! -e $RES ] || \
         [ `diff $TMP $RES | wc -l` -ne 0 ]
      then
        mv $TMP $RES # replace destination with the new one
      else
        rm $TMP      # clean temporary file
      fi
    fi
    ;;
  down)
    echo >$RES          # blank the resolv.conf file
    ;;
  *)
    ;;
esac


I've recently switched to using rp-pppoe so I now longer use the configuration above.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum