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.
Changing the hostname serves various purposes:
Identification: A meaningful hostname helps identify the machine on a network.
Organization: In enterprise environments, systems might need to adhere to specific naming conventions.
Troubleshooting: Having a distinct hostname makes troubleshooting network issues easier.
Customization: Giving your system a unique name can also add custom flavor to your device.
For demonstration purposes, you’ll use the terminal below to execute the commands:
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:
Before changing the hostname, you should check the current hostname. To do that, type the following command in the terminal:
hostname
This command will display your current hostname. In the terminal above, it should display educative
.
To change the hostname, you need to edit the /etc/hostname
file. You can do this using the Nano text editor:
nano /etc/hostname
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.
You need to update the /etc/hosts
file to reflect the new hostname:
nano /etc/hosts
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.
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
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.
Once the system has restarted, type the following command to verify that the hostname has been changed:
hostname
You should see your new hostname displayed in the following way:
Now,, you can easily change the hostname of your terminal in Ubuntu and use it as necessary!