How to Change Hostname on Ubuntu 20.04

[ad_1]

The hostname is set during the installation of the operating system or dynamically assigned to the virtual machine when it is created.

This guide explains how to set or change the hostname on Ubuntu 20.04 without needing to restart the system.

Understanding Host Names #

A hostname is a label that identifies a device on a network. You shouldn’t have two or more machines with the same hostname, on the same network.

In Ubuntu, you can edit the system hostname and related settings using the hostnamectl command. This tool recognizes three different classes of hostname:

  • static – The traditional hostname. It is stored in the /etc/hostname file and can be set by the user.
  • pretty – A descriptive free-form UTF8 hostname used for presentation to the user. For example, Linuxize's laptop.
  • transient – A dynamic hostname that is maintained by the kernel. DHCP or mDNS servers can change the transient hostname at run time. By default, it is the same as the static hostname.

It is recommended to use a fully-qualified domain name (FQDN
) such as host.example.com for both static and transient names.

Only root or users with sudo privileges
can change the system hostname.

Displaying the Current Hostname #

To view the current hostname, invoke the hostnamectl command without any argument:

hostnamectl
Ubuntu 20.04 hostnamectl

In this example, the current hostname is set to ubuntu2004.localdomain.

Changing the System Hostname #

Changing the system hostname is a simple process. The syntax is as follows:

sudo hostnamectl set-hostname host.example.comsudo hostnamectl set-hostname "Your Pretty HostName" --prettysudo hostnamectl set-hostname host.example.com --staticsudo hostnamectl set-hostname host.example.com --transient

For example, to change the system static hostname to neptune.linuxize.com, you would use the following command:

sudo hostnamectl set-hostname neptune.linuxize.com

Optionally you can also set the pretty hostname:

sudo hostnamectl set-hostname "Linuxize's laptop" --pretty

hostnamectl does not produce output. On success, 0 is returned, a non-zero failure code otherwise.

The static hostname is stored in /etc/hostname, and the pretty hostname is stored in /etc/machine-info file.

You shouldn’t use the same hostname on two different machines on the same network.

On most systems, the hostname is mapped to 127.0.0.1 in /etc/hosts. Open the file and change the old hostname to the new one.

/etc/hosts

127.0.0.1   localhost
127.0.0.1   neptune.linuxize.com

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

If you are running Ubuntu on a cloud instance and the cloud-init package is installed, you also need to edit the /etc/cloud/cloud.cfg file. This package is usually installed by default in the images provided by the cloud providers, and it is used to handle the initialization of the cloud instances.

sudo vim /etc/cloud/cloud.cfg

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

/etc/cloud/cloud.cfg

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

Save the file and close your editor.

Verify the change #

To verify the hostname has been fully changed, enter the hostnamectl command:

hostnamectl

Your new hostname will be printed on the terminal:

   Static hostname: neptune.linuxize.com
   Pretty hostname: Linuxize's desktop
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a04e3543f3da460294926b7c41e87a0d
           Boot ID: aa31b274703440dfb622ef2bd84c52cb
    Virtualization: oracle
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-26-generic
      Architecture: x86-64

Conclusion #

We have shown you how to easily change the hostname on Ubuntu 20.04 installation without restarting the machine.

There are a number of reasons why you may need to change the hostname. The most common is when the hostname is automatically set upon the instance creation.

Feel free to leave a comment if you have any questions.

[ad_2]

Source link

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.