How to Change Hostname on Ubuntu

Published:
July 25, 2024
Topics:

A hostname is a user-defined name that identifies a computer system on a network. In Ubuntu, users assign a hostname to the machine during the operating system (OS) setup.

In this tutorial, you will learn how to change a hostname on Ubuntu using the Linux command line or GUI.

How to change hostname on Ubuntu

Prerequisites

How to Check Current Hostname on Ubuntu

To check the current hostname of your Ubuntu system, use one of the two available commands: hostname or hostnamectl.

To print only the hostname, run the hostname command:

hostname
hostname command terminal output

To display both the hostname and additional information about your system, run hostnamectl:

hostnamectl
hostnamectl command terminal output

The Static hostname line displays your machine's hostname.

Note: Valid hostnames are between 2 and 64 characters in length. They can contain only letters, numbers, periods, and hyphens, but must begin and end with letters and numbers only.

Change Hostname on Ubuntu via CLI (No Reboot)

If you wish to permanently change the hostname without rebooting your computer, use the hostnamectl command.

Step 1: Use set-hostname to Change the Hostname

Type the following command to change your hostname:

hostnamectl set-hostname new-hostname

For example, to change the hostname to test-pnap, run the following:

hostnamectl set-hostname test-pnap

Step 2: Confirm the Change

If successful, hostnamectl set-hostname does not produce any output. Therefore, use hostname or hostnamectl to check the result.

hostname

Step 3: Change the Pretty Hostname (Optional)

A "pretty" hostname is the hostname presented to the user, not to other computers on a network. A computer system identifies another computer only by its static hostname.

Note: The pretty hostname does not have the naming limitations of its static counterpart, any UTF-8 value is permitted.

To change a machine's pretty hostname, use the same hostnamectl command with the --pretty option:

hostnamectl set-hostname new-hostname --pretty

For example, set your machine's pretty hostname to pnap-test with:

hostnamectl set-hostname pnap-test --pretty

Since the command has no output, check the result with hostnamectl:

hostnamectl
Pretty hostname in hostnamectl terminal output

The output prints an additional line, listing the computer's pretty hostname.

Note: Make sure the static and "pretty" hostname are not the same. Having the same value for both might cause some issues with systemd.

Change Hostname on Ubuntu via CLI (Reboot Required)

Another way to permanently change the hostname is by editing two configuration files: /etc/hostname and /etc/hosts.

The changes take effect immediately after the system reboots.

Step 1: Open /etc/hostname and Change the Hostname

Edit the file with a text editor of your choice. In this example, we use the Vim editor. Take the following steps:

1. Access the /etc/hostname file:

sudo vim /etc/hostname
sudo vim /etc/hostname terminal output in vim

The /etc/hostname file contains only the current hostname.

2. Replace the current hostname with your new choice.

3. Save the file and exit.

Step 2: Open /etc/hosts and Change the Hostname

Edit the /etc/hosts file in the same way. Take the following steps:

1. Access the file in Vim or a different text editor:

sudo vim /etc/hosts
sudo vim/etc/hosts terminal output in Vim

The file /etc/hosts maps hostnames to IP addresses.

2. Look for the hostname you wish to change and replace it with your new choice.

3. Save the edits and exit.

Step 3: Reboot the System

Reboot your computer to apply the changes:

sudo systemctl reboot

Note: If you use the Cloud-Init package (cloud-init) to run a cloud instance of Ubuntu, you need to perform another step before rebooting. Go to /etc/cloud/cloud.cfg file and make sure the line preserve_hostname is set to TRUE.

Change Hostname on Ubuntu via GUI

The Ubuntu GUI also allows you to edit the system hostname. Take the following steps:

1. Access Settings.

Ubuntu settings

2. Navigate to the System section.

Go to the System section

3. Choose the About tab.

Choose the About tab

4. Edit the Device Name field.

Finf the device name field

5. Replace the current hostname with a new one and confirm your choice by clicking the checkmark button.

This action permanently changes the hostname.

Temporarily Change Hostname on Ubuntu

Use the hostname command to change your computer's hostname temporarily. The syntax is:

sudo hostname new-hostname

For example, to temporarily change the hostname to test-pnap, run:

sudo hostname test-pnap

If successful, this step produces no output. To confirm the result, check the current system hostname:

Conclusion

This article presented different methods for changing the hostname on your Ubuntu machine using the command line or GUI. It also showed you how to change your hostname temporarily.

Next, learn how to change a hostname on Debian.

Was this article helpful?
YesNo