Home  »  ArticlesGuidesTechnology   »   How to Find and Permanently Change Hostname in Ubuntu Terminal

How to Find and Permanently Change Hostname in Ubuntu Terminal

You do not need to do much to permanently change hostname in Ubuntu. To find out what your hostname is with the Ubuntu terminal. In Desktop environments you need to go to the Applications menu and click on Accessories / Terminal.

When the terminal window opens you will be able to visually spot the hostname in two places. The first will be on the Title bar of the window while the other will be directly before the command-line prompt. In both cases, your hostname displays after your username in the format username@hostname.

If you want to unnecessarily drill down further, Ubuntu like any other Linux distro provides a command to print out the hostname. Simply type:

$ hostname

You can also display the current hostname with additional information by simply entering the following command:

$ hostnamectl

You can change the hostname using hostnamectl by running the following command:

$ sudo hostnamectl set-hostname NewHostname

How to Permanently Change Hostname and Hosts Files

If you are using Ubuntu Desktop and its derivatives you can simply go to Settings (the gear icon) or About this Computer. If you had to open Settings, scroll down to where it says Details and click on that.

Doing this should launch the About screen. Go to the editable text box labeled Device name. Type in the new hostname you would like into that field and press Enter to change it.

Edit Hostname Using the Command-line

With the Terminal window open, type in the following command and enter:

$ sudo hostname new-name

This command will change the hostname temporarily or until your restart your computer.

To make the change permanent you will need to edit the hostname file. Open the hostname file using this command:

$ sudo nano /etc/hostname

The file should contain nothing other than the hostname in the first line.

One more step. You also need to change the hostname in one other place, the hosts file. Run the following command to open the hosts file:

$ sudo nano /etc/hosts

With part of the output looking something like this after editing:

127.0.0.1 localhost
127.0.1.1 NewHostname

The /etc/hosts resolve an IP address into a hostname.

Go to the line with the old hostname and change it to the new one without altering any other part of the file then save and close the file. Once you change it in both of these locations, you can safely restart your computer and the new hostname will remain permanent.

If your changes revert to the old hostname it could be because of cloud-init.

About cloud-init

If you have the cloud-init package, a service used to initiate cloud services installed, you can also edit the /etc/cloud/cloud.cfg file. Your system may or may not have the /etc/cloud/cloud.cfg file. You can always confirm by entering:

$ cd /etc/cloud

and then type ls to display the files in that location. If the file is there enter the command to edit the file:

$ sudo nano /etc/cloud/cloud.cfg

Find and set the parameter “preserve_hostname” from “false” to “true” then save.

That’s it, you should now be able to permanently change hostname on your Ubuntu or any Linux system.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Available under:
Articles, Guides, Technology