Hamsa K
Editor
5 min read | 2 months ago

How to Change Hostname in Ubuntu 18 04 and 20 04

How to change hostname in ubuntu 18.04/20.04

The hostname is basically just your computer’s name. It’s used to identify your computer on the network.The hostname is set at the time when the Ubuntu operating system is installed or if you are spinning up a virtual machine it is dynamically assigned to the instance at startup.

First we need to login with root user or any user having sudo previliges.

To view the current hostname, open a terminal window, and enter the following command

hostnamectl
root@linuxbox:~# hostnamectl
   Static hostname: linuxbox
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2c4fd6580fd74093b803ff392d50a6d9
           Boot ID: 52be94a60b89444381816951cf76b102
    Virtualization: oracle
  Operating System: Ubuntu 18.04.2 LTS
            Kernel: Linux 4.18.0-15-generic
      Architecture: x86-64

Step 1: change ubuntu hostname

change the hostname using options with the hostnamectl command

sudo hostnamectl set-hostname lampblogs.com

you can change to any name instead of lampblogs.com.now you can exit from the shell and login again.then you can run hostnamectl command

root@lampblogs:~# hostnamectl
   Static hostname: lampblogs.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2c4fd6580fd74093b803ff392d50a6d9
           Boot ID: 52be94a60b89444381816951cf76b102
    Virtualization: oracle
  Operating System: Ubuntu 18.04.2 LTS
            Kernel: Linux 4.18.0-15-generic
      Architecture: x86-64

step 2:  Change Server name in the Hosts File

Find your public or private IP address by running the following command

root@lampblogs:~# ip a

Update /etc/hosts file with your system ipaddress and hostname using your favourite editor vi or nano

root@lampblogs:~# nano /etc/hosts

Add a second line as follows (replace this IP address with yours)

127.0.0.1       localhost
192.168.0.8     lampblogs.com
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

save and close the file.( if you are using nano editor press ctrl+x and press  'y' to save changes).

and also edit /etc/cloud/cloud.cfg file as below.

If the cloud-init package is installed then you need to edit the cloud.cfg file.

To check if the package is installed run the following command

root@lampblogs:~# ls -l /etc/cloud/cloud.cfg
ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory

If the package is installed sample output will be like below

-rw-r--r-- 1 root root 0 Jan 22 17:05 /etc/cloud/cloud.cfg

Then open /etc/cloud/cloud.cfg file and search for preserve_hostname

root@lampblogs:~# nano /etc/cloud/cloud.cfg
preserve_hostname: true

If above line already set to true, leave it as it is. If it is set to false, change it to true. Save the file and exit.

Step 3: verify changes

Finally restart the server to apply changes and verify that the hostname was successfully changed, once again check with command hostnamectl.

reboot
hostnamectl

Now you are able to see your new server name on the console.

 



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy