Skip to content

Instantly share code, notes, and snippets.

@davidlonjon
Last active November 8, 2015 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidlonjon/28ff802be900fce8564a to your computer and use it in GitHub Desktop.
Save davidlonjon/28ff802be900fce8564a to your computer and use it in GitHub Desktop.
Update hostname
#!/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