announcement

Check our homepage for new, visually rich, fast and immersive experiences!

How to Change Hostname in Linux

Hostname is nothing but the name of a device. It gains more importance if the device is connected to a network. Leaf through this article to understand the process of changing the hostname in Linux.
Techspirited Staff
Hostnames are used for a variety of purposes. Different naming systems name devices differently. Network Information Service (NIS), Domain Name System (DNS), Server Message Block (SMB) are some of the naming systems used to name devices. Hostnames are generally used for administrative purposes and they may appear in computer browser lists, directory lists, etc.

Before learning to change the hostname, it is also important to learn how to check it.

How to Check Hostname in Linux

To do so, you need to try the following commands.

uname -n or hostname

Both these commands will return the present hostname of the system. Now suppose you have decided to change the hostname to NEW_HOSTNAME. Then, you can type in the following command.

hostname NEW_HOSTNAME

However, the problem is that if you reboot the system, and again check the hostname, you will find that it has been again set back to the old hostname. This is because on rebooting, the hostname is selected from a certain system configuration file. To verify, you can type init 6 at the command prompt and then check for it using the hostname command. If you want to change it permanently, so that it shows the new name even after a reboot, then you need to make some simple changes in the /etc/sysconfig/network file. To make these changes, simply type in the command vi /etc/sysconfig/network at the terminal. In the options that appear, change the hostname option to your desired hostname. You will have to also set it manually in the /etc/hosts file. To check whether it has changed permanently or not, you can try restarting the network and rebooting the system with the following commands:

/etc/init.d/network restart and init 6

Now, you can try the commands uname -n and hostname, to check out the changed hostname. However, the process for changing the hostname for different versions of Linux permanently may vary. For example, in Debian based Linux systems, you will have to make the changes in the file /etc/hostname.