Linux hostname modification details

Source: Internet
Author: User
Linux hostname modification explanation Linux operating system hostname is a kernel variable, you can use the hostname command to view the local hostname. You can also directly view the catprocsyskernelhostname. Hostnamecatprocsyskernelhost linux hostname modification details Linux operating system hostname is a kernel variable, you can use the hostname command to view the local hostname. You can also view it in cat/proc/sys/kernel/hostname. # Hostname # cat/proc/sys/kernel/hostname the two output results are the same. Modify the hostname of the Linux system at runtime, that is, you do not need to restart the system hostname command. you can set the system hostname # hostname newname, that is, the new hostname to be set, which takes effect immediately after running, however, after the system is restarted, the modification will be lost. if you want to change the hostname of the system permanently, you need to modify the relevant settings file. There is a permanent change to The hostname man hostname in Linux, "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 ). "RedHat does not contain this file, but is composed of/etc/rc. d/rc. the sysinit script is responsible for setting the system's hostname. it reads the/etc/sysconfig/network text file, and the RedHat hostname is set in this file. Therefore, if you want to permanently modify the RedHat hostname, modify the/etc/sysconfig/network file and change the HOSTNAME line to HOSTNAME = NEWNAME, NEWNAME is the hostname you want to set. The configuration file of the Debian release hostname is/etc/hostname. After the configuration file is repaired, the system restarts and reads the configuration file to set a new hostname. The relationship between hostname and/etc/hosts is also mentioned. when you change the hostname, you first need to modify the/etc/hosts file and think that the hostname configuration file is/etc/hosts. Actually not. The hosts file acts like DNS and provides the corresponding IP address to the hostname. There were few computers on the Internet in the early days, and the hosts file on a single machine was enough to store all the computers on the Internet. However, with the development of the Internet, this is far from enough. As a result, a distributed DNS system emerged. The DNS server provides a similar IP address to the corresponding domain name. Man hosts is supported. 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 hosts will be used. The/etc/hosts file usually contains this record localhost. localdomain localhost hosts file in the format of one line, which is the IP address hostname aliases. The three are separated by blank characters, which is optional for aliases. We recommend that you do not modify the configuration from to localhost, because many applications use this configuration, such as sendmail. after modification, these programs may not run properly. After the hostname is modified, if you want to access the host with newhostname, you must add a newhostname record in the/etc/hosts file. For example, if my eth0 IP address is, modify the hosts file as follows: # hostname # cat/etc/hosts localhost. localdomain localhost blog so that I can access the local machine through blog or From the above, it doesn't matter whether/etc/hosts sets the hostname directly, the/etc/hosts file is used only when you want to access your host with a new hostname. There is no bound relationship between the two. RHEL has another problem. When I started the test, I only modified/etc/hosts and added blog to it, while/etc/sysconfig/network remained the same, that is, HOSTNAME = localhost. localdomain. After I restarted the system, I found that the hostname was changed to In this case, I think/etc/hosts is the configuration file of hostname. Later, I found the problem in the/etc/rc. d/rc. sysinit startup script. Rc. the sysinit file sets hostname if [-f/etc/sysconfig/network]; then. /etc/sysconfig/network fi if [-z "$ HOSTNAME"-o "$ HOSTNAME" = "(none)"]; then HOSTNAME = localhost fi does use the hostname value in/etc/sysconfig/network. However, there is another section about ipaddr = if ["$ hostname" = "localhost"-o "$ HOSTNAME" = "localhost. localdomain "]; then ipaddr =$ (ip addr show to 0/0 scope global | awk '/[[: space:] inet/{print gensub ("/. * "," "," g ", $2)} ') if [-n" $ ipaddr "]; then eval $ (ipcalc-h $ ipaddr 2>/dev/null) hostname $ {HOSTNAME} fi script to determine whether the hostname is localhost or localhost. localdomain. If yes, the hostname corresponding to the interface IP address will be used to reset the system hostname. The problem is that the default hostname of my/etc/sysconfig/network is localhost. localdomain, the IP address of eth0 is, and the/etc/hosts contains the records of So we replaced the hostname with the record It is estimated that this is why many people mistakenly think of/etc/hosts as the hostname configuration file. Hostname-s-f-I with the option query hostname and so on are all used in the/etc/hosts or DNS system. the hostname discussed with us is a little far away and may be misunderstood. For more information, see man hostname.
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: 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.