Last active
August 29, 2015 14:06
-
-
Save deanrather/2b26a32a53081ff4cf24 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Changes your machine's hostname | |
# Usage: bash <(wget -O - http://git.io/Kbt8oQ) MY_HOSTNAME | |
# | |
old=$(hostname) | |
new=$1 | |
# Backup old hosts file | |
date=$(date +%F_%T | sed 's/:/-/g') | |
sudo cp /etc/hosts{,.backup-$date} | |
# Set hosts file with both old and new hostname | |
sudo bash -c "cat > /etc/hosts" <<EOF | |
127.0.0.1 localhost | |
127.0.1.1 $new # Current Hostname | |
127.0.1.1 $old # Previous Hostname | |
# The following lines are desirable for IPv6 capable hosts | |
::1 ip6-localhost ip6-loopback | |
fe00::0 ip6-localnet | |
ff00::0 ip6-mcastprefix | |
ff02::1 ip6-allnodes | |
ff02::2 ip6-allrouters | |
EOF | |
# Set hostname | |
sudo bash -c "cat > /etc/hostname" <<EOF | |
$new | |
EOF | |
# Reload hostname | |
sudo service hostname start | |
# Set hosts file with only new hostname | |
sudo bash -c "cat > /etc/hosts" <<EOF | |
127.0.0.1 localhost | |
127.0.1.1 $new | |
# The following lines are desirable for IPv6 capable hosts | |
::1 ip6-localhost ip6-loopback | |
fe00::0 ip6-localnet | |
ff00::0 ip6-mcastprefix | |
ff02::1 ip6-allnodes | |
ff02::2 ip6-allrouters | |
EOF | |
echo "Hostname changed to: $new" | |
echo "Reboot recommended" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment