LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2004, 07:19 PM   #1
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Question /etc/resolv.conf and dnsmasq on a gateway


i have a slackware box acting as a gateway for a lan...

the slackware box has a dhcp-configured connection to the internet...

it is also running dhcp-server and dnsmasq (for the machines on the lan)...

but i'd like to know how to make the slackware box use it's local dnsmasq also, while still obtaining all it's regular configuration from the ISP's dhcp server...

i want someone who sits to use the slackware box (yes, it's an X workstation) to be able to enjoy dnsmasq's awesome dns request cache (just like if they were on one of the other machines on the lan), instead of dealing directly with my ISP's dns servers ...

what's the best way to go about doing this??

=)


Last edited by win32sux; 07-28-2004 at 11:34 PM.
 
Old 07-28-2004, 11:27 PM   #2
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
HI Mr.,
For your local resolutions to pass by dnsmasq, all you have is to put the address of "lo" as as dnsserver in your resolv.conf
Code:
YOURMACHINE:~# cat /etc/resolv.conf
nameserver 127.0.0.1
You must pay attention how and where your internet setup scripts put your servers for it doesn't overwrite your resolv.conf.
 
Old 07-28-2004, 11:47 PM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
hello osvaldomarques! thanks for replying!

i just have a few questions:

if i put that in /etc/resolv.conf, won't it be overwritten next time the dhcp client runs??

should i be lookng for a way to add "nameserver 127.0.0.1" to the top of my /etc/resolv.conf after the dhcp client has written to /etc/resolv.conf???

also, i believe dnsmasq looks at the /etc/resolv.conf of the machine it's running on, right (please correct me if i'm wrong)??

so i'm wondering if perhaps this can have a negative effect on dnsmasq... if dnsmasq detects "localhost" as a "real" dns server then could it end up in some kinda infinite loop??

or perhaps dnsmasq ignores localhost as a real (isp) dns server when found in /etc/resolv.conf??

thanks for you input!!
 
Old 07-29-2004, 12:14 AM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Sorry for a partially complete answer. I did it about one year ago. You have to inform dnsmasq to not look for /etc/resolv.conf. My dnsmasq.conf is
Code:
XXX:~# cat /etc/dnsmasq.conf
filterwin2k
cache-size=500
resolv-file=/etc/ppp/resolv.conf
As my connection is via ppp, I redirected resolv-file to the one assembled by ppp. I was looking the case of the dhcp client and I suggest you to read "man dhclent-script", where it talks about HOOKS to not overwrite the /etc/resolv.conf.
 
Old 07-29-2004, 06:35 AM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
thanks for that good info!!


i created a resolv file for dnsmasq and i put my current ISP's dns servers in it:

cat /etc/resolv.conf > /etc/resolv.conf.masq


then i killed my running dhcp client and dnsmasq daemon:

killall dhcpcd dnsmasq


then i told the machine to use localhost (dnsmasq) for dns queries:

echo "nameserver 127.0.0.1" > /etc/resolv.conf


then i re-started the dhcp client, telling it "do not touch" /etc/resolv.conf:

dhcpcd -R eth0


then i re-started dnsmasq and told it to use the new dns file:

dnsmasq --resolv-file=/etc/resolv.conf.masq


THIS WORKS GREAT!!!

=)


as you can see, there's only one thing i'm missing now:

i need to find a way for the dhcp client to write the dns server addresses to /etc/resolv.conf.masq instead of /etc/resolv.conf...

i tried editing /sbin/dhclient-script (changing the two instances of "resolv.conf" to "resolv.conf.masq") but that didn't help...


Last edited by win32sux; 07-29-2004 at 06:43 AM.
 
Old 07-29-2004, 04:45 PM   #6
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi,
In your last post you referenced dhcpcd as you dhcp client. The dhclient-script is for another dhcp client named "dhclient". For dhcpcd you have to edit the script "/etc/dhcpc/dhcpcd-ethN.exe, where N is your ethernet. Look at "man dhcpcd".
 
Old 07-30-2004, 06:00 AM   #7
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
i took a good look at the dhcpcd manual and at that script, but no luck...

i think that script only runs when dhcpcd detects a change in the ip address, mine looks like this:

Code:
bash-2.05b# cat /etc/dhcpc/dhcpcd-eth0.exe 
PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
#!/bin/sh
echo "(dhcpcd)  IP address changed to $1" | logger

for now, i could still use dnsmasq locally by setting the dns servers in the alternate location manually, but if/when my isp changes the dns server addresses i would be out of business...

=(
 
Old 07-30-2004, 06:09 AM   #8
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
i can't believe specifying an alternate location for /etc/resov.conf is so difficult...

i'm starting to think /etc/resolv.conf is probably being used from inside the dhcpcd binary...

i'm gonna "grep" through the dhcpcd source code to see what i can find...

i'll let you know if i find anything... and if i do, i'll try changing it and re-compiling dhcpcd...
 
Old 07-30-2004, 07:55 PM   #9
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi man,
It is not so hard! All the variables got from the dhcp server are in the an script like file which is the first parameter to the dhcpcd-eth0.exe. The second parameter tell us about the reason it is called, "up", "down" or "new" for a lease renew. The third parameter is just for debug. So, all we need is to interpret the passed file and generate our resolv.conf. Here go my suggestion:
Code:
Modelo:/mnt/ref/tmp/dhcpcd/etc/dhcpc# cat dhcpcd-eth0.exe
#!/bin/sh
echo "(dhcpcd)  IP address changed to $1" | logger
#
# man 8 dhcpcd states parameter
# - 1 is <HostInfoFilePath>
# - 2 is <up|down|new>
# - 3 is -d if debug
#
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
      RES=/etc/resolv.conf.masq # destination file
      TMP=/tmp/resolv.conf.$$ # temporary file
      [ -e $TMP ] && rm $TMP    # remove it if it exists
      for i in `echo $DNS | sed 's/,/ /g'` # process dns server addresses
      do
        echo nameserver=$i >>$TMP          # create the resolv.conf line
      done
      # 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
    ;;
  *)
    ;;
