How to change hostname in Ubuntu

The hostname is a label assigned to a network device that helps identify devices uniquely within a network. Like in any other Linux distribution, changing the hostname is a straightforward process in Ubuntu. This Answer shows how to change the hostname in Ubuntu.

Reasons to change the hostname

Changing the hostname serves various purposes:

  1. Identification: A meaningful hostname helps identify the machine on a network.

  2. Organization: In enterprise environments, systems might need to adhere to specific naming conventions.

  3. Troubleshooting: Having a distinct hostname makes troubleshooting network issues easier.

  4. Customization: Giving your system a unique name can also add custom flavor to your device.

Changing the hostname

For demonstration purposes, you’ll use the terminal below to execute the commands:

Terminal 1
Terminal
Loading...

Note: You can’t restart and apply your changes on the terminal above. It’s there only for demonstration purposes. It’s recommended that you execute these commands on your own Ubuntu environment.

Here are the steps to change the hostname:

  1. Before changing the hostname, you should check the current hostname. To do that, type the following command in the terminal:

hostname
Command to check the current hostname of the device

This command will display your current hostname. In the terminal above, it should display educative.

  1. To change the hostname, you need to edit the /etc/hostname file. You can do this using the Nano text editor:

nano /etc/hostname
Command to edit the file using Nano

This command will open the /etc/hostname file in the Nano text editor. You’ll see your current hostname in this file. Simply replace it with the new hostname of your choice. After making the changes, press “Ctrl + O” to save the file, and then press the “Enter” key. To exit Nano, press the “Ctrl + X” keys.

  1. You need to update the /etc/hosts file to reflect the new hostname:

nano /etc/hosts
Command to edit the file using Nano

You’ll see a line that starts with 127.0.0.1. At the end of this line, you’ll find your current hostname. Replace it with the new hostname. Save the file and exit Nano.

  1. You must restart the system or the hostname service to apply the changes. You can restart the service by typing the following command in the terminal:

sudo reboot
Command to restart the system

Note: This command won’t work in the terminal above as it doesn’t save the changed files when we reboot it. It would be better to execute this step locally to check its execution.

  1. Once the system has restarted, type the following command to verify that the hostname has been changed:

hostname
Command to check the current hostname of the system

You should see your new hostname displayed in the following way:

Output on Ubuntu after changing the hostname
Output on Ubuntu after changing the hostname

Now,, you can easily change the hostname of your terminal in Ubuntu and use it as necessary!

Copyright ©2024 Educative, Inc. All rights reserved