Random hostname on boot

From Telecomix Crypto Munitions Bureau

Jump to: navigation, search

This is tested on Ubuntu 9.04. To accomplish this, I've had to modify one script (/etc/init.d/hostname.sh) and write my own (/etc/init.d/hostname-rewrite-files). These are experimental scripts - do not use them if you do not understand them and feel confident they won't screw stuff up :-)

[edit] Why two scripts?

The first script, hostname.sh, is run as one of the first init scripts (it runs at runlevel S02) - before the filesystems have been mounted. The second script runs at run level S36, right after the filesystems have been brought up. This enables it to save the new random hostname to the files /etc/hostname and /etc/hosts. These file changes are required for the new hostname to be propagated and used throughout the system.

[edit] /etc/init.d/hostname.sh

#!/bin/sh
### BEGIN INIT INFO
# Provides:          hostname
# Required-Start:
# Required-Stop:
# Should-Start:      glibc
# Default-Start:     S
# Default-Stop:
# Short-Description: Sets a random hostname
# Description:       Read /usr/share/dict/words, pick a random word
#                    and update the kernel value with this value.
#                    If /etc/hostname is empty, it is created.
#                    The old hostname in /etc/hosts is also replaced
#                    If everything fails, the value 'localhost' is used.
### END INIT INFO
# INSTALLING:
# sudo cp hostname.sh /etc/init.d/hostname.sh
# sudo chmod u+x /etc/init.d/hostname.sh
# sudo update-rc.d hostname.sh start 02 S .
# reboot & pray
# WRITTEN BY Pragmatk, May 2010


PATH=/sbin:/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start () {
	# either current name or /etc/hostname (/etc/hostname shouldn't be missing)
	[ -f /etc/hostname ] && OLD_HOSTNAME="$(cat /etc/hostname)"

	# Keep current name if /etc/hostname is missing.
	[ -z "$OLD_HOSTNAME" ] && OLD_HOSTNAME="$(hostname)"

	# below we pick a random word and filter it for special chars (and make sure we still have string)
	# could be approached cleaner (ie rejecting a word with special chars instead of filtering,
	# but that'd give us fewer pretty names to pick from!)
	HOSTNAME=`cat /usr/share/dict/words |/usr/bin/perl -e '@w=<>;$g="";while($_!~/\A[a-z]+\Z/i){@good=$w[int rand $#w]=~/([a-z]+)/ig;$_=join("",@good);}print'`

	#default to localhost (we do not want to use a previously stored value)
	[ -z "$HOSTNAME" ] && HOSTNAME=localhost

	log_action_begin_msg	"$HOSTNAME is now the new hostname :-)"
	[ "$VERBOSE" != no ] && log_action_begin_msg "Setting hostname to '$HOSTNAME'"
	hostname "$HOSTNAME"
	ES=$?
	[ "$VERBOSE" != no ] && log_action_end_msg $ES
	exit $ES
}

case "$1" in
  start|"")
	do_start
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	# No-op
	;;
  *)
	echo "Usage: hostname.sh [start|stop]" >&2
	exit 3
	;;
esac

[edit] /etc/init.d/hostname-rewrite-files

#!/bin/sh
### BEGIN INIT INFO
# Provides:          hostname-rewrite-files
# Required-Start:    mountall
# Required-Stop:
# Should-Start:      glibc
# Default-Start:     S
# Default-Stop:
# Short-Description: Writes the current hostname to disk
# Description:       Writes the current hostname to disk when the
#                    filesystem has been remounted (to enable permanent
#                    name change).
### END INIT INFO
# Written by Pragmatk, May 2010
## INSTALLING:
# sudo cp hostname-rewrite-files /etc/init.d/hostname-rewrite-files
# sudo chmod u+x /etc/init.d/hostname-rewrite-files
# sudo update-rc.d hostname-rewrite-files start 36 S .
# hope it works
# ???
# PROFIT!


PATH=/sbin:/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start () {
	HOSTNAME=$(hostname)
	OLD_HOSTNAME=`cat /etc/hostname`
	if [ ! -z "$OLD_HOSTNAME" ] && [ ! -z "$HOSTNAME" ] && [ "$OLD_HOSTNAME" != "$HOSTNAME" ]; then
	for file in /etc/hostname /etc/hosts; do
		log_action_begin_msg "replacing ${OLD_HOSTNAME} with $HOSTNAME in file $file"
		sed s:${OLD_HOSTNAME}:${HOSTNAME}:g $file > ${file}.new && mv ${file}.new $file && log_action_begin_msg "done: replacing ${OLD_HOSTNAME} with ${HOSTNAME}" || log_action_warning_msg "failed when replacing ${OLD_HOSTNAME} with ${HOSTNAME} in $file"
	done
	fi
	exit 0
}

case "$1" in
  start|"")
	do_start
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	# No-op
	;;
  *)
	echo "Usage: hostname.sh [start|stop]" >&2
	exit 3
	;;
esac
Personal tools