Skip to content

Instantly share code, notes, and snippets.

@voxxit
Last active August 29, 2015 14:08
Show Gist options
  • Save voxxit/4891917f9ac0bcb90620 to your computer and use it in GitHub Desktop.
Save voxxit/4891917f9ac0bcb90620 to your computer and use it in GitHub Desktop.
Script to change hostname on Ubuntu >= 13.04
#!/bin/bash
#
# Example:
#
# \curl -fsSL http://srv.im/change-hostname.sh | sudo bash /dev/stdin hostname.example.com
_hostname="$1"
_dnsline="127.0.1.1\t$1"
hostnamectl set-hostname ${_hostname}
if grep -q 127.0.1.1 /etc/hosts
then
sed -i "s/127.0.1.1.*/${_dnsline}/g" /etc/hosts
else
echo -e ${_dnsline} >> /etc/hosts
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment