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:

NEW_HOSTNAME=john-pc

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

sudo sed -i -e "s/127.0.1.1.*/127.0.1.1 $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.