genhostname
#!/usr/bin/env bash
# Description: Generate and set a random hostname on Linux
# Usage: set-random-hostname
__gen_random_hostname() {
# set a prefix if you'd like (thx for the idea @czerniachowicz)
local prefix="${1:-ubuntu}"
# set the size of the randomly generated portion
local random_size="6"
# generate new hostname
if [ -n "$prefix" ]; then
local new_hostname="$(echo ${prefix} | sed -e 's/[^[:alnum:]]//g')-$(head -n1 < <(fold -w${random_size} < <(tr -cd 'a-z0-9' < /dev/urandom)))"
else
local new_hostname="$(head -n1 < <(fold -w${random_size} < <(tr -cd 'a-z0-9' < /dev/urandom)))"
fi
# ensure new hostname is not too long
if [ ${#new_hostname} -gt 63 ]; then
new_hostname=${new_hostname:0:63}
fi
echo "$new_hostname"
}
__gen_random_hostname "$1"