How to set the hostname of a machine


In this article you will learn how to set and check the hostname of a machine.  

Note: These instructions will work with almost all Linux / Unix distributions.


What is a hostname?

A hostname is name of your machine or computer. Generally referred by layman users of “Microsoft Windows” as a Computer name which they actually see over the network and other services. Hostnames are used primarily for administrative tasks, application related, identification, IP resolution and even more.

If you’re talking about Linux / Unix then you either need to set up your hostname manually or during installation of the operating system. If you did not choose or enter any hostname during installation most likely your hostname will be “localhost.localdomain”

 

To find out what is your current hostname we use following command:
# hostname

Most likely, if you did not choose or enter any hostname during installation you will get output as “localhost.localdomain”

You can also easily identify whether your hostname has been changed when you login to get prompt, something like below:

[root@localhost ~]#

 

In above prompt you can see that “localhost” tells you that you have the default hostname.

Hostname command

Now let’s come back to the “hostname” command. Hostname command has a lot of switches each of them have different usage and outputs which you can study by using the “man” command which means manual.

# man hostname

 

Administration

Now let’s talk about some of the important hostname switches that are used in the system administration world:

# hostname –a

Above the hostname command displays the complete hostname that is currently set. In some cases the “hostname” command may or may not display the complete hostname; “-a” switch forces the hostname command to display complete hostname.

 

# hostname –f

The above hostname command displays you complete “FQDN” fully qualified domain name.

 

# hostname –i

The above command displays you the IP address associated with the hostname you have set. For example, if  you have “server.myhost.com” it will return the IP address associated with it.

You can override or define the IP address for the hostname you set in /etc/hosts file, which will be explained later.

 

To set a hostname:

When you set a hostname there are two primary options: one is a temporary hostname and the other is a permanent change of hostname.

Temporary Hostname Changes

When it comes to a temporary change of hostname, this change will not be permanent after next reboot.

# hostname something.yourdomain.com

Or

# hostname something

Permanent Hostname

How to make your hostname permanent:

To change or set your hostname permanently― that it should remain even after next reboot― you need to follow below procedure which depends to various Linux / Unix distributions.

 

Note: We will cover CentOS and Debian

 

To set your name under CentOS:

Choose your favorite editor as we need to edit a configuration file, ours is “vim”. However, you may use any file editor whether its gedit (graphical text editor), vi or nano.

# vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=localhost.localdomain

Context of the file will be something like above. You need to change HOSTNAME= part. After editing if I want my hostname to be “something.yourdomain.com” it will look like below:

NETWORKING=yes
HOSTNAME=something.yourdomain.com

We save the file and we have changed our hostname permanently to “something.yourdomain.com”

Now, the above change will be reflected only if you reboot your machine. If we talk about mission critical servers which cannot be rebooted for various reasons, we can run below command.

# /etc/rc.d/rc.sysinit

Now if you are Debian / Ubuntu Desktop or Server user you can follow the procedure below to change your hostname.

 

# vim /etc/hostname

Edit the file hostname in /etc/ directory.

If you did not set the hostname while installation, it will be “localhost.localdomain”. Delete that and without the quotes enter the hostname you want to set.


For example, if you want to set “ubuntu.iscool.com” enter it without the quotes and save the file.

Now, the above change will be reflected only if you reboot your machine. If you talk about mission critical servers which cannot be rebooted for various reasons, run the command below.

/etc/init.d/hostname.sh start

 

Posted in
Last update:
2015-12-03 20:39
Average rating:0 (0 Votes)