Ubuntu 18.04 LTS Set Hostname Permanently

Ubuntu 18.04 LTS Set Hostname Permanently

 

In this article I will show how to set hostname in Ubuntu server 18.04 permanently. Lets say my server old hostname is: serv and should be changed to: serv1.

Ubuntu Server 18.04 Set hostname

 

In Ubuntu 18.04 hostnamectl command using for query, change the system hostname and related settings.

To show Ubuntu server 18.04 hostname type:

$ hostnamectl

As you see on picture below the current hostname is: serv.

Example output:

ubuntu 18.04 hostname

ubuntu 18.04 hostname

Now lets set hostname to serv1:

$ sudo hostnamectl set-hostname serv1

 

Display IP address on Ubuntu 18.04

 

If you know your server IP address you can skip this paragraph.

To display IP address on Ubuntu 18.04 type:

$ ip a

Example output:

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:52:df:f7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.83.131/24 brd 192.168.83.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe52:dff7/64 scope link
       valid_lft forever preferred_lft forever

So in my case on my IP address is: 192.168.83.131/24

 

 

Edit hosts file

 

Then we need edit hosts file: /etc/hosts (you can use any text editor)

$ sudo vi /etc/hosts

Ubuntu 18.04 hosts

After 127.0.0.1 localhost.localdomain localhost  add your host IP and desired hostname. Something like this:

127.0.0.1 localhost.localdomain localhost
192.168.83.131 serv1
::1 localhost6.localdomain6 localhost6

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

Now save and close your editor.

 

 

Preventing Ubuntu Revert To Old Hostname After Reboot

 

I know about common problem: Ubuntu hostname keeps reverting. To preserve hostname and preventing Ubuntu revert to old hostname after reboot you should edit cloud configuration file: /etc/cloud/cloud.cfg.

# vi /etc/cloud/cloud.cfg

Example output:

preserve hostname Ubuntu 18.04 LTS

Change from:

preserve_hostname: false

to:

preserve_hostname: true

Then save and close opened cloud.cfg file.

 

Now you can reboot your server and verify changes to make sure everything is fine after reboot.

$ hostnamectl

   Static hostname: serv1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 636bc4706e6a4a4db155e10f427cf812
           Boot ID: a3eaf86094d24a6d8a966fdec88b020a
    Virtualization: vmware
  Operating System: Ubuntu 18.04.3 LTS
            Kernel: Linux 4.15.0-58-generic
      Architecture: x86-64

In this tutorial I explained how to change hostname  in Ubuntu 18.04 server.

arstech

Leave a Reply