January 22, 2020
January 22, 2020
Having the same hostname for all systems will make administrative and maintenance task for the system and network itself very difficult.
The hostname in any Linux based distribution is used to identify a system uniquely in a network. The default hostname that is set during installation is localhost. In the case of a few cloud services, the default hostname is set according to their preferences and is not always localhost.
Installed application use the hostname as a default identifier while communicating with other systems.
Therefore to communicate with any other systems in a network without any confusion, it is always a good idea to change it. In this article, we will look at how to change the hostname in Ubuntu 18.04 LTS.
Remember, that for all the methods we’re about to show, you need to use SSH to access your Linux server. Here’s a tutorial in case you need a little help.
The easiest way to change the hostname is by using the hostnamectl command. The command is a part of systemd utilities and is installed by default. If for some reason it is not present in your system then use the following apt-get command to install it:
apt-get install systemd
Now the hostnamectl command will be available if it was missing earlier. Type hostnamectl in the terminal command line to view the system information – including the hostname. Here’s the command:
This is the output you would get:
Static hostname: hostinger Icon name: computer-vm Chassis: vm Machine ID: 45598cbdb6ee462e8696166b520fe788 Boot ID: 99526e56aeea45c2a0f3b2ffaaffe9d9 Virtualization: xen Operating System: Ubuntu 18.04.1 LTS Kernel: Linux 4.15.0-1021-aws Architecture: x86-64
The output of the above command lists the permanent hostname as the static hostname. In order to change the system’s hostname permanently just use the following hostnamectl command:
hostnamectl set-hostname hostinger
The above command does not produce an output. To verify the new hostname, type hostnamectl from the terminal without any options or parameters:
The hostname command is used to show or set a system’s hostname and domain name. The basic syntax for the command is:
hostname [options] [new_host_name]
To view the current setting of your system, use the hostname without any options and arguments:
To change the hostname, just use the hostname command without any options by typing in the new hostname as the only argument.
To verify if the hostname have been set correctly, just use the simple hostname command.
Remember the changes in the hostname is temporary. If you reboot the system, the hostname will be reverted to the old one.
The hostname command is useful in a situation where you have a minor task that requires a temporary changes, but don’t want to make permanent changes.
It is possible to change the hostname by editing a few configuration hostname files directly. You just need to update the hostname in two configuration files – /etc/hostname and /etc/hosts.
To start with, edit the file /etc/hostname and update the record with the new hostname:
vi /etc/hostname hostinger
Next update the hostname record with the new one in /etc/hosts file so that the system resolves the new hostname in the network.
# vi /etc/hosts 127.0.0.1 hostinger
At this stage, you are done with updating records in the necessary configuration files. But you are still a step away from applying the above changes.
In the final step, you need to edit a cloud configuration file. This cloud init package is installed from the default images provided by a few cloud service providers to initialize a cloud instance.
To complete the formalities, edit the cloud configuration file and change the value of preserve_hostname to true.
# vi /etc/cloud/cloud.cfg
# This will cause the set+update hostname module to not operate (if true)
If the above cloud configuration file is not present in your system then don’t worry, you can safely skip this step.
Reboot your system and open a terminal session. To verify that the hostname was indeed preserved during reboot just type the following command in the terminal:
To change the hostname using the Hostinger control panel, just go to the Servers menu, at the top navigation bar and select the instance for which you want to change the hostname. In the newly opened window, update the value of hostname to the new one and hit Save changes. Congratulations, you successfully changed the hostname using Hostinger’s control panel:
Changing the hostname in Linux Ubuntu 18.04 is pretty easy. Now you know how to do it through Hostinger’s control panel, and the Linux terminal. Now you can manage your network easily!