Skip to content

Instantly share code, notes, and snippets.

@dbannik
Last active August 23, 2020 01:10
Show Gist options
  • Select an option

  • Save dbannik/a86eeb3bd1b5f7e79b0b529a61d5f917 to your computer and use it in GitHub Desktop.

Select an option

Save dbannik/a86eeb3bd1b5f7e79b0b529a61d5f917 to your computer and use it in GitHub Desktop.
Modify hostname
#!/usr/bin/env bash
newHostname=$1
if [[ "$newHostname" == "" ]];
then
echo "Please set 1 parameter"
echo "$0 hostname"
exit 1
fi
oldHostname=$(hostname)
hostnamectl set-hostname $newHostname
while read ip address;
do
hosts=''
for host in $address;
do
if [[ $host == "$oldHostname" ]];
then
host="$newHostname"
fi
hosts="$hosts $host"
done
sed -i "s/^$ip.*$/$ip$hosts/g" /etc/hosts
done < <(cat /etc/hosts | grep -vP "(^#|^$)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment