Skip to main content

How to Configure Hostname Permanently on Ubuntu 16.04 LTS?

A hostname is a label that identifies a machine on the network. You shouldn’t use the same hostname on two different machines on a same network..You might ask yourself why you would need to change your hostname? The most common scenarios would be due to a domain name change, your business has changed its course, or because you have thought of something better.
The hostname is used by many of the networking programs (such as sendmail, Apache servers) to identify the machine. By default, your server is started with the server’s given name as the hostname. Some software, such as cPanel, CWP , VestaCP requires a valid Fully Qualified Domain Name (FQDN) for the hostname.

Types of hostnames (The hostname can be configured as follows):

  • Static host name assigned by sysadmin. For example, “server1”, “wwwBot2”, or “host.datahead.biz”.
  • Transient/dynamic host name assigned by DHCP or mDNS server at run time.
  • Pretty host name assigned by sysadmin/end-users and it is a free-form UTF8 host name for presentation to the user. For example, “Rubel’s netbook”.

On a Ubuntu 16.04 LTS server you can use any one of the following tool to manage hostnames:

  • hostnamectl command : Control the system hostname. This is a recommended method.
  • nmtui command : Control the system hostname using text user interface (TUI).
  • nmcli command : Control the system hostname using CLI part of NetworkManager.

Benefits to using a Fully Qualified Domain Name for your Hostname
It is good practice to use your FQDN “Fully Qualified Domain Name” as your hostname. Following this practice creates more options for securing your hostname with an SSL. This will allow services like email to function using a secured connection. Using a hostname with a registered domain will allow you to add a corresponding DNS entry. This will prevent unpredictable behavior by some services that use the hostname. This would allow you to set up a reverse lookup DNS entry. It can be very important especially with services like email verification. For example, when an email is sent the receiving server runs a reverse lookup on the sender’s hostname. The reverse lookup allows the receivers server to ensure the hostname resolves to the matching IP address. This is just one preventive measure servers now use to reduce email spoofing incidents.

1.Show Present Hostname :
# hostnamectl status
# cat   /etc/hostname
2.Change Hostname Temporary :
# hostname mail.datahead.biz 
3.Change Hostname Permanently: In newer versions of Ubuntu, you will also want to use the hostnamectl command mentioned earlier
# hostnamectl set-hostname mail.datahead.biz
# hostnamectl status
   Static hostname: mail.datahead.biz
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 19c3ff48f9e1ee9f69452cf05d7287dd
           Boot ID: ad0cbbce6aee48f2b7fa1de5ba745458
    Virtualization: vmware
  Operating System: Ubuntu 16.04.6 LTS
            Kernel: Linux 4.4.0-171-generic
      Architecture: x86-64
It also writes this information to the /etc/hostname file as well.
# cat /etc/hostname
mail.datahead.biz

# vi   /etc/hostname      (To Edit)
mail.datahead.biz
:x  (save & quit) 
4.One thing the hostnamectl command doesn’t do is modify the /etc/hosts file, so we’ll still need to do that
# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
SERVER_IP    mail.datahead.biz     host
:x  (save & quit)
Don't edit this file manually on Ubuntu Distro
# cat  /etc/resolv.conf
# Generated by NetworkManager
search datahead.biz
nameserver   8.8.4.4
nameserver   8.8.8.8
nameserver   127.0.0.1
:x  (save & quit)
5.Check the below status
# systemctl status  systemd-hostnamed
# systemctl status  network.service
# hostnamectl status  
How to Configure Hostname Permanently on Ubuntu 16.04 LTS?

Change the hostname in Ubuntu 18.04.

You need to edit the cloud.cfg file, If the cloud-init package is installed. This package is usually installed by default in the images provided by the cloud providers such as AWS and it is used to handle the initialization of the cloud instances.
To check if the package is installed run the following ls command:
root@mail:~# ls -l /etc/cloud/cloud.cfg
ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory
The above output means that the package is not installed and no further action is required.
If the package is installed the output will look like the following:
-rw-r--r-- 1 root root 3169 Apr 27 09:30 /etc/cloud/cloud.cfg
In this case you'll need to open the /etc/cloud/cloud.cfg file, Search for preserve_hostname and change the value from false to true:
# vi /etc/cloud/cloud.cfg
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

Comments