The hostname identifies your server on a network and appears in shell prompts, log files, and monitoring dashboards. Ubuntu 24.04 LTS uses hostnamectl to view and change it persistently.

Tested and valid on:

  • Ubuntu 24.04 LTS

Prerequisites

  • Ubuntu 24.04 LTS server
  • A user with sudo privileges

Step 1 – Check the Current Hostname

Display the full hostname configuration:

hostnamectl

Step 2 – Set a New Static Hostname

Replace new-hostname with your desired name:

sudo hostnamectl set-hostname new-hostname

Step 3 – Update /etc/hosts

Ensure the new hostname resolves locally:

sudo nano /etc/hosts

Update the 127.0.1.1 line:

127.0.1.1   new-hostname

Step 4 – Set a Pretty Hostname (optional)

A pretty hostname can include spaces:

sudo hostnamectl set-hostname "My Production Server" --pretty

Step 5 – Verify the Change

Confirm the new hostname:

hostnamectl
hostname
uname -n

Step 6 – Preserve Hostname in Cloud Environments

On cloud instances, disable cloud-init hostname resets:

sudo nano /etc/cloud/cloud.cfg

Set:

preserve_hostname: true

Conclusion

Your Ubuntu 24.04 LTS server now has a persistent hostname that survives reboots. Update DNS records if needed and inform any monitoring systems of the change.