Linux hostname Command Detailed

Source: Internet
Author: User

1, check the hostname of the machine

#hostname


2, modify the hostname of the machine

#hostname newname


Takes effect immediately after running (restarting the shell), but changes are lost after the system restarts, and if you want to permanently change the system's hostname, modify the associated configuration file.

Redhat hostname, modify/etc/sysconfig/network file, will be inside the hostname this line to modify into Hostname=newname, where NEWNAME is the hostname you want to set.
The hostname configuration file for the Debian distribution is/etc/hostname.
After the configuration file is repaired, a new hostname is read from the configuration file when the system is restarted.

The relationship between hostname and/etc/hosts
A lot of people mention change hostname first think of modifying/etc/hosts file, think hostname configuration file is/etc/hosts. Actually, it's not.
The Hosts file works quite like DNS, providing the IP address to hostname correspondence. The early Internet computers were few, and the stand-alone hosts file was sufficient to store all networked computers. But with the development of the Internet, this is far from enough. A distributed DNS system is then present. The DNS server provides a similar IP address to the corresponding domain name. Specific can man hosts.
The Linux system queries the/etc/hosts file before issuing a domain name resolution request to the DNS server, and if it has a corresponding record, it will use the records inside the hosts.


there are several related in Linux and hostname:1. There are hostname environment variables in the system, with the command echo $HOSTNAME will display the hostname (host name)2. There is also a hostname command: It has several functions:a) query host name:I. hostname//Display host name short format, such as localhost;II. HOSTNAME-F//Display the long format of hostname, with domain name, for example: Localhost.localdomainIII. HOSTNAME–D//display domain name, such as Localdomain iv>. Hostname–i//Displays the IP address of the host nameIv. hostname–a//Display host aliases (alias), like the output of hostname, such as localhostv. Hostname–s//Displays the short format of the hostname, i.e. from the front part of the first comma on the left, such as Localhost.localdo Main comma front is localhostb) Set hostname: the hostname set by the command is temporary and the hostname is lost after the system restarts.
i. Hostname newname//Set host name to NewName3. Permanently modify hostnameThe hostname modified with hostname is temporary, and if you want to permanently modify the hostname, you must modify the corresponding configuration file.  there is a saying in man hostname that "the host name is usually set once at system startup In/etc/rc.d/rc.inet1 Or/etc/init.d/boot (no Rmally by reading the contents of a file which contains the host name, e.g./etc/hostname). ". CentOS does not have this sentence example of hostname file, but by/etc/rc.d/rc.sysinit this script is responsible for setting the system hostname, it reads/etc/sysconfig/network this text file, Redhat's hostname is set in this file.  temporarily put down the/etc/sysconfig/network configuration method, first understand the hostname and hostname-a–d–f without parameters, the difference between-I: hostname is to invoke the GetHostName (2) function, and hostname -a–d–f, the-i command is called the gethostbyname (3) function,the difference between gethostname (2) and gethostbyname (3) is the network of gethostbyname (3), So the gethostbyname (3) function needs to ask the/etc/nsswitch.conf and/etc/host.conf files to determine whether it is from the/etc/sysconfig/network configuration file or from the/etc/ The hosts configuration file reads the information.  Therefore, the configuration hostname has two configuration files:1,/etc/sysconfig/network: No/etc/hosts function just provide a host namefunction Description: No/etc/hosts function just provide a hostname, it hostname is/etc/rc.d/rc.sysinit readConfiguration method: Use VIM to open the network file, change hostname=newname; So the hostname name is changed to NEWNAME.

2,/etc/hosts:It is related to the network, the equivalent of DNS, providing IP address to the corresponding hostnameConfiguration method: Open the Hosts file with vim, its configuration format is: IP FQDN alias, such as 192.168.1.1 localhost.localdomain localhost, where ip=192.168.1.1, fqdn= Localhost.localdomain, Alias=localhost Note: Be sure to make the hostname in the profile network consistent with the alias in the hosts and the first part of the FQDN, otherwise the hostname-a-f-d-i command will not work correctly.

Linux hostname Command Detailed

Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.