How To Change The Hostname Without Restart On Ubuntu 24.10 and Ubuntu 24.04

In this article I will show you how to change the hostname of your Ubuntu 24.10 or Ubuntu 24.04 without having to restart the machine.

How to Change Hostname on Ubuntu 24.10 And Ubuntu 24.04 Without Restart

Changing the hostname of your Ubuntu system without requiring a restart is a straightforward process. Ubuntu 24.10, like other recent versions, allows you to make this change dynamically and permanently. Here’s how you can achieve this:

Check the Current Hostname

Before making any changes, it is good practice to check the current hostname of your system. Open a terminal and type:

hostnamectl

Output:

geekvater@geekvater:~$ hostnamectl
Static hostname: geekvater
Icon name: computer-vm
Chassis: vm 🖴
Machine ID: f8a55f67fc0842e7be8bcfd4b7d07f93
Boot ID: 0338b211e4e5412b94876dcecc82b2f9
Virtualization: oracle
Operating System: Ubuntu 24.10
Kernel: Linux 6.11.0-13-generic
Architecture: x86-64
Hardware Vendor: innotek GmbH
Hardware Model: VirtualBox
Firmware Version: VirtualBox
Firmware Date: Fri 2006-12-01
Firmware Age: 18y 1month 6d

This command displays the current hostname along with other system information. In my scenario, the hostname is geekvater, but I will change this to newhostname, just for this article.

Change the Hostname Temporarily

To change the hostname temporarily (until the next reboot), use the hostnamectl command as follows:

sudo hostnamectl set-hostname newhostname

Replace newhostname with the desired hostname. This change takes effect immediately but is not persistent across reboots.

Optional: This already updates the hostname in the /etc/hostname file, so you don’t need to do anything here. Just to be sure, you can view the file:

cat /etc/hostname

Change the hostname permanently, without reboot:

2. /etc/hosts

Edit the /etc/hosts file to associate the new hostname with the loopback address:

sudo vim /etc/hosts

Locate the line containing the old hostname (it should look something like this):

127.0.1.1    geekvater

Replace the old hostname (in my case geekvater) with your new hostname (in my case newhostname), so it looks like:

127.0.1.1    newhostname

Save the file and exit the editor.

Step 4: Verify the Change

To ensure the new hostname is applied, use:

hostnamectl

Output:

Static hostname: newhostname
Icon name: computer-vm
Chassis: vm 🖴
Machine ID: f8a55f67fc0842e7be8bcfd4b7d07f93
Boot ID: 0338b211e4e5412b94876dcecc82b2f9
Virtualization: oracle
Operating System: Ubuntu 24.10
Kernel: Linux 6.11.0-13-generic
Architecture: x86-64
Hardware Vendor: innotek GmbH
Hardware Model: VirtualBox
Firmware Version: VirtualBox
Firmware Date: Fri 2006-12-01
Firmware Age: 18y 1month 6d

This command should now display the updated hostname. Additionally, check that the hostname is resolved correctly:

ping -c 1 newhostname

Output:

NG newhostname (127.0.1.1) 56(84) bytes of data.
64 bytes from newhostname (127.0.1.1): icmp_seq=1 ttl=64 time=0.014 ms

--- newhostname ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.014/0.014/0.014/0.000 ms

Next, in order to see the new hostname (in my case newhostname) in your terminal prompt, type:

bash

This will load the shell again and so, your new hostname will be displayed!

Note:

If you are running any services that rely on the hostname, such as web servers or database servers, you may need to restart those services for the change to take effect.

By following these steps, you can change the hostname on Ubuntu 24.10 and Ubuntu 24.04 dynamically and persistently without needing to restart your system.

If you want to elevate your commnad-line interface skills, you might also be interested in:

Show 2 Comments

2 Comments

Leave a Reply

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