How To Display And Set Hostname in Linux

December 1, 2013 | By in LINUX COMMANDS, LINUX HOWTO
| Reply More

hostname command linux
With more and more computer connected to the network, computer need to have an attribute to make it different to each other. Same as human in the real world, computer also hava an attribute which named hostname.

What is hostname

From its manual page, hostname is used to display the system’s DNS name and to display or set its hostname or NIS domain name. So hostname is related to DNS (Domain Name System) or NIS (Network Information System).

How to display hostname

Hostname is a pre-installed command in every Linux distribution. You can display your machine hostname by typing hostname in your console. Here’s a sample command and the output :

$ hostname

The above command will tell you that the computer name is ubuntu.

How to set a hostname

Hostname is set when you install you Linux at the first time. There is a step in your installation procedure that your Linux will ask you to fill hostname information. However, you can do it later if you want.
To set it your hostname, you can use this command :

# hostname dev-machine

$ hostname

You need to be root user or equal to set / change your hostname machine. The # sign is indicated that you are a root user. The above command is telling your computer to set its hostname into dev-machine. If you don’t receive any error message then your hostname is changed. Again, you check it using hostname command to see the result.

Setting hostname using hostname command is not permanent. When you reboot your computer, your setting will gone. To make it permanent, you must manually edit hostname configuration files.

On Debian / Ubuntu based Linux

You will find it in these folders, /etc/hostname and /etc/hosts.

Here’s the content of each files.


# vi /etc/hostname


# vi /etc/hosts localhost dev-machine

You will found it active immediately without restarting your Linux.

On RedHat / CentOS based Linux

You will find it in these folders, /etc/hosts and and /etc/sysconfig/networks

Here’s the content of each files.

/etc/hosts localhost.localdomain localhost dev-machine



How to display dnsdomainname

From hostname definition above, hostname can also display a dnsname for your Linux. If hostname command will display your hostname, then dnsndomainname command will show your domain name. Let’s see the sample.

$ dnsdomainname

On this article, the result of dnsdomainname command is

If you see the result is (none), then your machine is not configured in FQDN (Fully Qualified Domain Name). Dnsdomainname command will grab information from /etc/hosts file. You should configure it in FQDN format. Here’s the sample :

/etc/hosts localhost.localdomain localhost dev-machine
::localhost dev-machine

To display it more detail, you can use parameter -v

$ dnsdomainname -v
Resolving ‘’ ...
Result: h_name=’’
Result: h_aliases=’dev-machine’
Result: h_addr_list=’’

How to display hostname with more detail information

Hostname command comes with some parameters and some aliases such as dnsdomainname command. Here’s some parameter that may useful on day-to-day operation. The results of the commands below is based on /etc/hosts configuration above

Display the IP Address

$ hostname -i

Display the domain

$ hostname -d

Display the the short name

$ hostname -s

This command will produce the same result with only typing hostname

Display with FQDN format

$ hostname -f

Display with detail information

All the parameters mentioned above can be ‘summarized’ by the parameter -v and -d. Let’s take a look the sample

$ hostname -v -d
Resolving ‘’ ...
Result: h_name=’’
Result: h_aliases=’dev-machine’
Result: h_addr_list=’’

Feel familiar? Yes, this result is the same with dnsdomainname -v command that also mentioned above.

That is some usage the hostname command to daily operations. Please stay tuned to see more Linux commands discussed in


Share This :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.