How to Change the Hostname in Ubuntu: A Step-by-Step Guide

Change the Hostname in Ubuntu

One of the first things to do after the installation of Ubuntu is to change the hostname. The hostname helps to identify and distinguish each device on a network. Changing the hostname in Ubuntu is a simple and quick process.

To change the hostname in Ubuntu, there are a few steps to follow. The first step is to open the terminal window and type the command “hostnamectl set-hostname your-new-hostname“. Replace “your-new-hostname” with the desired name for the device. Press Enter after the command.

After completing the command, it is advisable to verify that the hostname has actually changed. Type the command “hostname” in the terminal window and press Enter. The terminal window should display the new hostname. By following these steps, you can easily Change Hostname on Ubuntu devices.

Backup Your System

Before proceeding with the steps to change the hostname in Ubuntu, it is recommended to back up your system to prevent any data loss or system damage.

There are several methods to back up your system, including using the built-in backup tool in Ubuntu, creating a disk image, or copying important files to an external hard drive. Whichever method you choose, make sure to follow these best practices:

  • Verify that you have enough storage space to back up all the necessary files.
  • Choose a reliable backup location, such as an external hard drive or a cloud service.
  • Create a backup schedule and stick to it to ensure that your data is always up-to-date.
  • Test your backups regularly to make sure that you can restore your data if needed.

Backing up your system ensures that you have a copy of all your important files and system settings in case of an unforeseen event. It gives you peace of mind, knowing that your data is safe and secure.

Once you have completed the backup, you can safely proceed with the steps to change the hostname in Ubuntu without worrying about losing any files or data.

Edit the Hostname File

To change the hostname in Ubuntu, we need to edit the hostname file which contains the current hostname of the system.

  1. The first step is to open the terminal in Ubuntu. You can either use the shortcut key “Ctrl+Alt+T” or search for “terminal” in the Ubuntu dashboard.
  2. Once the terminal is open, type the following command to open the hostname file in the Nano text editor:sudo nano /etc/hostnameThis will prompt you to enter the root password for your system.
  3. After entering the password, you will see the contents of the hostname file. This file will contain the current hostname of your system.
  4. Edit the hostname according to your preference. Make sure that the new hostname is a single word, without any spaces or special characters.
  5. Once you have edited the hostname, press “Ctrl+O” to save the changes and “Ctrl+X” to exit the editor.
  6. Next, we need to edit the hosts file to make sure the system recognizes the new hostname. Type the following command in the terminal to open the hosts file in the Nano text editor:sudo nano /etc/hosts
  7. In the hosts file, you will see a line that starts with “” and is followed by the current hostname of your system. Edit this line to replace the current hostname with the new hostname that you just set in the hostname file.
  8. Save the changes by pressing “Ctrl+O” and exit the editor by pressing “Ctrl+X”.
  9. Finally, restart the system or the hostname service by typing the following command in the terminal:sudo systemctl restart hostnameNow your Ubuntu machine should be running with the new hostname that you set.

Changing the hostname of your machine can be useful when you want to make your machine more identifiable on a network. We hope this tutorial has been helpful in guiding you through the process of changing the hostname in Ubuntu.

Update the Hosts File

After changing the hostname, the next step is to update the hosts file. This file maps hostnames to IP addresses on the local Linux system. By default, Ubuntu looks for the hostname in the hosts file before checking DNS servers. If the hostname is not found in the hosts file, it will be resolved using DNS.

To update the hosts file, follow these steps:

  1. Open the hosts file using a text editor. In Ubuntu, you can use the nano text editor in the terminal by typing:
    sudo nano /etc/hosts
  2. Enter your system’s root password to gain access to the file.
  3. The hosts file should contain several lines. Look for the line that contains the old hostname and IP address. For example:   old-hostname
  4. Replace the old hostname with the new hostname. For example:   new-hostname
  5. Save the changes to the hosts file by pressing Ctrl+O, then Ctrl+X to exit nano.
  6. Verify that the changes were successful by pinging the new hostname. In the terminal, type:
    ping new-hostname

    If the hostname resolves to the correct IP address, then the changes were successful.

Updating the hosts file is an important step in the process of changing the hostname in Ubuntu. It ensures that the system can find itself using the new hostname, and enables other services to connect to the system using the updated name.

Restart Your System

After changing the hostname in Ubuntu, it’s important to restart your system to ensure that the changes take effect properly. Fortunately, this is an easy process that can be done quickly.

To restart your Ubuntu system, you can use the following command in the terminal:

sudo reboot

Once you enter this command, your system will begin the restart process. Depending on the speed of your computer and the number of open applications, this process can take a few moments.

After the restart is complete, you can confirm that your new hostname has been applied by opening a new terminal window and typing the following command:


This will display your current hostname. If the new hostname is displayed, congratulations! You have successfully changed the hostname in Ubuntu.

If you encounter any issues with the hostname not being applied, ensure that you followed all steps correctly and try restarting your system again.

It’s also worth noting that changing your hostname can affect other services and applications on your system that rely on the hostname. Be sure to check all affected services and update their configurations if necessary.

In summary, restarting your Ubuntu system is a crucial step in successfully changing the hostname. It’s a simple process that can be done quickly with the sudo reboot command. After the restart, ensure that the new hostname is properly applied and check all affected services for any necessary updates.


That’s how easy it is to change the hostname in Ubuntu. By following these simple steps, you can customize your device’s name to a more personalized one that suits your needs. Changing the hostname can help you identify your system quickly and reduce any confusion that arises when multiple systems are connected to the same network.

The command-line interface is a powerful tool and can be daunting for new users to understand and use effectively. However, with the right guidance, it can be very convenient, and you can accomplish a lot in a short time.

Remember that the hostname is an important identifier for your computer, and it must be unique if you are using it on a network. Avoid using a hostname that has already been taken by another computer in the same network.

In conclusion, changing the hostname in Ubuntu is a straightforward process that can be accomplished quickly by following the steps mentioned above. It’s a convenient process and can be handy in situations where you’re managing multiple computers in the same network. With these simple steps, you can customize your system’s name with ease.

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts