Cybersecurity Updates & Tools

Change Hostname on Ubuntu 18.04: hostnamectl and /etc/hosts Guide

The hostname is the label that identifies a machine on a network. It appears in your terminal prompt, in system logs, and in SSH session information. When another machine connects to your server via SSH, the prompt displays the username and hostname together — a clear, accurate hostname makes managing multiple servers much easier.

When you install Ubuntu or spin up a virtual machine, the hostname is either set during installation or dynamically assigned at startup by a service like cloud-init. Changing it later is straightforward and does not require a reboot.

This guide shows you how to change the hostname on Ubuntu 18.04 without restarting the system. The same steps work on Ubuntu 16.04 and any Ubuntu-based distribution, including Linux Mint and Elementary OS.

<strong>Prerequisite:</strong>&nbsp;You need sudo access. Do not assign the same hostname to two machines on the same network.

Change the Hostname on Ubuntu with hostnamectl

First, check your current hostname:

bashhostnamectl

The output shows the static hostname along with system and virtualization details:

   Static hostname: ubuntu1804.localdomain         Icon name: computer-vm  Operating System: Ubuntu 18.04 LTS            Kernel: Linux 4.15.0-22-generic      Architecture: x86-64

Ubuntu 18.04 uses systemd, and hostnamectl is the standard way to read and update hostname settings without editing files directly.

Set the new hostname with the set-hostname subcommand:

bashsudo hostnamectl set-hostname linuxize

The command produces no output on success. When choosing a new hostname, follow these naming conventions:

  • Use lowercase letters, numbers, and hyphens only
  • Do not use underscores or spaces
  • Keep it under 63 characters

Update /etc/hosts and the cloud.cfg File

The hostnamectl command updates the static hostname, but you also need to update /etc/hosts. This file maps hostnames to IP addresses locally. If you leave the old hostname in place, some programs and services may fail to resolve local addresses correctly and throw errors like could not resolve host.

Open /etc/hosts and replace the old hostname with the new one:

127.0.0.1   localhost127.0.0.1   linuxize::1     localhost ip6-localhost ip6-loopbackff02::1 ip6-allnodesff02::2 ip6-allrouters

Save the file when done.

Handling cloud-init on cloud servers

If your server runs on a cloud provider like AWS, DigitalOcean, or Google Cloud, the cloud-init package is likely installed. Cloud-init can reset your hostname back to the original value on the next reboot, overriding your change entirely.

Check if cloud-init is installed:

bashls -l /etc/cloud/cloud.cfg

If you see this output, cloud-init is not installed and no further steps are needed:

ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory

If the file exists, open it:

bashsudo vim /etc/cloud/cloud.cfg

Find preserve_hostname and change its value from false to true:

ini# This will cause the set+update hostname module to not operate (if true)preserve_hostname: true

Save and close the file. This tells cloud-init to leave your hostname alone on future reboots.

Verify the New Hostname

Run hostnamectl once more to confirm the change is in place:

bashhostnamectl
   Static hostname: linuxize  Operating System: Ubuntu 18.04 LTS            Kernel: Linux 4.15.0-22-generic      Architecture: x86-64

If you see your new name under Static hostname, the change is complete. Open a new terminal session to see the updated shell prompt reflecting the new hostname.

Your Ubuntu 18.04 hostname is now updated with no reboot required. Leave a comment below if you run into any issues during the process.