3 Methods To Change the Hostname of Your Linux Machine

This tutorial explains 3 methods to change the hostname of your Linux machine.

Check the current hostname

You can use hostname command.


or use hostnamectl


change hostname

Change Hostname: Method1

Hostname is configured in /etc/hostname file. So we can manually open this file and change hostname there.

sudo nano /etc/hostname

Delete the current name in this file and enter a new hostname for your Linux box. Save and close this file.

Check hostname again with hostname or hostnamectl command and you will see hostname has changed. If you open a new terminal window, you will also notice the hostname has changed. The new hostname is preserved across system reboots with this method.

Update /etc/hosts file

Another thing we need to do is to update /etc/hosts file.

sudo nano /etc/hosts

Just replace the old hostname with the new hostname. Note that Linux is case sensitive. Then save and close this file.

change hostname

If you don’t update /etc/hosts file, some programs such as sudo don’t know how to resolve the new hostname.

If you change the hostname of a Internet-facing Linux server, the new hostname should resolve to public IP address. If you change the hostname of a Debian or Ubuntu home computer, then the new hostname should resolve to

The Debian installer create <host_name> entry for a system without a permanent IP address. For a system with a permanent IP address, should be replaced by the permanent IP address. uses the loopback interface. In fact, ( ~ is all bound to the loopback interface.

Method 2: Change Hostname with hostnamectl

sudo hostnamectl set-hostname <newhostname>

This command will remove the old hostname from /etc/hostname and put the new hostname in its place. As with the first method, we need to update /etc/hosts file.

Method3: Change Hostname Temporarily

To temporarily change hostname (lost after reboot), use hostname command:

sudo hostname <new-hostname>

This command won’t change the static hostname in /etc/hostname file. It only changes the transient hostname, so the new hostname won’t be preserved across reboots.

The static hostname is stored in /etc/hostname file.

Rate this tutorial
[Total: 5 Average: 4]

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here