LinuxQuestions.org
Visit Jeremy's Blog.
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 12-14-2009, 02:02 AM   #1
deadeyes
Member
 
Registered: Aug 2006
Posts: 609

Rep: Reputation: 79
/etc/resolv.conf search line depending on connected network


Hi all,

I use my laptop both at home and at my job.
At home it gives me the correct search line.
But when I am at the office I only get 1 search entry while if you boot in windows it gives you 3 (for example):
domain
domain.com
my.domain.com

So this should be in /etc/resolv.conf
search domain domain.com my.domain.com

It seems that this is not passed by the (Windows) dhcp server (Active Directory).
Is there any way that linux can detect depending on the network it is in that there are a few search domains added(for example like resolvconf does with ppp tunnels)?
 
Old 12-15-2009, 03:06 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If this is only about /etc/resolv.conf then if you use dhclient or similar DHCP client SW then it's just per-network configuration of DHCP client SW. If you want separate and automagical per network configuration then netenv, netGo, searching your distro repo or searching Freshmeat (like http://freshmeat.net/search?q=laptop...&submit=Search), Sourceforge and Nongnu should yield usable results.
 
Old 12-15-2009, 03:19 AM   #3
deadeyes
Member
 
Registered: Aug 2006
Posts: 609

Original Poster
Rep: Reputation: 79
Ok, thanks for your reply.

But I notice that these tools need "human interaction". Is it possible that the tool does recognize which search line it should use through network, netmask and gateway given by dhcpclient? Or are these only necessary when I want manual configuration.

What do you mean with SW?

Thanks in advance!

Last edited by deadeyes; 12-15-2009 at 03:26 AM.
 
Old 12-17-2009, 11:10 AM   #4
deadeyes
Member
 
Registered: Aug 2006
Posts: 609

Original Poster
Rep: Reputation: 79
So I did some more research and I found out that in /etc/dhclient.conf (location depending on distro) you can add the following to have static addition of multiple domain-name (search mydomain mydomain2 in /etc/resolv.conf):
Code:
append domain-name "mydomain mydomain2"
BUT: this will always add those search domains to your /etc/resolv.conf.
Another way is by installing openresolv and adding the following line to /etc/resolvconf.conf
Code:
search_domains="mydomain mydomain2"
BUT: this will again always add those entries.

REAL SOLUTION:
I found something about dhclient-script (resides in /sbin). This is just a bash script so you can easily change this script to your needs.
I changed make_resolv_conf() and added a few lines in the beginning of the file to initialize some variables (which you have to tune to your network).
After the code is changed only the first 3 variables should be edited to match your network where you want to add those additional search domains.
Code:
# Definition of variables
INTERFACE="eth0"
OFFICE_NETWORK="192.168.50."
ADDITIONAL_DOMAINS="mydomain mydomain2"

make_resolv_conf() {
  if [ x$PEER_DNS = x ] || [ x$PEER_DNS = xyes ]; then
    if [ "x$new_domain_name" != x ] || [ "x$new_domain_name_servers" != x ]; then
      conf="# Generated by dhclient for interface $interface\n"
      if [ "x$new_domain_name" != x ]; then
        # DO NOT MOVE THESE: if put with the variables, the interface wont have an IP yet
        IP=$(ifconfig $INTERFACE | grep inet\ addr: | grep -o -E "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | head -n1)
        ISATOFFICE=$(echo $IP | grep $OFFICE_NETWORK > /dev/null ; echo $?)
        if [ "$ISATOFFICE" = "0" ]; then
           conf="${conf}search $new_domain_name $ADDITIONAL_DOMAINS\n"
        else
           conf="${conf}search $new_domain_name\n"
        fi
      fi
      for nameserver in $new_domain_name_servers; do
        conf="${conf}nameserver $nameserver\n"
      done
      if [ -x /sbin/resolvconf ]; then
        printf "$conf" | resolvconf -a "$interface"
      else
        printf "$conf" > /etc/resolv.conf
        chmod 644 /etc/resolv.conf
      fi
    fi
  fi
  # If we're making confs, may as well make an ntp.conf too
  make_ntp_conf
}
For people who want to use this I will put a unified diff:
Code:
--- /sbin/dhclient-script.bak   2009-12-17 17:05:36.000000000 +0100
+++ /sbin/dhclient-script       2009-12-17 17:59:17.000000000 +0100
@@ -22,12 +22,24 @@
 # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
 # of the $1 in its args.

+# Definition of variables
+INTERFACE="eth0"
+OFFICE_NETWORK="192.168.50."
+ADDITIONAL_DOMAINS="mydomain mydomain2"
+
 make_resolv_conf() {
   if [ x$PEER_DNS = x ] || [ x$PEER_DNS = xyes ]; then
     if [ "x$new_domain_name" != x ] || [ "x$new_domain_name_servers" != x ]; then
       conf="# Generated by dhclient for interface $interface\n"
       if [ "x$new_domain_name" != x ]; then
-        conf="${conf}search $new_domain_name\n"
+       # DO NOT MOVE THESE: if put with the variables, the interface wont have an IP yet
+       IP=$(ifconfig $INTERFACE | grep inet\ addr: | grep -o -E "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | head -n1)
+       ISATOFFICE=$(echo $IP | grep $OFFICE_NETWORK > /dev/null ; echo $?)
+       if [ "$ISATOFFICE" = "0" ]; then
+          conf="${conf}search $new_domain_name $ADDITIONAL_DOMAINS\n"
+       else
+           conf="${conf}search $new_domain_name\n"
+       fi
       fi
       for nameserver in $new_domain_name_servers; do
         conf="${conf}nameserver $nameserver\n"
As you can see: only a few lines of code!

I will do a final test when I am home in a few hours
 
Old 12-22-2009, 03:25 AM   #5
deadeyes
Member
 
Registered: Aug 2006
Posts: 609

Original Poster
Rep: Reputation: 79
Quote:
Originally Posted by deadeyes View Post
So I did some more research and I found out that in /etc/dhclient.conf (location depending on distro) you can add the following to have static addition of multiple domain-name (search mydomain mydomain2 in /etc/resolv.conf):
Code:
append domain-name "mydomain mydomain2"
BUT: this will always add those search domains to your /etc/resolv.conf.
Another way is by installing openresolv and adding the following line to /etc/resolvconf.conf
Code:
search_domains="mydomain mydomain2"
BUT: this will again always add those entries.

REAL SOLUTION:
I found something about dhclient-script (resides in /sbin). This is just a bash script so you can easily change this script to your needs.
I changed make_resolv_conf() and added a few lines in the beginning of the file to initialize some variables (which you have to tune to your network).
After the code is changed only the first 3 variables should be edited to match your network where you want to add those additional search domains.
Code:
# Definition of variables
INTERFACE="eth0"
OFFICE_NETWORK="192.168.50."
ADDITIONAL_DOMAINS="mydomain mydomain2"

make_resolv_conf() {
  if [ x$PEER_DNS = x ] || [ x$PEER_DNS = xyes ]; then
    if [ "x$new_domain_name" != x ] || [ "x$new_domain_name_servers" != x ]; then
      conf="# Generated by dhclient for interface $interface\n"
      if [ "x$new_domain_name" != x ]; then
        # DO NOT MOVE THESE: if put with the variables, the interface wont have an IP yet
        IP=$(ifconfig $INTERFACE | grep inet\ addr: | grep -o -E "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | head -n1)
        ISATOFFICE=$(echo $IP | grep $OFFICE_NETWORK > /dev/null ; echo $?)
        if [ "$ISATOFFICE" = "0" ]; then
           conf="${conf}search $new_domain_name $ADDITIONAL_DOMAINS\n"
        else
           conf="${conf}search $new_domain_name\n"
        fi
      fi
      for nameserver in $new_domain_name_servers; do
        conf="${conf}nameserver $nameserver\n"
      done
      if [ -x /sbin/resolvconf ]; then
        printf "$conf" | resolvconf -a "$interface"
      else
        printf "$conf" > /etc/resolv.conf
        chmod 644 /etc/resolv.conf
      fi
    fi
  fi
  # If we're making confs, may as well make an ntp.conf too
  make_ntp_conf
}
For people who want to use this I will put a unified diff:
Code:
--- /sbin/dhclient-script.bak   2009-12-17 17:05:36.000000000 +0100
+++ /sbin/dhclient-script       2009-12-17 17:59:17.000000000 +0100
@@ -22,12 +22,24 @@
 # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
 # of the $1 in its args.

+# Definition of variables
+INTERFACE="eth0"
+OFFICE_NETWORK="192.168.50."
+ADDITIONAL_DOMAINS="mydomain mydomain2"
+
 make_resolv_conf() {
   if [ x$PEER_DNS = x ] || [ x$PEER_DNS = xyes ]; then
     if [ "x$new_domain_name" != x ] || [ "x$new_domain_name_servers" != x ]; then
       conf="# Generated by dhclient for interface $interface\n"
       if [ "x$new_domain_name" != x ]; then
-        conf="${conf}search $new_domain_name\n"
+       # DO NOT MOVE THESE: if put with the variables, the interface wont have an IP yet
+       IP=$(ifconfig $INTERFACE | grep inet\ addr: | grep -o -E "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | head -n1)
+       ISATOFFICE=$(echo $IP | grep $OFFICE_NETWORK > /dev/null ; echo $?)
+       if [ "$ISATOFFICE" = "0" ]; then
+          conf="${conf}search $new_domain_name $ADDITIONAL_DOMAINS\n"
+       else
+           conf="${conf}search $new_domain_name\n"
+       fi
       fi
       for nameserver in $new_domain_name_servers; do
         conf="${conf}nameserver $nameserver\n"
As you can see: only a few lines of code!

I will do a final test when I am home in a few hours
It does work as wanted.
Only thing is that when you don't have any network connected, you will get the search line from the office.
But that has no influence as you don't have network
 
  


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
resolv.conf search limited to six Xitium Linux - General 7 12-29-2009 03:40 AM
search in /etc/resolv.conf gyanug Linux - Networking 1 04-10-2009 10:36 AM
search path in resolv.conf overwritten by the hostname after reboot powah Linux - Networking 3 03-20-2009 02:13 PM
/etc/resolv.conf question. Max Search Entries. grizly Linux - Networking 1 05-28-2008 07:43 PM
resolv.conf search line dunkyb Linux - Networking 4 01-28-2005 01:50 AM

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

All times are GMT -5. The time now is 08:38 PM.

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