How to Change the hostname on Ubuntu 24.04LTS Without Rebooting

I am a PHP developer and regularly need to change the hostname on Ubuntu 24.04LTS without rebooting.

I need to do this because I clone a basic Ubuntu server so I can create differing installations without having to create everything from scratch.

It is very simple to permanently change the hostname on Ubuntu 24.04LS.

This will take 4 steps.

Let me show you how.

1) At the command line run the command : sudo hostnamectl set-hostname <new-hostname> –static The command hostnamectl set-hostname <new-hostname> –static is used to set the hostname for your system.

2) At the command line run the command : sudo vi /etc/hostname and change the hostname to the new hostname.

Since we are using vi for our editor, to exit press the ESC key followed by the colon key, then the “q” key, and press the enter key.

3) At the command line run the command : sudo vi /etc/hosts and set the host name to the new hostame.

4) At the command line run the command : sudo systemctl restart systemd-hostnamed . The command systemctl restart systemd-hostnamed restarts the systemd-hostnamed service. This service is responsible for managing the system’s hostname and related configuration.

To verify : run the command hostnamectl . You can also issue the command hostname to verify the hostname has been changed.

Notice the hostname on your SSH connection will be the prior hostname. To correct this logout and then log in again.

Conclusion

In this article we learned how to permanently change the hostname on a Ubuntu 24.04LTS server. This will work on a desktop machine as well.