Change Hostname in Linux

Change Hostname in Linux

A hostname is a unique identifier that is assigned to a device (host) connected to the network. It can distinguish one device from another on a specific network. This tutorial shows how to change hostname in the Linux. Provided method doesn't require rebooting system.

Assign new hostname to variable:


Use the sed command to find the line that contains IP address and replace the old hostname with the new one.

sudo sed -i -e "s/*/ $NEW_HOSTNAME/" /etc/hosts

Run the hostnamectl command to change hostname:

sudo hostnamectl set-hostname $NEW_HOSTNAME

To change hostname in Bash prompt of currently logged user, switch to current user:

sudo su - $USER

Leave a Comment

Cancel reply

Your email address will not be published.