How to Change the Hostname in Ubuntu

Jimin
2 min readMar 7, 2024

--

During the installation process of Ubuntu, you might have chosen a hostname for your system. However, there could be instances where you need to change the hostname to better suit your preferences or requirements. In this guide, we’ll walk you through the steps to change the hostname in Ubuntu.

Step 1: Open a Terminal

First, you’ll need to open a terminal window. You can do this by pressing Ctrl + Alt + T on your keyboard or by searching for “Terminal” in the applications menu.

Step 2: Edit the Hostname File

In the terminal, enter the following command to edit the hostname file:

sudo nano /etc/hostname

Step 3: Change the Hostname

In the text editor that opens, you’ll see the current hostname. Simply replace it with your desired hostname. For example, if you want to change the hostname to “ubuntu,” edit the file to look like this:


ubuntu

Save the changes by pressing `Ctrl + X`, then `Y` to confirm, and `Enter` to exit the editor.

Step 4: Edit the Hosts File

Next, you’ll need to edit the hosts file. Enter the following command in the terminal:


sudo nano /etc/hosts

Step 5: Update Hosts File

In the text editor, you’ll see a line with the current hostname followed by “localhost.” Replace the current hostname with your new hostname (e.g., “ubuntu”). Save the changes and exit the editor.

127.0.0.1 localhost
127.0.1.1 ubuntu # Replace this line with your new hostname

Step 6: Apply Changes

After updating the hostname and hosts file, apply the changes by entering the following command in the terminal:


sudo hostnamectl set-hostname ubuntu

Step 7: Reboot

Finally, to ensure that the changes take effect, reboot your system by entering the following command:

sudo reboot

After rebooting, your Ubuntu system should now have the new hostname. You can verify the change by opening a terminal and running the command:


hostname

It should display your new hostname.

I hope this guide has been helpful in understanding how to change the hostname in Ubuntu. If you have any questions or feedback, feel free to leave a comment below.

--

--

Jimin

DevOps engineer and tech enthusiast. Sharing tech insights to simplify the complex. Let's connect on LinkedIn! https://www.linkedin.com/in/byun-jimin/