Skip to content

Instantly share code, notes, and snippets.

@smd1000
Created March 2, 2017 18:04
Show Gist options
  • Save smd1000/d95144901bac2bf6a898853b7969b136 to your computer and use it in GitHub Desktop.
Save smd1000/d95144901bac2bf6a898853b7969b136 to your computer and use it in GitHub Desktop.
change_hostname
#!/usr/bin/env bash
OLD_HOSTNAME="$( hostname )"
NEW_HOSTNAME="$1"
if [ -z "$NEW_HOSTNAME" ]; then
echo -n "Please enter new hostname: "
read NEW_HOSTNAME < /dev/tty
fi
if [ -z "$NEW_HOSTNAME" ]; then
echo "Error: no hostname entered. Exiting."
exit 1
fi
echo "Changing hostname from $OLD_HOSTNAME to $NEW_HOSTNAME..."
hostname "$NEW_HOSTNAME"
sed -i "s/HOSTNAME=.*/HOSTNAME=$NEW_HOSTNAME/g" /etc/sysconfig/network
if [ -n "$( grep "$OLD_HOSTNAME" /etc/hosts )" ]; then
sed -i "s/$OLD_HOSTNAME/$NEW_HOSTNAME/g" /etc/hosts
else
echo -e "$( hostname -I | awk '{ print $1 }' )\t$NEW_HOSTNAME" >> /etc/hosts
fi
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment