Skip to content

Instantly share code, notes, and snippets.

@imneonizer
Last active November 3, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imneonizer/28ea572b719b394c4e1867fe334c3d73 to your computer and use it in GitHub Desktop.
Save imneonizer/28ea572b719b394c4e1867fe334c3d73 to your computer and use it in GitHub Desktop.
# Check if sudo permission is available, else exit
if [ `id -u` != "0" ]
then
echo '[ERROR] please run with sudo'
exit
fi
# Retrieve new hostname from args
if [ -z "$1" ]
then
echo "usage: $0 HOSTNAME"
else
# Assign existing hostname to $hostn
hostn=$(cat /etc/hostname); newhost=$1
echo "[INFO] updating '$hostn' > '$1'"
sed -i "s/$hostn/$newhost/g" /etc/hosts
sed -i "s/$hostn/$newhost/g" /etc/hostname
hostnamectl set-hostname $newhost
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment