Created
March 2, 2017 18:04
-
-
Save smd1000/d95144901bac2bf6a898853b7969b136 to your computer and use it in GitHub Desktop.
change_hostname
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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