Skip to main content

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

Screenshot 2022-09-28 220338.png

Use the following to set a new hostname for your system:

sudo hostnamectl set-hostname your_new_hostname

Screenshot 2022-09-28 221224.png

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

Screenshot 2022-09-28 222153.png

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:

Screenshot 2022-09-28 222847.png

Simply replace the old hostname in the file with the new hostname, and reboot your system.

Screenshot 2022-09-28 223340.png

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 script for changing hostname

#sets the exit condition for while loop
TEMP1=true

while $TEMP1
do
	#Asks user input for a name
    echo "Enter a new hostname:"
    read newhostname
    
    #Asks user to input it again
    echo "Enter the hostname again:"
    read newhostname2

	#If the two input matches, exit the while loop
    if [ "$newhostname" == "$newhostname2" ]
        then
            TEMP1=false
            
	#Otherwise, repeat the loop again
	else
        echo "Hostname did not match. Try Again."
    fi
done

#Use sed to change the previous HOSTNAME to the new hostname in /etc/hosts
sudo sed -i "s/$HOSTNAME/$newhostname2/g" /etc/hosts

#Change the system hostname to the new hostname
sudo hostnamectl set-hostname $newhostname2

#print the new hostname information
hostnamectl

Screenshot 2022-09-28 224600.png

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.