5b98 Random hostname on boot - Telecomix Crypto Munitions Bureau

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 :-)

Testing working on [BackTrack 5R1] - You must add a dictionary words file to: /etc/dictionary-common/ which is symlinked from: /usr/share/dict/words

Contents

[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 Someone, 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_random.sh

I use an ecnrypted LVM for everything other than /boot so the suggested script gave me a few issues. However the following minor changes seem to resolve them.

#!/bin/bash
### BEGIN INIT INFO
#   Provides:             hostname_random
#   Required-Start:
#   Required-Stop:
#   Should-Start:         glibc
#   Default-Start:        S
#   Default-Stop:         
#   Short-Description:    Sets a Random Hostname
#   Description:          Select random word from /etc/default/local_wordlist, and
#                         update the kernel value with this word.
#                         If everything fails then "localhost" is used.
### END INIT INFO
# INSTALLING:
# sudo cp hostname_random.sh /etc/init.d/hostname_random.sh
# sudo chmod u+x /etc/init.d/hostname_random.sh
# sudo update-rc.d hostname_random.sh start 02 S
# --- remove "hostname"
# sudo update-rc.d hostname remove
#
# reboot & pray
#
# Based on script WRITTEN BY Someone, May 2010
# Modifications by Someone Else.  July 2012

PATH=/sbin:/bin

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

do_start () {
    OLD_HOSTNAME="$(hostname)"

    HOSTNAME=$( /bin/busybox awk 'BEGIN {srand()} {l[NR]=$0} END { print l[int(rand() * NR + 1)] }' /etc/default/local_wordlist )
    
    [ -z "$HOSTNAME" ] && HOSTNAME=localhost

    log_action_begin_msg        "The new hostname is $HOSTNAME"
    hostname "$HOSTNAME"
    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_random.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 Someone, 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


[edit] Enable the scripts

To enable these scripts on Debian (thus Ubuntu and possible other *nixes, too), run:

chmod +x /etc/init.d/hostname.sh
update-rc.d hostname.sh defaults
chmod +x /etc/init.d/hostname-rewrite-files
update-rc.d hostname-rewrite-files defaults


[edit] Much simpler solution for Arch Linux

In your /etc/rc.conf, replace your HOSTNAME line with
HOSTNAME=$(date +%N | md5sum | cut -f 1 -d " ")

[edit] Minor Issue with KDE

When you use a randomly selected and assigned hostname so that it is changed with each system boot as suggested and you use KDE4, then you can run into a minor issue. When you login, KDE creates 3 links in the ${HOME}/.kde directory. Now these links take up an infinitesimally small amount of disk space but after a few reboots will begin to make the directory seem untidy. The following three commands find any of the links that do not include the current hostname and deletes them.

After creating this script, simply open the KDE4 System Settings, select the "Startup and Shutdown" option, then on the "Autostart" tab click the "Add Script" button. In the dialog that appears find your script and make sure the "Create as Symlink" option is checked. Click "OK" and it will be added to the Script File section of Autostart. Make sure that it is Enabled and set to run at Startup.

Bingo, you're done and depending on how much you have set to autostart the script will run shortly after you log in each time.

Script:

#!/bin/bash

find ${HOME}/.kde -type l -name "cache-*" ! -name "cache-${HOSTNAME}" -exec rm {} \;
find ${HOME}/.kde -type l -name "socket-*" ! -name "socket-${HOSTNAME}" -exec rm {} \;
find ${HOME}/.kde -type l -name "tmp-*" ! -name "tmp-${HOSTNAME}" -exec rm {} \;
Personal tools
0