Detailed explanation of Linux hostname command

Original address: http://blog.csdn.net/qhairen/article/details/45913465

1. View the hostname of the machine

#hostname

 

2. Modify the hostname of the machine

#hostname newname


It takes effect immediately after running (restart the shell) , but the modifications will be lost after the system restarts. If you want to permanently change the hostname of the system, you must modify the relevant configuration files.

For the hostname of RedHat, modify the /etc/sysconfig/network file, and modify the HOSTNAME line in it to HOSTNAME=NEWNAME, where NEWNAME is the hostname you want to set.
The Debian distribution's hostname configuration file is /etc/hostname.
After modifying the configuration file, restarting the system will read the configuration file to set a new hostname.

The relationship between hostname and /etc/hosts
Many people first think of modifying the /etc/hosts file when they mention changing the hostname, thinking that the configuration file of the hostname is /etc/hosts. Actually not.
The role of the hosts file is quite like DNS, providing the correspondence between IP addresses and hostnames. The early Internet computers were few, and the hosts file of a single machine was enough to store all networked computers. But with the development of the Internet, this is far from enough. So there is a distributed DNS system. A similar IP address to domain name correspondence is provided by a DNS server. Specifically, you can man hosts.
The Linux system will query the /etc/hosts file before sending a domain name resolution request to the DNS server. If there is a corresponding record in it, the record in the hosts will be used.

 

 

There are several related to hostname in Linux :
1. There is a HOSTNAME environment variable in the system, use the command echo $HOSTNAME to display the hostname (host name)
2. There is also a hostname command: it has several functions:
a) Query the hostname:
                        i. hostname //Display the short format of the host name, such as localhost;
                       ii. hostname -f //Display the long format of the host name, with the domain name, for example: localhost.localdomain
                      iii. hostname –d //Display the domain name, such as localdomain iv>.hostname –I //Display the IP address corresponding to the hostname
                       iv. hostname –a //Display the host alias (alias), the same as the output of hostname, such as localhost
                       v. hostname –s //Display the short format of the host name, that is, the part before the first comma on the left, such as localhost.localdomain before the comma is localhost
b) Set the hostname: The hostname set with the command is temporary, and the set hostname will be lost after the system is restarted.
                         i. hostname newname //Set the hostname to newname
3. Permanently modify the hostname
用hostname修改的hostname是暂时的,如果想永久的修改hostname, 则必须修改相应的配置文件。
 
man hostname里有这么一句话,”The host name is usually set once at system startup in /etc/rc.d/rc.inet1 or /etc/init.d/boot (normally by reading the contents of a file which contains the host name, e.g. /etc/hostname).”. CentOS中没有这句话举例说的hostname文件,而是由/etc/rc.d/rc.sysinit这个脚本负责设置系统的hostname,它读取/etc /sysconfig/network这个文本文件,RedHat的hostname就是在这个文件里设置。
 
暂时放下/etc/sysconfig/network配置方法,先了解不带参数的hostname和hostname -a –d –f, -i的区别: hostname是调用gethostname(2)函数,而hostname -a –d –f, -i 命令是调用gethostbyname(3)函数;gethostname(2)和gethostbyname(3)的区别就是gethostbyname(3)的网络可知性,因此gethostbyname(3)函数需要询问/etc/nsswitch.conf和/etc/host.conf文件来决定是从/etc/sysconfig/network配置文件里,还是从/etc/hosts配置文件里读取信息。
 
因此配置hostname有两个配置文件:
1、  /etc/sysconfig/network:没有/etc/hosts的功能只是提供一个主机名
功能介绍: 没有/etc/hosts的功能只是提供一个主机名, 它里面的hostname被/etc/rc.d/rc.sysinit读取
配置方法: 用vim打开network文件,更改HOSTNAME=NEWNAME; 这样hostname名就被改为newname了

2、/etc/hosts:
它和网络相关的,相当于DNS,提供IP地址到hostname的对应
配置方法: 用vim打开hosts文件,它的配置格式是: IP FQDN alias, 比如192.168.1.1 localhost.localdomain localhost,其中IP=192.168.1.1, FQDN=localhost.localdomain, alias=localhost
 
注意: 一定要让配置文件network中的hostname和hosts中的alias以及FQDN的第一部分一致,否则hostname -a -f -d -i命令不能正常工作。
 
令:遇到问题,在debian系统中更改/etc/hosts和/etc/hostname文件后,使用 uname -n或uname-a
命令查看还是原来的主机名称,此时需要执行/etc/init.d/hostname.sh stop; sudo /etc/init.d/hostname.sh start;hostname才能生效。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326115033&siteId=291194637