How to Display and Set Hostname in Linux

hostname command linux
With more and more computer connected to the network, the computer needs to have an attribute to make it different from each other. Same as a human in the real world, the computer also has an attribute which named 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 display domain name

If hostname command will display your hostname, then dnsndomainname command will show your domain name. Let’s see the sample.

$ dnsdomainname

In 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 use 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 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=’’

How to set a hostname

The 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 /etc/sysconfig/networks

Here’s the content of each files.

/etc/hosts localhost.localdomain localhost dev-machine



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

Pungki Arianto 9:49 am

About Pungki Arianto

Pungki , currently working as a Linux / Unix administrator for a banking company. He love to work in Linux / Unix since it's fun for him. He is also interested in information technology, information security and writing.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.