Skip to main content

How to change hostname in Ubuntu 24.04

4 minutes


In Linux, the hostname refers to the name assigned to a computer or device on a network. It's used to identify the system within a network environment. The hostname can be a simple and single-word name, which may include domain details thereby forming a complete fully qualified domain name (FQDN).

There can be various compelling reasons why you want to change the hostname in any Linux distro. The prominent reason to update hostname in a Linux distribution is security and identification of system especially in larger network environments.

Network Configuration: If you're setting up a new server or device on a network, assigning a unique hostname will reflects its purpose or role within the network. This can help with identification of the machine, especially in larger network environments.

Naming Conventions: Organizations follow the naming conventions for their servers and devices. Changing the machine name to adhere to these conventions will make the maintain task consistent and also make it easier to manage and identify systems within the organization.

Security: Changing the machine name in Ubuntu or Debian Linux can be part of security best practices. It can help obscure information about the system's identity, making it slightly more difficult for attackers to gather information about the network architecture.

This blog post will show you exactly how to change hostname in Ubuntu or Debian Linux distribution.

Prerequisites

To update or change hostname on Ubuntu or Debian, ensure you meet the following requirements.

  • A running local or cloud instance of Ubuntu or Debian Linux.
  • SSH access to the server with sudo privilege.

 

 

Change hostname permanently without reboot

The easiest way to change the hostname permanently and without reboot is by using the hostnamectl set-hostname command. This command is part of systemd utility and is installed by default. If you don't find the hostnamectl command in your system then use the following apt command to install it.

$ sudo apt install systemd

Execute the following hostnamectl command in the terminal to view the hostname in your system.

$ hostnamectl 
 Static hostname: kubelynx
 Icon name: computer-laptop
 Chassis: laptop
 Machine ID: ec5ad7d7f3274dcdb975b4128bbd3682
 Boot ID: e04305e7c00a48a3852688b4b7fd96f4
 Operating System: Ubuntu 22.04.4 LTS              
 Kernel: Linux 6.5.0-35-generic
 Architecture: x86-64
 Hardware Vendor: Lenovo
 Hardware Model: IdeaPad 3 15IGL05

You can verify from the output of the above command that the permanent hostname is set as the static hostname. To permanently change the system’s hostname, execute the following hostnamectl command:

$ sudo hostnamectl set-hostname cluster-node-1

The above command will not emit any output. To verify the new hostname, execute the hostnamectl command from the terminal without any options:

$ hostnamectl
hostnamectl command
 

Optionally, you can also change the pretty hostname of the system which is presented to the user. A computer system in a network is always identified by its static hostname. To change the pretty hostname, run the hostnamectl command with --pretty switch.

$ sudo hostnamectl set-hostname "pretty-hostname" --pretty
Pretty hostname Ubuntu
 

Change hostname temporarily

If you need to temporarily update or change the hostname on Ubuntu or Debian Linux, you can do that by leveraging the hostname command.  Run the following command to update the hostname by replacing new-hostname with your name.

$ sudo hostname new-hostname

The above command will not produce any output. To confirm the hostname of the system changed, run the hostname command.

$ hostname
new-hostname

The changes applied by the hostname command is temporary. Once you reboot the system, the hostname will be reverted to the old hostname.

The hostname command is useful when you have a minor task that requires a temporary change in the hostname, but don’t want to make it permanent.

Change hostname manually(Reboot needed)

In case you are working with cloud instances, It is possible to change the hostname by editing a few configuration files. To edit hostname manually,  update the hostname in two configuration files namely /etc/hostname and /etc/hosts.

To start with, edit the file /etc/hostname and update the hostname:

$ vi /etc/hostname 
new-hostname

Next update the hostname with a new hostname in /etc/hosts file so that the system is resolved with the new hostname in a network.

$ vi /etc/hosts
...
...
127.0.0.1 new-hostname
...
...
 

Once you updated the hostname in the above mentioned files, you still need to edit a cloud configuration file. This cloud-init package is installed from the default images provided by a few cloud service providers to initialize a cloud instance. 

For example, to permanently change or update hostname in a Linode cloud instance, you need to edit a cloud configuration file(cloud.cfg) and change the value of preserve_hostname to true.

$ vi /etc/cloud/cloud.cfg
...
...
preserve_hostname: true
...
...
 

Skip the above step if the above cloud configuration file is not present in your system.

Reboot the system. To verify that the hostname is indeed preserved during reboot just type hostname in the terminal:

$ hostname
new-hostname

 

Conclusion

These are a few ways to change the hostname using CLI.  You can now update the hostname of your system using any one of the methods described above. Moreover, You can also proceed with changing the hostname using any GUI tool if a desktop environment is present in the system.

fivestar_rating
No votes yet
Comments