Change hostname in Linux via Command Line

Posted on

In this article, change hostname in Linux operating system will be explained using command line. As long as the interface of the operating system itself can be accessed via CLI (Command Line Interface) as a root user, the purpose of changing the hostname can be achieved.

First, check the original hostname by typing the following command in the bash prompt :

hostname

Below is the output of the command execution :

[root@application home]# hostname
application
[root@application home]#

We will change the hostname from application to database. Below is the fastest way to do it by typing the following command :

hostname the_new_host_name

If it is done and executed in the bash prompt :

[root@application home]# hostname database
[root@application home]#

But the change cannot be viewed. If the change is meant to be a permanent, file hostname which is located in /etc need to be edited as follows :

[root@application home]# vim /etc/hostname

The above command is opening file hostname located in /etc to be edited by vim text-editor. Just remove the old hostname which is ‘application’ in the first line of the file. Change it with the new hostname which is ‘database’ in this context of article. After changing the content of the file and saving the file, it need to be tested further.

To prove that the hostname has already been changed, exit from the current session or disconnect from the server if it is a remote connection. Reconnect and relog so that the new hostname can already be viewed :

First of all, try logout or exit from the current session :

[root@application home]$ exit
logout
Connection to xxx.xxx.xxx.xxx closed.
[me@local ~]#

After successfully logout or exit from the live login session on that server or workstation, try to reconnect or login to the server or worstation where the hostname has already been changed :

[me@local ~]# ssh user@xxx.xxx.xxx.xxx
user@xxx.xxx.xxx.xxx's password:
Last login: Wed Nov 23 11:28:39 2016 from xxx.xxx.xxx.xxx
[user@database ~]$ 

As it can be seen in the above output, the name of the hostname has already changed from ‘application’ to ‘database’ and it works permanently.

One thought on “Change hostname in Linux via Command Line

Leave a Reply