How to Change the Hostname in Ubuntu: Temporary, Permanent, and Netplan Configuration Guide

1. Introduction

Why Change the Hostname in Ubuntu?

The hostname is a crucial element for identifying machines within a system or network when managing servers or virtual machines. Especially in corporate and cloud environments where multiple servers or virtual machines are operated, having a clear and meaningful hostname directly contributes to operational efficiency and ease of management. Hostname modification is also commonly required when migrating servers or making changes to the existing environment.

This article explains how to temporarily change the hostname in Ubuntu, how to make permanent changes that persist after reboot, and how to configure network settings using Netplan.

2. How to Check the Current Hostname

Command to Verify the Hostname

The following command is the most basic method to check the currently configured hostname:

hostname

This command displays the current hostname. If you want more detailed system information, use the hostnamectl command as shown below:

hostnamectl

This command displays not only the hostname but also detailed system information, producing an output similar to the example below:

Static hostname: my-hostname
Operating System: Ubuntu 20.04 LTS

With this, you have successfully confirmed the hostname.

3. How to Temporarily Change the Hostname

Using the hostname Command

To temporarily change the hostname, use the hostname command. This change reverts after a reboot, making it suitable for short-term testing or work on virtual machines.

sudo hostname new-hostname

For example, to temporarily change the hostname to temp-hostname, run the following command:

sudo hostname temp-hostname

Verifying the Temporary Change

To confirm that the hostname was changed correctly, run the hostnamectl command again:

hostnamectl

This allows you to verify that the change has been applied successfully. However, because the hostname reverts after reboot, proceed to the next steps if you need a permanent change.

4. How to Permanently Change the Hostname

Using the hostnamectl Command

The most recommended way to permanently change the hostname is by using the hostnamectl command. This method ensures the hostname remains even after rebooting the system.

sudo hostnamectl set-hostname new-hostname

For example, to change the hostname to my-new-hostname, execute the following:

sudo hostnamectl set-hostname my-new-hostname

Editing the /etc/hostname File Directly

Another permanent method involves manually editing the /etc/hostname file.

  1. Open the /etc/hostname file with a text editor.
sudo nano /etc/hostname
  1. Replace the current hostname with the new one.
my-new-hostname
  1. Save the file, exit the editor, and reboot the system.
sudo reboot

Editing the /etc/hosts File

When changing the hostname, do not forget to update the /etc/hosts file as well. This file maps hostnames to IP addresses.

127.0.1.1 my-new-hostname

This ensures that the hostname is correctly recognized across the network.

5. Changing Hostname and Network Settings with Netplan

What Is Netplan?

Netplan is a tool for managing network configurations in Ubuntu. It is especially recommended in server and virtual machine environments. Netplan helps automate network configuration in cloud-based or large-scale environments, and enables unified management of hostname and network settings—particularly useful in complex network scenarios.

Changing Hostname and Network Settings with Netplan

  1. Edit the Netplan configuration file.
sudo nano /etc/netplan/50-cloud-init.yaml
  1. Add the hostname and network configuration to the file.
network:
    ethernets:
        ens33:
            addresses:
            - 192.168.1.100/24
            gateway4: 192.168.1.1
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4
    version: 2
    hostname: my-new-hostname
  1. Apply the Netplan configuration using the following command:
sudo netplan apply

Troubleshooting

If an error occurs when applying Netplan settings, use the following command to display debug information and identify issues:

sudo netplan --debug apply

If an error message appears, it is likely caused by syntax errors or misconfigured network settings, so review the configuration file carefully. Creating a backup of the original configuration before making changes is recommended.

6. Security Considerations

Changing the hostname can affect SSH connections and firewall settings, so verifying proper security configurations is essential. After modifying the hostname, ensure that the /etc/hosts file and firewall rules reflect the correct settings. Additionally, confirm that SSH access remains functional, and update related configurations if necessary.

7. Summary

Ubuntu provides two methods for changing the hostname: temporary and permanent. Use the hostname command for temporary changes and hostnamectl for permanent ones. Managing configuration through Netplan is also beneficial, especially in complex network environments.

After changing the hostname, always review the /etc/hosts file, firewall rules, and SSH settings to ensure the system operates correctly.

侍エンジニア塾