Skip to content

Instantly share code, notes, and snippets.

@cies
Created February 21, 2013 12:47
Show Gist options
  • Save cies/5004515 to your computer and use it in GitHub Desktop.
Save cies/5004515 to your computer and use it in GitHub Desktop.
Upstart script to set domain and host name in case they are reset on reboots (OpenVZ VPSes sometimes have that problem).
# hostname - set system hostname
#
# This script sets the hostname in case it is not set properly.
# This may be the case with OpenVZ VPSes that the provider is not willing to
# provide you a proper hostname for. Annoying but not unfixabe.
#
# Before using this script make sure that the first stanza in /etc/hosts that
# gives names to the node's primary IP address look as follows:
#
# 123.123.123.123 fully.qualified.name.com shortname
#
# This script will pick that up and set the domain and host name accordingly.
description "set system hostname"
start on net-device-up
task
script
# parse the IPv4 address from the ifconfig stanzas
primary_ip=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
# get the FQDN from /etc/hosts
fqdn=$(getent hosts $primary_ip | awk '{print $2}')
# set the domain and host name from the FQDN
/bin/domainname $(echo $fqdn | cut -d. -f2-)
hn=$(echo $fqdn | cut -d. -f1)
echo $hn > /etc/hostname
/bin/hostname $hn
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment