Last active
August 29, 2015 14:07
-
-
Save shichao-an/7c43e22c21c666f384ef to your computer and use it in GitHub Desktop.
Set hostname on Ubuntu
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 | |
# Set hostname on Ubuntu 12.04 and 14.04 | |
usage='Usage: set-hostname.sh HOSTNAME' | |
[ $# -ne 1 ] && { echo "$usage" >&2; exit 1; } | |
echo 'Updating hostname...' | |
if which hostnamectl > /dev/null | |
then | |
# Ubuntu 14.04 that ships with hostnamectl | |
hostnamectl set-hostname "$1" | |
else | |
echo "$1" > /etc/hostname | |
hostname "$1" | |
fi | |
echo 'Changing /etc/hosts...' | |
if grep '127\.0\.1\.1' /etc/hosts > /dev/null | |
then | |
sed -i "s/127\.0\.1\.1.*/127.0.1.1 $1/g" /etc/hosts | |
else | |
echo -e "127.0.1.1\t$1" >> /etc/hosts | |
fi | |
echo "Hostname settings have been changed to $1." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment