How to Change Hostname in Ubuntu 24.04

This article provides instructions on changing the hostname in Ubuntu 24.04. It emphasizes the importance of unique hostnames for device identification on a network. Users can change the hostname using the `hostnamectl` command and must also update the “/etc/hosts” file. A system reboot is required for changes to take effect.

This article explains how to change the hostname on Ubuntu 24.04.

A hostname is a human-readable label that identifies a device on a network, distinguishing one machine from another and making the communication and management of devices easier.

Using distinct hostnames when managing multiple systems improves identification. Modifying the hostname can assist when systems are deployed or relocated.

To change the hostname in Ubuntu, you typically use the command line or edit configuration files based on your version of Ubuntu.

Show the current hostname

To view the current hostname on Ubuntu before making any changes or updates, execute the following commands.

hostnamectl

This should display text similar to the lines below and a hostname for your Ubuntu computer.

 Static hostname: srv1.ubuntu.local
Icon name: computer-vm
Chassis: vm 🖴
Machine ID: f95989a019994920b26e3b317b0d3f27
Boot ID: 3c115a8be03d4a0194ca6cd9fe488e2d
Virtualization: vmware
Operating System: Ubuntu 24.04.2 LTS
Kernel: Linux 6.11.0-19-generic
Architecture: x86-64
Hardware Vendor: VMware, Inc.
Hardware Model: VMware Virtual Platform
Firmware Version: 6.00
Firmware Date: Thu 2020-11-12
Firmware Age: 4y 4month 1w 1d

Change the hostname

Once you know your Ubuntu machine’s hostname, you can easily change or rename it using a simple command.

To change the hostname on Ubuntu, use the `hostnamectl` command.

For example, to change the device hostname to srv1.ubuntu.local, you will run the command below.

sudo hostnamectl set-hostname srv1.ubuntu.local

The command above will replace the device name in the [/etc/hostsname] file to reflect the new name.

/etc/hostname

The [/etc/hosts] file won’t be updated to the hostname. You may have to edit it to replace the hostname there as well.

sudo nano /etc/hosts

Then, replace the old name with the new one, save the file, and exit.

127.0.0.1 localhost
127.0.1.1 srv1.ubuntu.local

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback

After changing the hostname, you may have to restart the device. Run the command below to do that.

sudo reboot

When you log back in, the system should have a new name you assigned.

That should do it!

Conclusion:

  • Changing the hostname in Ubuntu 24.04 is a straightforward process using the hostnamectl command.
  • Always check your current hostname using hostnamectl before making changes.
  • Update both the /etc/hostname and /etc/hosts files to ensure the hostname is configured correctly.
  • Restart your device to apply the changes effectively.
  • Using clear and distinct hostnames is essential for better network management and identification of devices.
Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *