Logotipo do Ubuntu em fundo laranja

How to Change the Hostname in Ubuntu (Step-by-Step Guide)

In Ubuntu, the hostname is the name your system uses to identify itself on a network or terminal prompt. By default, Ubuntu often sets this to “localhost” (especially on fresh installs), which can make network management and troubleshooting harder — particularly in homelabs, multi-server setups, or cloud environments.

This guide shows you how to change your hostname in Ubuntu using the recommended tools and configuration files, with both temporary and permanent methods.


🧠 What Is a Hostname and Why It Matters

A hostname is a unique identifier for your Ubuntu system on a network or when accessing the terminal via SSH. A clear, consistent hostname helps you:

  • distinguish between multiple machines
  • avoid confusion when managing servers
  • organize your homelab effectively

🛠 Method 1 — Change Hostname with hostnamectl (Recommended)

The easiest and most modern way to change the hostname in Ubuntu is by using hostnamectl, which is part of systemd:

📌 Steps

  1. Open a terminal or SSH into your Ubuntu machine.
  2. Run this to see your current hostname:
hostnamectl
  1. To set a new hostname:
sudo hostnamectl set-hostname new-hostname
  1. Verify the change:
hostnamectl

The new hostname is applied immediately and persists after reboot.


⚡ Method 2 — Change Hostname Temporarily

You can change the hostname for the current session only (this will reset on reboot):

sudo hostname new-hostname

This is useful for testing, but won’t persist after reboot unless you update the configuration files.


✍️ Method 3 — Edit Configuration Files Manually

To ensure the hostname sticks and is recognized system-wide:

1️⃣ Edit /etc/hostname

Open the file with a text editor:

sudo vi /etc/hostname

Replace the old hostname with the new one and save.

2️⃣ Edit /etc/hosts

Then update the hostname entry in the hosts file:

sudo vi /etc/hosts

Find the line containing the old hostname and replace it, for example:

127.0.1.1 new-hostname

Save and exit.

3️⃣ Optional: Cloud Instances

If using a cloud-based Ubuntu system, open:

sudo nano /etc/cloud/cloud.cfg

Set:

preserve_hostname: true

This prevents cloud-init from overriding your change.

4️⃣ Reboot to Apply

sudo reboot

Once rebooted, you can confirm the change:

hostname

🔍 Tips & Best Practices

  • Always choose a unique hostname for each machine in your network.
  • Hostnames in Ubuntu can include letters, numbers, hyphens, and dots.
  • For cloud VMs, setting preserve_hostname prevents automation tools from resetting your hostname.
  • Use consistent naming conventions (e.g., web01, db-server, homelab-node) for easier management.

🏁 Conclusion

Changing the hostname on Ubuntu is straightforward and can be done in several ways — using hostnamectl, editing config files, or temporarily via the hostname command. This flexibility makes it suitable for both beginners and experienced sysadmins alike. Keeping clear hostnames is essential for network clarity, efficient troubleshooting, and homelab organization.


Looking for more Linux system administration tips and fixes?
Browse all Linux & System Administration guides →

Leave a Comment

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


Scroll to Top