[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Persisting a one-off hostname change



If you're wondering, the way I did it was to change
/etc/init.d/hostname.sh to include:

----
PATH=/sbin:/bin:/usr/bin:/usr/local/bin

...

do_start () {
	if [ -z "${HOSTNAME}" ]; then
		MAC_MAGIC="$(macstr | sed 's/://g' | cksum | cut -d ' ' -f 1 | xargs
printf '%08X')"
		HOSTNAME="prefix-${MAC_MAGIC}"
	fi

	[ "$VERBOSE" != no ] && log_action_begin_msg "Setting hostname to '$HOSTNAME'"
	hostname "$HOSTNAME"
	ES=$?
	[ "$VERBOSE" != no ] && log_action_end_msg $ES
	exit $ES
}

...
----

The "macstr" script just contains:

----
#!/bin/sh
PATH=$PATH:/sbin
ifconfig eth0 | grep 'HWaddr' | sed -r 's/.*HWaddr
(([0-9a-zA-Z]{2}:){5}[0-9a-zA-Z]{2}).*/\1/' | tr '[:upper:]'
'[:lower:]'

----

...ie, it prints the MAC address (including ':'s, but hostname.sh
strips them). So I get a consistent 8 character checksum of what
should be a unique input — I don't think there'll be much risk of
collision. I use rc.local to rewrite /etc/issue to display the
hostname to users.

I also have another script, run by rc.local, that does some sed
replacements on configuration files containing the hostname, and
writes it to /etc/hostname so it knows when it's changed.

Thanks for the advice :)

— Jason


Reply to: