Last active
November 8, 2015 13:41
-
-
Save davidlonjon/28ff802be900fce8564a to your computer and use it in GitHub Desktop.
Update hostname
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 | |
# Update hostname | |
# by David Lonjon (@davidlonjon) | |
# | |
# The script update a machine hostname. | |
# reboot is required after running this script | |
# | |
# LICENSE: GPLv2+ | |
# | |
# INSTALLATION: | |
# Copy this script anywhere | |
# | |
# USAGE: | |
# ./update-hostname.sh | |
# | |
# EXAMPLES: | |
# N/A | |
# | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root" 1>&2 | |
exit 1 | |
fi | |
if [ -z "$1" ]; | |
then echo "Hostname is required" 1>&2 | |
exit 1 | |
fi | |
HOSTNAME=$1 | |
# Set the host name | |
hostname $HOSTNAME | |
HOSTNAME=${HOSTNAME//./-} | |
HOSTNAME=${HOSTNAME//_/-} | |
echo $HOSTNAME > /etc/hostname | |
sed -i "2i127.0.1.1 $HOSTNAME" /etc/hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment