YouTube Icon

Code Playground.

How to Change Hostname on Ubuntu 20.04

CFG

How to Change Hostname on Ubuntu 20.04

The hostname is set during the establishment of the working framework or powerfully relegated to the virtual machine when it is made. 

This guide discloses how to set or change the hostname on Ubuntu 20.04 without expecting to restart the framework. 

Understanding Host Names

A hostname is a name that recognizes a gadget on an organization. You shouldn't have at least two machines with the equivalent hostname, on a similar organization. 

In Ubuntu, you can alter the framework hostname and related settings utilizing the hostnamectl order. This apparatus perceives three distinct classes of hostname: 

  • static - The conventional hostname. It is put away in the/and so forth/hostname record and can be set by the client. 
  • pretty - An elucidating freestyle UTF8 hostname utilized for introduction to the client. For instance, Linuxize's PC. 
  • transient - A dynamic hostname that is kept up by the piece. DHCP or mDNS workers can change the transient hostname at run time. Of course, it is equivalent to the static hostname. 

It is prescribed to utilize a completely qualified space name (FQDN ), for example, host.example.com for both static and transient names. 

Just root or clients with sudo benefits can change the framework hostname. 

Displaying the Current Hostname

To see the current hostname, conjure the hostnamectl order with no contention: 

hostnamectl

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

Changing the System Hostname

Changing the framework hostname is a basic cycle. The linguistic structure is as per the following: 

sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your Pretty HostName" --pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient

For instance, to change the framework static hostname to neptune.linuxize.com, you would utilize the accompanying order: 

sudo hostnamectl set-hostname neptune.linuxize.com

Alternatively you can likewise set the pretty hostname: 

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

hostnamectl doesn't create yield. On progress, 0 is restored, a non-zero disappointment code in any case. 

The static hostname is put away in/and so on/hostname, and the pretty hostname is put away in/and so on/machine-information document. 

You shouldn't utilize the equivalent hostname on two distinct machines on a similar organization. 

On most frameworks, the hostname is planned to 127.0.0.1 in/and so on/has. Open the record and change the old hostname to the upgraded 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

In the event that you are running Ubuntu on a cloud example and the cloud-init bundle is introduced, you likewise need to alter the/and so on/cloud/cloud.cfg record. This bundle is normally introduced naturally in the pictures gave by the cloud suppliers, and it is utilized to deal with the instatement of the cloud examples. 

On the off chance that the document exists on your framework open it: 

sudo vim /etc/cloud/cloud.cfg

Quest for preserve_hostname, and change the incentive from bogus to valid: 

/etc/cloud/cloud.cfg

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

Spare the document and close your proofreader. 

Verify the change

To confirm the hostname has been completely changed, enter the hostnamectl order: 

   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 told you the best way to effectively change the hostname on Ubuntu 20.04 establishment without restarting the machine. 

There are various reasons why you may need to change the hostname. The most widely recognized is the point at which the hostname is naturally set upon the case creation. 

Don't hesitate to leave a remark in the event that you have any inquiries.




CFG