hostnamectl - Change the current hostname
The command hostnamectl can be use to quickly change the hostname of the Linux host.
Running hostnamectl will give you the current hostname of the system:
hostnamectl
Use the following to set a new hostname for your system:
sudo hostnamectl set-hostname your_new_hostname
If you run a command with sudo after setting the new hostname, you may see the following warning in the terminal:
sudo: unable to resolve host test: No address associated with hostname
This can be easily solve by editing the host file in /etc/host . When you open the host file, you will see that the new hostname is not in the file:
Simply replace the old hostname in the file with the new hostname, and reboot your system.
Here's a simple Bash script that I wrote that can be use to change the hostname, and can be combine with other scripts to create a simple initial setup script for Linux installs:
#!/bin/bash
#This is a simple initial setup script for changing hostname
TEMP1=true
while $TEMP1
do
echo "Enter a new hostname:"
read newhostname
echo "Enter the hostname again:"
read newhostname2
if [ "$newhostname" == "$newhostname2" ]
then
TEMP1=false
else
echo "Password did not match. Try Again."
fi
done
sudo sed -i "s/$HOSTNAME/$newhostname2/g" /etc/hosts
sudo hostnamectl set-hostname $newhostname2
hostnamectl
While you may see the "unable to resolve host" line from the above script, if you check the /etc/hosts file, the script actually have successfully change the hostname in there. Simply reboot and the new hostname will take effect.