How to Change Hostname on Ubuntu 18.04?


Introduction

A hostname is the unique identifier of a device in a network, typically used to identify and connect to other devices on the same network. In other words, it is a label given to a computer or device that enables it to be uniquely identified when connected to the internet or any other network. In most cases, when your device is connected to the internet or a local network, it gets assigned a hostname by default.

However, there are situations where you may need or want to change the hostname of your Ubuntu 18.04 machine. For instance, if you want your machine's hostname to reflect its function or location in the network for easier identification by other machines in the same network.

Checking Current Hostname

Using the Hostnamectl Command to Check Current Hostname

Before changing your Ubuntu 18.04 server's hostname, it's crucial to know its current name. This step will give you an overview of the system's current configuration and aid in debugging potential issues that may arise.

To check your system's current hostname, Ubuntu 18.04 comes with a pre-installed command-line tool named hostnamectl. To initiate the command, open a terminal window on your Ubuntu 18.04 server and type −

hostnamectl

This will display a list of information about your system including the current hostname under "Static hostname."

Explanation of What the Output Means

When running the hostnamectl command, three types of hostnames can be displayed; static, transient or pretty. The static and transient hostnames are regular hostnames that can be given explicitly to a machine (static) or dynamically by DHCP (transient).

The pretty name is simply a human-friendly version of either of them. The output includes information such as −

  • Static hostname − This is the name given to your system at install time.

  • Icon name − This describes how applications should refer to your computer in graphical environments.

  • Chassis type − This describes what type of machine you're running on.

  • Machine ID − This identifies your hardware uniquely for some application purposes.

  • Boot ID − This identifies which boot cycle you’re currently running on.

You are now equipped with knowledge on how to use hostnamectl command to check for Ubuntu 18.04 server’s current Hostname and an understanding of what each section means in context with each other.

Changing Hostname

Using the Hostnamectl Command to Change Hostname Temporarily

The hostnamectl command is the easiest and quickest way to change the hostname on Ubuntu 18.04. To use it, open a terminal window and enter the following command −

sudo hostnamectl set-hostname [new_hostname]  

Replace [new_hostname] with your desired new hostname.

Syntax and Options Available

The syntax of the hostnamectl command is straightforward. The only required parameter is the new hostname itself. However, there are other options that can be used in conjunction with this command, such as −

  • --pretty − This option sets a "pretty" name for the machine, which can be any descriptive string you like.

  • --static − This option sets the static hostname of the system.

  • It can be set to any valid static hostname.

  • --transient − This option sets a transient host name which means that it will not survive a reboot.

Explanation of how it Works

When you run this command, it immediately changes your computer's current hostname to your specified one for as long as you do not reboot or log out from your session. However, when you restart your Ubuntu system or log out from your session, Ubuntu reverts back to using its default generated hostname unless you take further action on making changes permanent.

Making Changes Permanent by Editing /etc/hostname File

To make changes made through "Using the Hostnamectl Command", this requires additional steps before restarting or logging out from an active session. These steps include editing two files - /etc/hostname and /etc/hosts −

  • Open a terminal window and use this command to open the file using nano or another editor −

sudo nano /etc/hostname  
  • Replace the default generated hostname with new_hostname.

Save and close the file.

  • Open the /etc/hosts file using a text editor −

sudo nano /etc/hosts  
  • Replace the old hostname with new_hostname on both lines of text, save, and close the file.

Syntax and Options Available

Editing /etc/hostname requires only that you replace a single line with your desired hostname. No additional parameters are required.

Editing /etc/hosts involves replacing each instance of the old hostname with your desired new hostname. This applies to both lines within this file.

Explanation of How it Works

By editing these two files, you are telling Ubuntu to use your specified hostname as its default from then on, even after restarting or logging out from your session. Now any commands or applications that rely on knowing your computer's name will use this configured name to connect and work accurately.

Verifying Changes Made

Once you have changed your hostname using either the temporary or permanent method, it is important to verify whether the changes have been made successfully. To do this, you can use the same command we used earlier to check the current hostname.

Checking new Hostname Using the Same Command Used in Step II

To check if your new hostname is now active, run the following command −

hostnamectl  

You should see an output similar to what you saw when checking the current hostname earlier. Look for the "Static hostname" field and verify that it displays your new desired hostname.

If you have made a mistake during any of the steps above, you may still see your old hostname displayed. If so, double-check that you have followed all steps correctly and try again.

Congratulations! You have successfully changed your hostname on Ubuntu 18.04!

Troubleshooting Common Issues

As with any system configuration, there can be issues that arise when changing the hostname on Ubuntu 18.04. Here are some common errors that users encounter when changing their hostname and solutions to resolve them −

Permission Denied Error

One common error users may encounter is a "permission denied" error message when trying to change the hostname using either method mentioned in the previous section. This occurs because changes to the /etc/hostname file require root privileges.

To fix this issue, run the command with sudo in front of it like this −

sudo hostnamectl set-hostname new-name. 

This will prompt you for your password before running the command with elevated privileges.

Incorrect Hostname Error

Sometimes, even after following all of the steps correctly, users might see an incorrect or old hostname appear instead of their desired one. This can happen if there is a cached version of your old name stored somewhere on your computer. To fix this issue, try restarting your computer or using the command sudo systemd-resolve --flush-caches to clear any outdated entries in DNS caches.

sudo systemd-resolve --flush-caches

No Network Connection After Changing Hostname

If you lose network connectivity after changing your hostname, it could be due to incorrect configuration settings on your local network or router. In this case, check that DHCP is enabled and functioning properly on your network equipment. To resolve this issue, ensure that all configurations are set according to manufacturer documentation or consult with an IT professional for further assistance.

Conclusion

Changing the hostname on Ubuntu 18.04 is a simple but essential task that can help improve security, organization, and network management. In this article, we have covered how to check the current hostname, change it temporarily or permanently using the appropriate commands and file editing tools, and verify that the changes were successful.

Updated on: 24-Aug-2023

365 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements