How to Change Hostname on Ubuntu 20.04?

A hostname is the unique name that identifies a device on a network. It allows other devices to locate and connect to it, making it an essential component of any network setup. Whether you're running a small home network or managing multiple servers in an enterprise environment, understanding how to change the hostname can come in handy.

In this article, we'll explore how to change the hostname on Ubuntu 20.04 using both the command line interface (CLI) and Ubuntu's graphical user interface (GUI).

Checking Current Hostname

Before changing the hostname, you first need to know what the current hostname is. This can be easily checked using the terminal.

Step 1: Open Terminal

To open the terminal, press Ctrl+Alt+T or search for "Terminal" in the Applications menu.

Step 2: Enter Command

Enter the following command into the terminal

hostname

This will display the current hostname of your Ubuntu system. The output should look something like this

my-hostname

You can also use the hostnamectl command for more detailed information

hostnamectl

Changing Hostname via Command Line

The command line interface (CLI) method is a quick and efficient way to change the hostname on Ubuntu 20.04. This method involves using a series of commands in the terminal.

Using hostnamectl Command

To change the hostname via CLI, open a terminal window and enter the command

sudo hostnamectl set-hostname new-hostname

Replace new-hostname with your desired hostname (without brackets). This command will update the hostname in multiple system files automatically.

Editing /etc/hosts File

After changing the hostname, you should also update the /etc/hosts file to ensure proper local hostname resolution

sudo nano /etc/hosts

Update the line that contains the old hostname to reflect the new one. For example

127.0.1.1    new-hostname

Save the changes by pressing Ctrl+X, then Y, and finally Enter.

Verify the Changes

To verify that the hostname has been changed successfully, run

hostnamectl

Changing Hostname via GUI

Ubuntu's graphical user interface provides a user-friendly way to change the hostname without using the command line. This method is ideal for users who prefer a visual approach.

To begin, click on the Activities button in the top left corner of your screen and search for Settings in the search bar. Once you have opened Settings, navigate to About which is found on the left side of the window.

Under Device name, click on the current hostname or look for a Rename button. You will be prompted to enter your administrative password. After entering it, you will be able to edit your device's hostname. Simply type in your desired hostname and click Rename to apply the changes.

Restarting Network Services

When you change the hostname on Ubuntu 20.04, it's important to restart network services to ensure that the changes are applied correctly. If you don't restart network services, your system may not be able to resolve hostnames properly.

Why Restarting Network Services is Important

The hostname is used by many network services to identify your device. After changing the hostname, network services such as SSH, HTTP, and Samba will have the old hostname cached until they are restarted or the system is rebooted.

Restarting Network Services

To restart network services on Ubuntu 20.04 after changing your hostname

sudo systemctl restart systemd-resolved

Alternatively, you can restart the networking service

sudo systemctl restart networking

Or simply reboot the system for all changes to take effect

sudo reboot

Troubleshooting Common Issues

Changing the hostname on Ubuntu 20.04 can sometimes present issues that may need troubleshooting. Here are common issues and their solutions.

Conflicts With Existing Hostnames

One common issue is conflicts with existing hostnames on the network. This can occur if there are multiple devices with the same hostname. To avoid conflicts, choose a unique hostname that hasn't been used before.

If you encounter a conflict, try changing the hostname again by adding a number or letter at the end to make it unique.

Issues With DNS Resolution

DNS resolution issues can occur after changing the hostname. To troubleshoot

  • Check if other devices can resolve your new hostname using nslookup newhostname

  • Ensure the /etc/hosts file contains the correct hostname mapping

  • Restart the DNS resolver service: sudo systemctl restart systemd-resolved

  • Flush DNS cache if needed: sudo systemd-resolve --flush-caches

Best Practices

When choosing a hostname, follow these best practices

  • Use only lowercase letters, numbers, and hyphens

  • Start with a letter and avoid ending with a hyphen

  • Keep it descriptive but concise (under 63 characters)

  • Avoid using special characters or spaces

Conclusion

Changing the hostname on Ubuntu 20.04 is a straightforward process that can be accomplished through both command line and GUI methods. The CLI method using hostnamectl is quick and efficient, while the GUI method provides a user-friendly alternative. Remember to restart network services after making changes and follow proper naming conventions for optimal network functionality.

Updated on: 2026-03-17T09:01:39+05:30

988 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements