Skip to content

Instantly share code, notes, and snippets.

@Remiii
Last active December 16, 2015 17:49
Show Gist options
  • Save Remiii/5473109 to your computer and use it in GitHub Desktop.
Save Remiii/5473109 to your computer and use it in GitHub Desktop.

How to custom Hostname

$ sudo ./customHostname.sh myHostname myDomain myIP

Sample

$ sudo ./customHostname.sh vito remibarbe.fr 45.132.12.35

Authors

Rémi Barbe (Remiii)

#!/bin/bash
if [ ${#} == 3 ]
then
echo "Updating..."
MY_HOSTNAME=${1}
MY_DOMAIN=${2}
MY_IP=${3}
echo "Hostname: "${MY_HOSTNAME}
echo "DOMAIN: "${MY_DOMAIN}
echo "IP: "${MY_DOMAIN}
# Set the hostname
sudo hostname ${MY_HOSTNAME}
sudo echo ${MY_HOSTNAME} > /etc/hostname
# Add fqdn to hosts file
sudo cat<<EOF > /etc/hosts
# This file is automatically genreated by customHostname.sh script
127.0.0.1 localhost
${MY_IP} ${MY_HOSTNAME}.${MY_DOMAIN} ${MY_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
ff02::3 ip6-allhosts
EOF
else
echo "Bad number of arguments"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment