Skip to content

Instantly share code, notes, and snippets.

@Varstahl
Created May 29, 2019 13:52
Show Gist options
  • Save Varstahl/099d1a845fd64bfdd33c3e65a1a4900c to your computer and use it in GitHub Desktop.
Save Varstahl/099d1a845fd64bfdd33c3e65a1a4900c to your computer and use it in GitHub Desktop.
A small CentOS VM post-cloning script to change hostname and regenerate SSH keys
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <hostname>"
exit 1
fi
printf "Starting setup with hostname \033[1;32m$1\033[0m: continue [y/N]? "
while true; do
read -sn1 response
case "$response" in
[yY]) printf '\033[1;32my\033[0m\n'; break;;
[nN]|"") printf '\033[1;31mn\033[0m\n'; exit 0;;
esac
done
echo Regenerating SSHd keys…
rm /etc/ssh/ssh_host_* 2> /dev/null
echo Changing hostname…
sed -Ei "s/(\s{2,})(\S{1,}\s)?(localhost\s)/\1$1 \3/g" /etc/hosts
hostnamectl set-hostname $1
echo Restarting services…
systemctl restart network sshd
echo Deleting myself…
rm -- "$0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment