Randomly generated hostname

Mikhail Morfikov mmorfikov at gmail.com
Tue May 24 17:40:25 UTC 2016


I know that dhclient has some ability, which disables sending a hostname to a
DHCP server, but I don't want to use it. Instead I wanted to design a mechanism,
where a randomly generated hostname is sent to the DHCP server.

In the dhclient-script(8) manual you have some "reasons" that can trigger the
scripts. So I created the following scripts in the /etc/dhcp/ directory:

============================================================================
/etc/dhcp/dhclient-enter-hooks.d/random-hostname
============================================================================

#!/bin/sh

RUN="yes"

if [ "$RUN" = "yes" ]; then
	if [ "${interface}" = "wlan0" ]; then

		case $reason in
			PREINIT|RENEW)

			pick_name=$(shuf -n 1 /etc/dhcp/names)
			sed "s=$(hostname)=REPLACEME=g" -i /etc/hosts && \
				hostname $pick_name && \
				sed "s=REPLACEME=$(hostname)=g" -i /etc/hosts

			;;

		esac
	fi

fi
============================================================================

============================================================================
/etc/dhcp/dhclient-exit-hooks.d/random-hostname
============================================================================

#!/bin/sh

RUN="yes"

if [ "$RUN" = "yes" ]; then
	if [ "${interface}" = "wlan0" ]; then

		case $reason in
			BOUND|RENEW|RELEASE)

			pick_name="aaa"
			sed "s=$(hostname)=REPLACEME=g" -i /etc/hosts && \
				hostname $pick_name && \
				sed "s=REPLACEME=$(hostname)=g" -i /etc/hosts

			;;

		esac
	fi

fi
============================================================================

When I start dhclient, the first script sets the random hostname, and this
hostname is sent via network. The DHCP server sends its answer back. After that
my normal hostname is set. That's the desired behavior. But the problem is when
dhclient wants to renew the lease. See the log:


May 24 19:22:52 kornelia dhclient[21932]: DHCPDISCOVER on wlan0 to
255.255.255.255 port 67 interval 11
May 24 19:22:52 kornelia dhclient[21932]: DHCPREQUEST of 192.168.1.150 on wlan0
to 255.255.255.255 port 67
May 24 19:22:52 kornelia dhclient[21932]: DHCPOFFER of 192.168.1.150 from
192.168.1.1
May 24 19:22:52 kornelia dhclient[21932]: DHCPACK of 192.168.1.150 from 192.168.1.1
May 24 19:22:52 aaa dhclient[21932]: bound to 192.168.1.150 -- renewal in 58
seconds.


When dhclient asks for the IP, the hostname here is the randomly generated one
(the first four messages). But what happens when dhclient wants to renew the lease:

May 24 19:23:50 aaa dhclient[21998]: DHCPREQUEST of 192.168.1.150 on wlan0 to
192.168.1.1 port 67
May 24 19:23:50 aaa dhclient[21998]: DHCPACK of 192.168.1.150 from 192.168.1.1
May 24 19:23:51 aaa dhclient[21998]: bound to 192.168.1.150 -- renewal in 46
seconds.


The hostname wasn't changed.

Is there a way to force dhclient to use different hostname through all the renew
period of time? I just want to change the hostname when dhclient wants to
contact to the DHCP server. Maybe is there another way to do it?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: OpenPGP digital signature
URL: <https://lists.isc.org/pipermail/dhcp-users/attachments/20160524/d8085ba9/attachment.bin>


More information about the dhcp-users mailing list