How To Change Hostname On Ubuntu 18.04

This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

A computer hostname represents a unique name that gets assigned to a computer in a network in order to uniquely identify that computer in that specific network.

During the installation of the Ubuntu, the default hostname is set as “localhost”. But if you want to change the hostname to your own choice, you can follow the instructions in this tutorial. We will guide you to change the hostaname of your Ubuntu 16.04 as well as Ubuntu 18.04.

Displaying Current Hostname

You can display the current hostname of the system by running the following command

root@codesposts:~$ hostnamectl

   Static hostname: ubuntu
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: kvm
  Operating System: Ubuntu 18.04 LTS
            Kernel: Linux 4.13.0-32-generic
      Architecture: x86-64

Instructions To Change Hostname

To change your current hostname to a new one, you can follow the instructions below

Changing Hostname Using hostnamectl Command

Run the following command to change your hostname.

root@codesposts:~$ hostnamectl set-hostname codesposts

Replace the “codesposts” with your new hostname.

The hostnamectl command does not produce output in case of successful execution.

Editing /etc/hosts File

After that, edit the file /etc/hosts using nano command.

root@codesposts:~$ nano /etc/hosts

Replace any occurence of the old hostname in that file with your new hostname and then save the file and exit.

Editing cloud.cfg File.

If you have installed the cloud-init package on your system, you also need to edit the cloud.cfg file.

To check whether the cloud-init package is installed on your system or not, you can run the following command

root@codesposts:~$ ls -l /etc/cloud/cloud.cfg

If the output shows a error that “no such directory”, it means that the package is not installed on your system.

If the package is installed, run the following command:

root@codesposts:~$ nano /etc/cloud/cloud.cfg

Search for preserve_hostname and change the value from false to true

After that, you need to reboot your system.

Verification Of New Hostname

You can verify the new hostname by running the following command

root@codesposts:~$ hostnamectl

   Static hostname: codesposts
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: kvm
  Operating System: Ubuntu 18.04 LTS
            Kernel: Linux 4.13.0-32-generic
      Architecture: x86-64

The post How To Change Hostname On Ubuntu 18.04 appeared first on CODESPOSTS.COM.

Go to Source
Author: staff

Ubuntu Server Admin

Recent Posts

How to Fix VMware’s “Could not open /dev/vmmon” Error on Ubuntu

You’ve recently installed VMware Workstation on your Ubuntu system and encountered the frustrating “Could not…

30 minutes ago

How to Fix Ubuntu 404 Errors While Fetching Dependencies

Have you ever found yourself staring at a terminal full of 404 errors while trying…

30 minutes ago

How to Fix ‘Please Install All Available Updates’ Error When Upgrading Ubuntu 18.04 to 20.04 LTS

One particularly frustrating error that many users face when trying to upgrade from Ubuntu 18.04 …

31 minutes ago

How to fix “Release is not valid yet” Error in Docker Containers

In the world of containerization, time synchronization issues can create unexpected roadblocks when working with…

31 minutes ago

How to fix “Externally Managed Environment” Pip Errors on Ubuntu

If you’ve recently upgraded to Ubuntu 23.04 or newer, you might have encountered a frustrating…

32 minutes ago

Ubuntu now officially supports NVIDIA Jetson: powering the future of AI at the edge

Canonical announces the General Availability of Ubuntu for the NVIDIA® Jetson Orin™ for edge AI…

8 hours ago