Ubuntu

hostname supplied by dhcp server is not used

Reported by Gökdeniz Karadağ on 2007-03-07
50
This bug affects 8 people
Affects Status Importance Assigned to Milestone
dhcp3 (Ubuntu)
Low
Unassigned

Bug Description

I am trying to make lab machines get hostname from DHCP. Currently active solution is to grep ethX's ip address, do a reverse lookup and call hostname with the result. This breaks when another ethernet card is plugged into the machine as ethX does no longer have an ip and ethY has it instead.

Instead of more interface guessing, I want to have a more robust solution, as DHCP server already sends host-name option. I experimented with "get-lease-hostnames yes" and, "option host-name XXX" options on the server. In the client I experimented with "request;" and request ..., host-name,...; options in dhclient.conf

I see the option host-name "XXX" in the lease file. But still the machine gets the hostname defined in /etc/hostname (which is "NO-NETWORK" to indicate a network problem to users)

If there is a setting I missed, that enables using DHCP supplied host names, feel free to reject this bug report. But all the solutions I found involved in writing new scripts. I think dhclient should set the host name sent by DHCP server when request host-name is set in the config file.

Related branches

Martin Pitt (pitti) wrote :

This cannot be enabled by default in dhcp client. X becomes very angry if you change the hostname underneath it, so this should only be done at boot time, not when changing the lease at runtime.

However, I do agree that there should be an option to enable this feature for people who know what they are doing.

Changed in dhcp3:
importance: Undecided → Low
status: Unconfirmed → Confirmed
Alex Mauer (hawke) wrote :

I believe Fedora can do this; how does it work over there?

Tam Pham (phamdt8) wrote :

I'm interested in this too. I've VMs that people can just use. When the VM boot up, it should get the hostname from the dhcp server. My Fedora VMs are working fine, but I've no clue how to do with Ubuntu.

Bart Cerneels (stecchino) wrote :

I need this for a rollout in a College computer lab where the hard-drives (dual-boot Kubuntu/WinXP) are cloned to 24 identical machines. These should take their hostname from DHCP.
I believe the correct option in /etc/dhcp3/dhclient.conf is:
default host-name "NO-NETWORK"

But this doesn't seem to work for me.

Importance ++

Thomas (thomas-sprinkmeier) wrote :

A hook script in "/etc/dhcp3/dhclient-exit-hooks.d/" should be able to fix this.

As has been mentioned, X might get angry, but you could always check for a running X before changing the hostname (assuming it's better to have the wrong hostname than it is to have a non-working X).

Signaling a "no network" error to users could be done with /etc/motd, /etc/issue and/or xmessage

A slight improvement on the interface guessing might be chasing up the default gateway:

host $(ifconfig `ip route | awk '/^default/{print $5}'` | sed -rne 's/.*inet addr:(.*)Bcast.*/\1/p') | cut --delim \ --field 5

The reverse lookup can return a different host name than declared for the kernel; I do not see any problem with that, given that different network interfaces can belong to different domains, let alone have different names.

jason (jason-doozer) wrote :

at least in dapper, i couldn't tell if that's what this bug was attached to, you can remove /etc/hostname and the hostname from the server will be used. however, there is the problem that if you do this and there isn't an entry for the ip address, the /etc/dhcp3/dhclient-script will set the hostname to blank which is no good. sudo will no longer work for starters. i think that might should be a different bug.

Gökdeniz Karadağ (gokdeniz) wrote :

We solved it by putting "hostname $new_host_name" into a file at /etc/dhcp3/dhclient-exit-hooks.d/
Thanks Thomas for the tip.

default host-name "NO-NETWORK" did not work, when network was not available hostname was "none" (or blank, i'm not sure) as jason said. Booting the machine took ages, so we fleed to other solutions.

Maybe as a fix, for this bug, a sethostname script can be included in dhclient-exit-hooks.d. It can be disabled by default like the debug script there.

Be aware that, we had problems with default ubuntu installation(and loads of packages we install afterwards). "smart" NetworkManager made DCHP client ask for leases "after" gdm started, so no-X-running condition was not satisfied at that stage. We solved this by populating /etc/network/interfaces as usual. The default had only loopback interface there.

Here is a script, checks for only a running gdm though. $new_host_name is set by dhclient before calling this;

=== /etc/dhcp3/dhclient-exit-hooks.d/sethostname ======================
# This script sets the machine hostname to the hostname sent from the DHCP server.
# Beware, if this happens while X is running, there will be problems, so the script checks
# for a running gdm and does not change the hostname if it detects one.
# If you want to enable this script, change SETHOSTNAME to "yes"

SETHOSTNAME="yes"

if [ "$SETHOSTNAME" = "yes" ]; then
        if test -r /var/run/gdm.pid && ps -ef | grep $(cat /var/run/gdm.pid) | grep -q /usr/sbin/gdm ; then
                echo "$(date): GDM running, not changing host name" >> /tmp/dhcp-sethostname.err ;
        else
                hostname $new_host_name;
        fi
fi

Frederic M (dist-list) wrote :

Tx Gökdeniz,
Can you be more specific about the other modifications you added in dhclient.conf and or interfaces ? I added the script without touching any other conf files and do not get my hostname from the dhcp server.

Gökdeniz Karadağ (gokdeniz) wrote :

Hi,

dhclient.conf does not need modifications.

On ubuntu, /etc/network/interfaces file has only loopback interface,
This causes the eth0 interface to be down until glorious NetworkManager kicks in.

Add the following lines to /etc/network/interfaces as a fix
auto eth0
iface eth0 inet dhcp

Gregg (gwiecek) wrote :

Gökdeniz Karadağ - your solution doesn't work on Ubuntu 8.10.
I had completely removed NetworkManager and put correct configuration lines into the interfaces file (network connection is working fine) and this did not changed anything.
Your previous solution did the trick. So thank you very much for that.

tynaud (tynaud) wrote :

Hello...

I was so happy finding this script but it didn't work for me... I tried to use it on karmic today. First I did like on hardy, using a hostname file empty (it was well working), but it seems it prevents the eth0 interface to be brought up networkmanager and even if declared in /etc/network/interfaces.
So it seems that the script is launched later than gdm... But I have not the /tmp/dhcp-sethostname.err file which should have been created...

So how to change this order and bring up eth0 before gdm ?

tynaud (tynaud) wrote :

Hi all,

well, after reinstallation an reconfiguration of the full system, it now works... The only thing i did is to remove the file /etc/hostname (without believing in this action) and it's ok now... So now I can deploy.... Sorry for that...

Gökdeniz Karadağ (gokdeniz) wrote :

Another approach might be to set "/etc/hostname" to the value from DHCP server, and leave the current hostname alone. That would set the host name to correct value at next boot, without any clash with netowork manager behaviour or a running X [ and hopefully the name would not change between reboots :) ]

As 9.10 enables parallel initscripts with upstart, having the network up before gdm(or kdm) can be hard, you may need to change ordering of initscripts, or it may be undesirable (startup "seems" slower). I cannot comment exactly about the situation as we are still at 8.04 LTS.

simply change the line
hostname $new_host_name;
TO
if [[ -n $new_host_name ]]; then echo "$new_host_name" > /etc/hostname ; fi

It's a shame Ubuntu can't get hostname from DHCP server. It's 2010. Bug is almost three years old.
But before I read the comments I didn't realize that X server may be affected by hostname change.
Any thoughts how can this be handled? I really want this fixed.

I don't use NetworkManager, for me networking script is preferable.
I read on some forum that people just use another dhcp client to get things working. Won't it be better to ship Ubuntu with fully working DHCP client?

Hope to see some move in right direction

I've just tested this on 9.10 server. Just create the file '/etc/dhcp3/dhclient-exit-hooks.d/hostname' with the contents:

if [[ -n $new_host_name ]]; then
  echo "$new_host_name" > /etc/hostname
  /bin/hostname $new_host_name
fi

Works!

Hello?? It used to work in Hardy!!!

Therefore either a newer version of dhcp3-client or network-manager or a change to the default configuration by the ubuntu developers must have caused this to stop working. The ability to centrally manage large numbers of workstations includes the ability to set the hostname from the server.

I want to express my irritation and frustration over this and other alterations made to ubuntu that makes System administrators lifes miserable. Do you know how much time I have spend to debug this stupid non-bug?
Another problem are these UUID's that popped up in /etc/fstab, in the grub config, even in the initrd for specifying the `resume partition'.

In a network of Ubuntu workstations, I want to:

* set hostnames from the server, via DHCP. NOT the other way around, i.e. let the client change the DNS server records. I do not want to have to create lots of little scripts like in the solution of Christoph Dwertmann for something that used to work without all that.
* I do not want to have network manager at all in managed clients. The users are not allowed to alter the network settings, the network is configured once at boot via dhcp and that is that. No network-manager needed. Make it removable.
* be able to use a single image to roll out to clients. This means that UUID's are not handy to have in /etc/fstab, or in the grub config files. The UUID of partitions in the master image will be wrong for the clients. I do not want to have to fix all the places in /etc/fstab, /boot/grub/grub.cfg, /etc/grub.d/*, the initrd file even, to replace the UUID for the clients UUID's. I just want to use the bloody device files! Just /dev/sda. Can the developers please make it an option to use device files throughout?

Please developers, think about people who administrate Ubuntu in schools, universities, companies etc. Not all ubuntu users are home-users, or laptop users, or server users. You are forgetting a whole class of ubuntu installations, which is Managed Workstations. Please make it easier for the sysadmin.

tags: added: regression-release
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package dhcp3 - 3.1.3-2ubuntu3

---------------
dhcp3 (3.1.3-2ubuntu3) lucid; urgency=low

  * debian/dhclient-script.linux: Fix regression in host_name option
    handling, so that it's always honored when /etc/hostname is not set,
    fixes LP: #537978, #482313, #90388, #476491
 -- Thierry Carrez <email address hidden> Thu, 01 Apr 2010 16:52:36 +0200

Changed in dhcp3 (Ubuntu):
status: Confirmed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Bug attachments