YouTube Icon

Code Playground.

How to Change Hostname on Ubuntu 18.04

CFG

How to Change Hostname on Ubuntu 18.04

This instructional exercise will manage you through the way toward changing the hostname on a Ubuntu 18.04 framework. 

The hostname is set when the Ubuntu working framework is introduced or on the off chance that you are turning up a virtual machine it is progressively relegated to the occurrence at startup. 

The technique depicted in this guide will work without the need of restarting your framework. 

Despite the fact that this instructional exercise is composed for Ubuntu 18.04 similar guidelines apply for Ubuntu 16.04 and any Ubuntu-based dispersion, including Linux Mint and Elementary OS. 

Prerequisites

Prior to proceeding with this instructional exercise, ensure you are signed in as a client with sudo advantages . 

A hostname is a mark that distinguishes a machine on the organization. You shouldn't utilize the equivalent hostname on two unique machines on an equivalent organization. 

Display the Current Hostname

To see the current hostname, enter the accompanying order: 

hostnamectl

Ubuntu 18.04 hostnamectl 

As should be obvious in the picture over, the current hostname is set to ubuntu1804.localdomain. 

Change the Hostname

The accompanying advances layout how to change the hostname in Ubuntu 18.04. 

1. Change the hostname using hostnamectl.

In Ubuntu 18.04 we can change the framework hostname and related settings utilizing the order hostnamectl. 

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

sudo hostnamectl set-hostname linuxize

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

2. Edit the /etc/hosts file.

Open the/and so forth/has document and change the old hostname to the enhanced one. 

/etc/hosts

127.0.0.1   localhost
127.0.0.1   linuxize

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

3. Edit the cloud.cfg file. 

In the event that the cloud-init bundle is introduced you likewise need to alter the cloud.cfg document. This bundle is generally introduced naturally in the pictures gave by the cloud suppliers, for example, AWS and it is utilized to deal with the instatement of the cloud occurrences. 

To check if the bundle is introduced run the accompanying ls order : 

ls -l /etc/cloud/cloud.cfg

In the event that you see the accompanying yield it implies that the bundle isn't introduced and no further activity is required. 

ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory

In the event that the bundle is introduced the yield will resemble the accompanying: 

-rw-r--r-- 1 root root 3169 Apr 27 09:30 /etc/cloud/cloud.cfg

For this situation you'll have to open the/and so on/cloud/cloud.cfg record: 

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

Save the document and close your proofreader. 

Verify the change

To confirm that the hostname was effectively changed, by and by utilize the hostnamectl order: 

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

You should see your new worker name imprinted on the support. 

Conclusion

In this instructional exercise, we have told you the best way to handily change your Ubuntu worker hostname without restarting the machine. 

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




CFG