How to Change Hostname on Ubuntu

Introduction

A hostname is a user-generated custom name that identifies a computer system in 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: <strong>hostname</strong> or hostnamectl.

To print only the hostname, run the hostname command:

hostname
hostname terminal output

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

hostnamectl
hostnamectl 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
terminal output for 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
terminal output for hostnamectl including a pretty hostname

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

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

2. Replace the current hostname with your new choice. For example, test.

change Ubuntu hostname by editing the /etc/hostname file

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
terminal output for sudo vim /etc/hosts

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, for example, with test.

change Ubuntu hostname in /etc/hosts file

3. Save the edits and exit.

Editing the /etc/hosts file

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 provides a way to edit the system hostname. Take the following steps:

1. Access Settings.

Ubuntu settings

2. Navigate to the About section.

Ubuntu settings about section

3. Locate the Device Name field.

Device name

Click the Device Name field to open the Rename Device dialogue box.

4. In the Rename Device dialogue box, replace the current hostname with a new one and confirm your choice by clicking the Rename button. For example, set the new hostname as pnap.

Permanently change hostname on Ubuntu via settings

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 does not provide any output. To confirm the result, check the current system hostname:

temporarily change hostname on ubuntu using the hostname command

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.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Use the hostname Command in Linux
October 8, 2020

The Linux hostname command lets you view your computers domain, hostname, and IP address.
Read more
How to Change or Set Hostname on CentOS 8 / RHEL 8
August 10, 2020

If you decide to change your hostname, you have multiple available options to do so. Follow the methods outlined in...
Read more
How to Change Hostname on Ubuntu 18.04
February 27, 2019

Learn The Simple Way How to Change Hostname on Ubuntu 18.04. Multiple options, including using the Linux terminal.
Read more
How to Set or Change a Hostname in CentOS 7
October 21, 2018

This technical guide is for users who already have a server running the CentOS 7 operating system and need to change the hostname.
Read more