How to change the host name in Linux?

Hostname is the machine name that has assigned during the installation of the OS. Not to be confused with the username, which is the user account login name. While there can be many user accounts on the system, there’s only one hostname and it’s the same for every user.

The shell prompt has this syntax,


user is the login name of the user account.
hostname is the physical machine name (mapped to
directory is the current working directory.
$ indicates the regular user.
# indicates the super user.

The command hostname prints the host name of the machine which is apparent in the shell prompt.

[deepak@rhel7 ~]$ hostname

To change the hostname, simply pass the new hostname as argument to the hostname. And it requires super user privileges.

[deepak@rhel7 ~]$ sudo hostname dileep
[sudo] password for deepak:
[deepak@rhel7 ~]$ hostname

Closing and opening the terminal confirms the changed hostname.

[deepak@dileep ~]$

But this change is temporary. Once the system is shut down the hostname we set will be lost, just like the IP address set via ifconfig. To make it permanent, open and edit the file /etc/hostname. After editing it looks as follows.

[deepak@dileep ~]$ cat /etc/hostname

Now reboot the system to confirm the new host name of the machine.

About Deepak Devanand

Seeker of knowledge
This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s