# Setting a laptop domain from DHCP

## sirlark

I have a laptop. I have set its host name in /etc/conf.d/hostname and /etc/hosts. I only put the hostname alias into /etc/hosts, not the fqdn. What I would like is for the domain name to be set dynamically, maybe pulled from DHCP with a fallback if I'm not connected to any network? Is this possible, and if so, how?

----------

## depontius

It can be done.  I do it, but your answer isn't simple, because "it depends..."  

Are you just using baselayout, or do you use network-manager, wicd, or the KDE equivalent?

Do you just plug this into one network, or would you like it to adapt to whatever network it's connecting to?

Do you want to take other network-based actions?

I have a mishmash of scripts on a multi-boot machine, so it hooks into network-manager on FC13 or RHEL6, or dhcp-5.x on Gentoo.

Once there, it gets my hostname back from DNS and sets both short and FQDN.

If I'm at work...

  I start OpenAFS.

  I start sshd.

  I tell gdm to allow TCP connections to X11.

  I tweak dnsmasq to also use company nameservers for company networks.

  I tweak the firewall to make sure the above stuff all works.

If I'm at home...

  I start sshd

  I tell gdm to allow TCP connections to X11.

  I tweak dnsmasq to also use my home nameserver for my home network.

  I tweak the firewall, both to allow above and to tighten beyond in-work needs.

If I'm on some other network...

  I start nothing.

  I tell gdm to only allow Unix socket connections to X11

  I tighten up the firewall beyond in-work or home needs.

----------

## sirlark

I'm using wicd, and sometimes blueman to form a 3G connection via my phone. And yes I would like to do a bunch of other stuff depending on the networks, off the top of my head 

At work:

 set my domain name to my office domain

 set up an ssh tunnel from the 'office' subnet to the 'lab' subnet

 start cups but block it from polling for the home printer

At home:

 set domain name to home domain

 start cups but block it polling for the work printer

Other networks:

 set my domain name to a fallback default, e.g. .local.net

My question is how do I do the domain name setting thing

----------

## depontius

First you need to get hooked in.  I'm not sure how this is done with WICD, with dhcp-5 you insert or symlink something into /lib/dhcpcd/dhcpcd-hooks/ and with network-manager you insert or symlink something into /etc/NetworkManager/dispatcher.d/.

Second you need to filter events.  You'd like most of this stuff to run once, not on every lease renewal.  So you'll have to see what's passed to you and make sure you only run on a new lease.  In addition network-manager passes some sort of event before it even gets an address, so that needs filtering out, too.

Third, once you're in your script, you need to find your IP.

```
export myIP=`/sbin/ip addr show dev ${1} | grep inet | cut -d/ -f 1 | cut -dt -f 2 | cut --delimiter=" " -f 2`
```

Fourth, you can tweak your hostname.  Here's a simplified version of what I use.  (I log a lot of stuff, and that's taken out here.)

```
oldFQDN=`hostname -f`

oldSN=`hostname | cut -d. -f1`

newFQDN=`host ${1} | cut --delimiter=" " -f 5`

newSN=`echo ${newFQDN} | cut -d. -f1`

if [ "x${oldFQDN}" = "x${newFQDN}" ] ; then

  #Error reporting stuff

else

  /usr/bin/logger -t netAdapt "fixHostname: change ${oldFQDN} to ${newFQDN} and ${oldSN} to ${newSN}"

  hostname ${newSN}

  tmpName=`mktemp`

  sed -e "s/${oldFQDN}/${newFQDN}/g" /etc/hosts >${tmpName}

  sed -e "s/${oldSN}/${newSN}/g"     ${tmpName} >/etc/hosts

fi

```

This code has 2 weaknesses that I see...  First, our DDNS around this place tends to be slow.  Every now and then I'll get a not-found-type error back from DNS, because even though it's going to give me my request hostname, it hasn't flushed through the system yet.  My code doesn't handle that situation well at all.  Second, at least on my FC13/RH6 installs network-manager also creates some sort of ipv6 line in /etc/hosts.  My scripts do a rather ugly job of editing that line, turning it into absolute gibberish.  For me it doesn't matter because I'm currently ipv4-only

----------