esac
Of course, you have to use the "-R" for not replacing the current resolv.conf.

Have fun!
 
Old 08-02-2004, 04:47 PM   #10
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
hey man thanks a lot for the script and all the help!!

i got it working by editing the source code and recompiling... i got the source here:

ftp://ftp.slackware.com/pub/slackwar...source/n/dhcp/

there's a file called pathnames.h in the tarball...

i simply changed /etc/resolv.conf to /etc/resolv.masq, i recompiled, and it did the trick...

Quote:
bash-2.05b# cat pathnames.h
/*
* dhcpcd - DHCP client daemon -
* Copyright (C) 1996 - 1997 Yoichi Hariguchi <yoichi@fore.com>
* Copyright (C) 1998 Sergei Viznyuk <sv@phystech.com>
*
* Dhcpcd is an RFC2131 and RFC1541 compliant DHCP client daemon.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef PATHNAMES_H
#define PATHNAMES_H

#include <paths.h>
#include "dhcpcd.h"

#define PID_FILE_PATH "%s/"PROGRAM_NAME"-%s.pid"
#define DHCP_CACHE_FILE "%s/"PROGRAM_NAME"-%s.cache"
#define DHCP_HOSTINFO "%s/"PROGRAM_NAME"-%s.info"
#define EXEC_ON_CHANGE "%s/"PROGRAM_NAME".exe"

#ifdef EMBED
#define CONFIG_DIR "/etc/config/dhcpc"
#define RESOLV_CONF "/etc/config/resolv.conf.masq"
#define NIS_CONF "/etc/config/yp.conf"
#define NTP_CONF "/etc/config/ntp.conf"
#else
#define CONFIG_DIR "/etc/dhcpc"
#define RESOLV_CONF "/etc/resolv.conf.masq"
#define NIS_CONF "/etc/yp.conf"
#define NTP_CONF "/etc/ntp.conf"
#endif

#endif

i'm sure your script was great, but i want to be able to do these things from memory, and it would be impossible for me to memorize the script...

but still, your help is greatly appreciated!! thanks a million!!

=)


Last edited by win32sux; 08-02-2004 at 04:48 PM.
 
Old 02-26-2005, 11:45 PM   #11
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
i just ran into this thread and i thought i'd share the method i eventually ended-up using for this issue (in case it might be useful for someone else)...

instead of editing scripts and/or source code, i simply added these three lines to my /etc/rc.d/rc.local file:

Code:
cat /etc/resolv.conf > /etc/resolv.dnsmasq
echo "nameserver 127.0.0.1" > /etc/resolv.conf
dnsmasq -r /etc/resolv.dnsmasq
so basically what happens is:

- the computer gets it's dhcp info and the nameservers are written to /etc/resolv.conf (as usual)...

- /etc/rc.d/rc.local gets run, and the nameservers obtained from the dhcp server (/etc/resolv.conf) are sent to file /etc/resolv.dnsmasq...

- file /etc/resolv.conf is made to only contain the text "nameserver 127.0.0.1", which makes the machine use itself (localhost) as it's nameserver...

- then when dnsmasq is started it is told to use the nameservers in /etc/resolv.dnsmasq, which will be the ones which were obtained at startup time...

=)
 
Old 02-27-2005, 08:30 AM   #12
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi win32sux,
Nice to read you again! Your method is simple and smart. However you may run into problems if you stay on line long enough to get a renew. As your resolv.conf is different from the newly created, dhcpcd will give you another without you take notice.
I did a look into dhcpcd source and it appears the script runs after dhcpcd writing of a new resolv.conf. So you can put your commands in the dhcpcd script to make sure even if you get a renewal of the lease, you stay with the proper resolv.conf and resolv.dnsmasq.

Have a nice sunday!

Osvaldo.
 
Old 02-27-2005, 02:43 PM   #13
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
hello osvaldomarques!!

well, you're absolutely right - the main issue with my method is that upon dhcp renewal localhost would once again be using the ISP's nameservers... i was actually aware of this, but because the machine in question is an X workstation which gets turned on and off sporadically, it didn't really affect me at all... i've actually never seen a renewal on this machine...

however, i think you're idea is great, and i will do as you suggested...

i'm gonna put my three lines in the dhcpcd script so that they are run right after any renewal... this would make the solution more apt for anyone trying this on a real dhcp gateway which is online 24/7...

i'll post the location where i placed the lines once i get it right...

take care bro... and many thanks!!

=)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dnsmasq.conf exvor Linux - Software 2 06-24-2005 05:28 PM
resolv.conf stanleytiew Linux - Newbie 2 05-13-2005 04:25 PM
resolv.conf Snump Mandriva 54 10-31-2004 04:16 PM
resolv.conf ricemark20 Linux - Networking 12 11-11-2003 04:08 PM
resolv.conf dnsbum Linux - Networking 1 10-21-2003 12:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration