Persistent Search Domains With NWAM and DHCP

What I Want

I want to be able to refer to systems on both my home and work networks by their hostnames rather than their fully-qualified domain names, so, ‘prey’ instead of ‘’ and ‘acad2’ instead of ‘’.

NWAM Settings

The Problem

I would typically set my home and work domains as the search setting in /etc/resolv.conf. Unfortunately, either NWAM or the Solaris DHCP client (I haven’t decided which) overwrites resolv.conf on every new connection. DHCP on Linux does the same thing, but I can configure it by editing dhclient.conf (or whatever is being used these days, it’s been a while. I think I just set my domains in the NetworkManager GUI and forget about it).

The Solaris DHCP client configuration is not nearly as flexible, and neither is NWAM which gives you the option of replacing resolv.conf with information supplied by the DHCP server, or provided by you, but not a mix of both. I do like having the nameservers set by the DHCP server, so supplying a manual configuration is not an option.

What I Tried

The first thing I tried was setting the LOCALDOMAIN environmental variable in /etc/profile. From the resolv.conf man page:

You can override the search keyword of the system
resolv.conf file on a per-process basis by setting the
environment variable LOCALDOMAIN to a space-separated list
of search domains.

I thought, great, a way to manage domain search settings without worrying about what’s doing what to resolv.conf. It didn’t work as advertised:

% ping prey
ping: unknown host prey
% s touch /etc/resolv.conf
% ping prey
prey is alive
% ping prey
ping: unknown host prey

Next, I considered adding an NWAM Network Modifier to set my search string in resolv.conf after a new connection is established. This worked reasonably well, but didn’t handle the case when you switch from one network to another, for example, from wireless to wired. The only events in NWAM that can trigger a script when the network connection changes happens before DHCP messes up resolv.conf.

Finally, in the course of my testing, I discovered that the svc:/network/dns/client service was restarting with every network connection change. I looked into its manifest and saw that it was designed to wait for changes to resolv.conf:

 Wait for potential DHCP modification of resolv.conf.
    <service_fmri value='svc:/network/service' />

So I could write another service which depends on dns/client and restarts whenever dns/client does and I would have the last word about what goes into my configuration file!

My Solution

I wrote a service, svc:/network/dns/resolv-conf, with the following manifest:

<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">

<service_bundle type="manifest" name="dns-resolv-conf">
    <service name="network/dns/resolv-conf"
        <create_default_instance enabled="false" />
        <single_instance />

        <dependency name="dns-client"
            <service_fmri value="svc:/network/dns/client" />

        <dependent name="resolv-conf"
            <service_fmri value="svc:/milestone/name-services" />

        <exec_method type="method"
            exec="/lib/svc/method/dns-resolv-conf start"
            timeout_seconds="60" />

        <exec_method type="method"
            exec="/lib/svc/method/dns-resolv-conf stop"
            timeout_seconds="60" />

        <property_group name="options" type="application">
            <propval name="search" type="astring" value="" />

        <property_group name="startd" type="framework">
            <propval name="duration" type="astring" value="transient" />

        <stability value="Unstable" />

                <loctext xml:lang="C">resolv.conf Settings</loctext>
                <manpage title="resolv.conf" section="4"
                    manpath="/usr/share/man" />

which calls the script, /lib/svc/method/dns-resolv-conf containing:


. /lib/svc/share/

search=$(svcprop -p options/search $SMF_FMRI)

case "$1" in
        # Don't do anything if search option not provided.
        [ "$search" == '""' ] && exit $SMF_EXIT_OK

        # Reverse the lines because we either want to:
        #   add the search line after the *last* domain line or
        #   add it to the very top of the file if there is no domain line
        tac /etc/resolv.conf | grep -v "^search" | gawk '
            /^domain/ {
                if (!isset) {
                    print "search", $2, search

            END {
                if (!isset) {
                    print "search", search

search="$search" | tac > /etc/ && mv -f /etc/ /etc/resolv.conf

        # Just get rid of any search lines, I guess.
        grep -v "^search" /etc/resolv.conf > /etc/ && mv -f /etc/ /etc/resolv.conf

        echo "Usage: $0 { start | stop }"
        exit $SMF_EXIT_ERR_CONFIG


So now I can set my search options like:

% svccfg -s resolv-conf setprop 'options/search=""'
% svcadm refresh resolv-conf
% svcadm enable resolv-conf
% cat /etc/resolv.conf

Problem solved! Or at least worked-around in the least hacky way I can!