How to display and set host names in Linux

Source: Internet
Author: User
Tags fully qualified domain name

Original link

As the number of computers connected to the network grows, each computer needs to have a property that distinguishes it from other computers. Like people in the real world, computers also have a property called hostname (host name).

What is hostname

From its operating manual, hostname is used to display the DNS name of the system and to display and set its hostname or NIS domain name. So hostname relies on DNS (domain Name System) or NIS (Network Information System).

How to show hostname

Hostname is a pre-installed command for every Linux distribution. By entering hostname in the console, you can display the hostname of your machine. Here's one with a simple command and its output.

$ hostnameubuntu

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

How to set hostname

Hostname is set when you install Linux for the first time. One of the steps Linux will let you enter the host name information. However, if you wish, you can set it later.

To set up your hostname, you can use the following command:

# hostname dev-machine$ Hostnamedev-machine

You need to set/modify the hostname of your computer by using root or equivalent root privileges. The "#" identity proves that you are the root user. The above command sets the hostname of your computer as dev-machine. If you do not receive any error messages, then your hostname has changed. Once again, check with the hostname command to see the results.

Using the hostname command to set your hostname is not permanent . When you restart your computer, your settings will expire. in order to change permanently , you must manually modify the hostname configuration file.

Linux in the Debian/ubuntu system

You can find this file in the /etc/hostname and /etc/hosts folders.

Here are the contents of each file


# Vi/etc/hostnamedev-machine


# vi/etc/hosts127.0.0.1 localhost127.0.0.1 Dev-machine

You will find that you do not have to restart your Linux and it will take effect immediately.

Linux in the Redhat/centos system

You can find this file in the /etc/hosts and /etc/sysconfig/networks folders.

Here are the contents of each file

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


How to display DNS domain names

From the above definition of hostname, hostname can also display your Linux DNS name. If your hostname command displays your hostname, then the DnsDomainName command will display your domain name as well. Take a look at this simple example.


In this post, the result of the DnsDomainName command is

If you see the result is (none), then your machine is not configured with the FQDN (Fully qualified domain name fully complies with the standard domains) . The DnsDomainName command extracts information from the /etc/hosts file. You should configure it for the FQDN format. The following is a simple example:

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

To show more detail, you can use the parameter- v

$ dnsdomainname-vgethostname () = ' ' resolving ' ' ... Result:h_name= ' ' result:h_aliases= ' dev-machine ' result:h_addr_list= ' '
How to display more details about the hostname

The hostname command can use multiple parameters and aliases, such as the DnsDomainName command, which is an alias. These parameters are useful in the daily operation. The results of the following commands are based on the above configuration of /etc/hosts .

Show IP Address

$ hostname-i192.168.0.104

Show Domain name


Show short hostname $ hostname-s dev-machine

This command will produce the same results as the input hostname.

Display FQDN format


Display detail Information

All parameters, including the above information, can be summarized by using the parameter- v and- D . Let's take a look at an example.

$ hostname-v-dgethostname () = ' ' resolving ' ' ... Result:h_name= ' ' result:h_aliases= ' dev-machine ' result:h_addr_list= ' '

Do you feel familiar? Yes, the result of the operation is the same as the above-mentioned dnsdomainname-v command.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.