Last active
August 23, 2020 01:10
-
-
Save dbannik/a86eeb3bd1b5f7e79b0b529a61d5f917 to your computer and use it in GitHub Desktop.
Modify hostname
This file contains hidden or 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 | |
| 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