How to Change Hostname in Ubuntu 24.04

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.

Frequently Asked Questions

How do I check my current hostname in Ubuntu 24.04?

You can check your current hostname by running the command 'hostnamectl' in the terminal. This will display the static hostname along with other system information.

What command do I use to change the hostname in Ubuntu 24.04?

To change the hostname in Ubuntu 24.04, use the command 'sudo hostnamectl set-hostname new-hostname'. Replace 'new-hostname' with your desired hostname.

Do I need to update the /etc/hosts file after changing the hostname?

Yes, after changing the hostname, you should manually update the /etc/hosts file to reflect the new hostname. This ensures that the system correctly associates the new hostname with the local IP address.

Is it necessary to restart my device after changing the hostname?

While it's not always necessary, it is recommended to restart your device after changing the hostname to ensure that all services recognize the new name. You can do this by running 'sudo reboot'.

Why is it important to have a distinct hostname for my Ubuntu machine?

Having a distinct hostname is crucial for better identification and management of devices on a network. It helps avoid confusion, especially when managing multiple systems, and facilitates easier communication between devices.

Categories:

Leave a Reply

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

Exit mobile version