How to Change Hostname on Ubuntu 18.04 Bionic Beaver

Change Hostname on Ubuntu

In this article, we will have explained the necessary steps to change Hostname on Ubuntu 18.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

A hostname is a name assigned to a “host” i.e. a computer on a network. The hostname is basically just your computer’s name. It’s used to identify your computer on the network.

Change Hostname on Ubuntu

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Change hostname in Ubuntu Using Command Line.

Open a terminal and use the command below to change the hostname. Replace <new_hostname> with the new hostname you want to use:

hostname <new_hostname>

To change your hostname permanently, you’ll also need to edit your /etc/hostname and /etc/hosts file, which is where Ubuntu distributions store the hostname:   localhost   linuxtips

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff04::1 ip6-allnodes
ff04::2 ip6-allrouters

If the cloud-init package is installed you also need to edit the cloud.cfg file. This package is usually installed by default in the images provided by the cloud providers such as AWS and it is used to handle the initialization of the cloud instances:

sudo nano /etc/cloud/cloud.cfg

Search for preserve_hostname and change the value from false to true:

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

To verify that the hostname was successfully changed, once again use the hostnamectl command:

Static hostname: linuxtips
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 6f17445f530bmwe46a008c9abd8ed64a5
           Boot ID: 1c769ab73b924a188c5cramona72e0f4
    Virtualization: kvm
  Operating System: Ubuntu 18.04 LTS
            Kernel: Linux 4.15.0-22-generic
      Architecture: x86-64

That’s all you need to do to change Hostname on Ubuntu 18.04. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.