Skip to content

Instantly share code, notes, and snippets.

@chales
Created June 24, 2014 15:54
Show Gist options
  • Save chales/80d25a47427c5a7eb6b8 to your computer and use it in GitHub Desktop.
Save chales/80d25a47427c5a7eb6b8 to your computer and use it in GitHub Desktop.
Set ubuntu hostname via script using sed
#!/bin/bash
# Set $chostname to current hostname
chostname=$(cat /etc/hostname)
# Display current hostname
echo "Current hostname is '$chostname'"
# Set $newhostname as new hostname
echo "Enter new hostname: "
read newhostname
# Change the hostname value in /etc/hostname and /etc/hosts files
sudo sed -i "s/$chostname/$newhostname/g" /etc/hostname
sudo sed -i "s/$chostname/$newhostname/g" /etc/hosts
# Display new hostname
echo "Your new hostname is $newhostname"
# Reboot the system
read -s -n 1 -p "Press any key to reboot!"
sudo reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment