How to Change Hostname on Ubuntu 22.04

Learn to change your Ubuntu 22.04 system's hostname, a unique identifier, through this guide. Explore temporary and permanent methods like commands and config files. Copy

How to Change Hostname on Ubuntu 22.04

Introduction

The hostname is a unique name assigned to a computer or device connected to a network. It serves as an identifier and facilitates communication with other devices on the same network. In some cases, you may need to change the hostname of your Ubuntu system, such as when setting up a server or to better reflect the system's purpose. This tutorial will guide you through the process of changing the hostname on Ubuntu 22.04.


Prerequisites

Before proceeding, ensure that you have sudo privileges or are logged in as the root user. Changing the hostname requires administrative access.


Changing the Hostname Temporarily

If you need to change the hostname temporarily, you can use the hostname command without modifying any configuration files. However, this change will only persist until the next system reboot.

  1. Open the terminal.
  2. Check the current hostname by running:
hostname
  1. To change the hostname temporarily, use the following command, replacing new-hostname with your desired hostname:
sudo hostname new-hostname

Changing the Hostname Permanently

To change the hostname permanently, you need to modify the appropriate configuration files. Follow these steps:

  1. Open the terminal.
  2. Edit the /etc/hostname file using a text editor like nano:
sudo nano /etc/hostname
  1. In the text editor, replace the existing hostname with your desired new hostname.
  2. Save the file and exit the text editor.
  3. Next, edit the /etc/hosts file:
sudo nano /etc/hosts
  1. In this file, locate the line that starts with the IP address of your system (e.g., 127.0.1.1 or 127.0.0.1) and modify the existing hostname to the new one.
  2. Save the file and exit the text editor.
  3. After updating the configuration files, reboot your system for the changes to take effect:
sudo reboot
  1. After the system restarts, verify the new hostname by running:
hostname

You should see the new hostname displayed in the terminal.


Changing the Hostname Using the Hostnamectl Command

Ubuntu 22.04 provides the hostnamectl command, which simplifies the process of changing the hostname. This command allows you to modify both the static and transient hostname settings.

  1. Open the terminal.
  2. To change the static hostname, use the following command, replacing new-hostname with your desired hostname:
sudo hostnamectl set-hostname new-hostname
  1. If you want to change the transient (temporary) hostname, use the following command:
sudo hostnamectl set-hostname --pretty new-hostname

The --pretty option sets the pretty hostname, which is used for display purposes.

  1. Reboot your system for the changes to take effect:
sudo reboot
  1. After the system restarts, verify the new hostname by running:
hostnamectl

This command will display the current static and transient hostname settings.


Conclusion

Changing the hostname on Ubuntu 22.04 is a straightforward process that can be accomplished using the hostname command for temporary changes or by modifying the /etc/hostname and /etc/hosts files for permanent changes. Additionally, the hostnamectl command provides a convenient way to manage both static and transient hostname settings.

Regardless of the method you choose, make sure to update any relevant configuration files or services that rely on the hostname after making the change. A consistent and meaningful hostname can greatly enhance the manageability and organization of your Ubuntu system.


Enjoying our content? Your support keeps us going! 🚀

Consider buying us a coffee to help fuel our creativity.