Deep understanding of hostname

Source: Internet
Author: User

Modify the hostname of the Linux system I believe everyone is already very familiar with, I have also modified hostname, general modification/etc/sysconfig/network and/etc/hosts two files under the relevant configuration can be. But today there are a few problems, here to tidy up a bit.

1. What is the relationship between the hostname command and the value of the hostname environment variable? Which configuration file is determined by?

1) First look at the relevant values and configuration

[Email protected]_179 ~]# hostnamebaiyu_179[[email protected]_179 ~]# echo $HOSTNAMEBAIYU _179[[email protected]_179 ~] # cat/etc/sysconfig/networknetworking=yesnetworking_ipv6=nohostname=baiyu_179[[email protected]_179 ~]# cat/proc/ Sys/kernel/hostname baiyu_179[[email protected]_179 ~]# cat/etc/hosts127.0.0.1 localhost localhost.localdomain Localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

2) Change the value of hostname in/etc/sysconfig/network before viewing

[[Email protected]_179 ~]# vi /etc/sysconfig/network  1 networking=yes   2 NETWORKING_IPV6=no  3 HOSTNAME=BAIYU_179179[[email protected]_179  ~]# hostname                  BAIYU_179[[email protected]_179 ~]# echo  $HOSTNAME             baiyu_179[[email protected]_179 ~]# cat /proc/ Sys/kernel/hostname baiyu_179[[email protected]_179 ~]# . /etc/sysconfig/network      #重新读取/etc/sysconfig/network and then review [[Email protected]_179 ~]# hostname                 baiyu_179[[ email protected]_179 ~]# echo  $HOSTNAME      #发现HOSTNAME改变了BAIYU_179179 [ [EMAIL PROTECTED]_179 ~]#&Nbsp;cat /proc/sys/kernel/hostname baiyu_179 

The above shows that the value of the environment variable hostname is configured by/etc/sysconfig/network

3) Change the value of/proc/sys/kernel/hostnmae and view

[[Email protected]_179 ~]# echo Test >/proc/sys/kernel/hostname[[email protected]_179 ~]# cat/proc/sys/kernel/ Hostname test[[email protected]_179 ~]# hostname test[[email protected]_179 ~]# echo $HOSTNAMEBAIYU _179179[[e Mail protected]_179 ~]# hostname xj[[email protected]_179 ~]# hostnamexj[[email protected]_179 ~]# cat/proc/sys/kernel/h Ostname xj[[email protected]_179 ~]# echo $HOSTNAMEBAIYU _179179

Check the relevant information to know that the hostname is a kernel parameter under the Linux system, it is saved under/proc/sys/kernel/hostname, but its value is Linux boot from /etc/rc.d/rc.sysinit read, and /etc/rc.d/rc.sysinit in the/etc/sysconfig/network under the hostname so far, we finally thoroughly understand.

There is a change in the value and/proc/sys/kernel/hostname value of the hostname command, and the other changes accordingly.

2. Does hostname have anything to do with/etc/hosts configuration?

From the above experiment, hostname and/etc/hosts under the configuration is not related. Hostname changes and changes are completely independent of the hosts file. The Hosts file works quite like DNS, providing the IP address to hostname correspondence. The number of early Internet computers is low, and the stand-alone hosts file is sufficient to store all networked computers. However, 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.

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. The/etc/hosts file usually contains these 2 records

[Email protected]_179 ~]# cat/etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4: : 1 localhost localhost.localdomain localhost6 localhost6.localdomain6

The Hosts file format is a single line of records, IP address, host name, host alias, three separated by white space characters, host alias Optional.

127.0.0.1 to localhost This recommendation does not change because many applications use this, such as SendMail, which may not work properly after the modification. after modifying hostname, if you want to use Newhostname to access it on this computer, you must add a newhostname record in the/etc/hosts file. For example, my eth0 IP is 192.168.100.179, I will change the hosts file as follows:

1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 2:: 1 localhost Localhost.locald Omain localhost6 localhost6.localdomain6 3 192.168.100.179 www.xj.com xj.com

I have always thought that the hostname and/etc/hosts are not related, but in the/etc/rc.d/rc.sysinit script file found such a 2-paragraph setting, so in special cases hostname can not say with/etc/hosts a little relationship.

 hostname=$ (/bin/hostname)   9  10 set  -m 11  12 if [ -f /etc/sysconfig/network ]; then 13      . /etc/sysconfig/network 14 fi 15 if [ -z   $HOSTNAME  -o  $HOSTNAME  =  (none)  ]; then 16      hostname=localhost 17 fi 
341         ipaddr=342          if [  "$HOSTNAME"  =  "localhost"  -o  "$HOSTNAME"  =  " Localhost.localdomain " ]; then343                  ipaddr=$ (ip addr show to 0.0.0.0/0 scope  global | awk  '/[[:space:]]inet / { print gensub ("/.*", "" "," G ", $ $)  } ') 344                 for  ip in  $ipaddr  ; do345                          HOSTNAME=346                            eval $ (ipcalc -h  $ip  2>/dev/null) 347                          [ - n  "$HOSTNAME"  ] && { hostname ${hostname} ; break; }348                  done349          fi

The first script determines whether the value of the hostname command is empty or "(none)" is the case hostname is localhost

      The second script determines whether hostname is localhost or localhost.localdomain, and if so, will use the IP address in the/etc/hosts Hostname to reset the system's hostname. The problem is here, my/etc/sysconfig/network default hostname is Localhost,eth0 IP is 192.168.100.179, and/etc/ The hosts have 192.168.100.179 records. So he replaced the hostname with the 192.168.100.179 record. So some people modify the hostname directly in the/etc/ The hosts add a line of IP and the corresponding host name.

experiment: Modify the hosts, network files, and the modified values are as follows

[Email protected]_179 ~]# Cat/etc/sysconfig/networknetworking=yesnetworking_ipv6=nohostname=localhost[[email         protected]_179 ~]# cat/etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.100.179 www.xj.com xj.com

Restart, and then view

[[email protected] ~]# hostnamewww.xj.com[[email  protected] ~]# echo  $HOSTNAMEwww. Xj.com[[email protected] ~]# cat /proc/sys /kernel/hostname www.xj.com[[email protected] ~]# cat /etc/sysconfig/ networknetworking=yesnetworking_ipv6=nohostname=localhost[[email protected] ~]# cat /etc/ hosts127.0.0.1   localhost localhost.localdomain localhost4  localhost4.localdomain4::1         localhost  Localhost.localdomain localhost6 localhost6.localdomain6192.168.100.179 www.xj.com xj.com 

3. There are several ways to modify hostname

1) hostname hostname Effective immediately after the operation (new session effective), but after the system restarts will lose the changes made;

2) echo HOSTNAME >/proc/sys/kernel/hostname takes effect immediately after operation (new session takes effect), but changes are lost after the system restarts;

3) Sysctl Kernel.hostname=hostname effective immediately after the operation (new session effective), but after the system restarts will lose the changes made;

4) Modify the hostname variable under/etc/sysconfig/network, which requires a reboot to be effective and permanently modified.

5) Modify the/etc/hosts, and the value of hostname under/etc/sysconfig/network is localhost or localhost.localdomain

4. How to make an immediate effect without restarting the operating system after modifying the hostname

after modifying the hostname in the/etc/sysconfig/network, then use the hostname command to change to the same hostname (which can also be counted as a method?). )

5. What is the value of the hostname?

[[email protected] ~]# hostnamewww.xj.com[[email  protected] ~]# echo  $HOSTNAMEwww. Xj.com[[email protected] ~]# cat /proc/sys /kernel/hostname www.xj.com[[email protected] ~]# cat /etc/sysconfig/ networknetworking=yesnetworking_ipv6=nohostname=localhost[[email protected] ~]# cat /etc/ hosts127.0.0.1   localhost localhost.localdomain localhost4  localhost4.localdomain4::1         localhost  Localhost.localdomain localhost6 localhost6.localdomain6192.168.100.179 www.xj.com xj.com 

I think you should have the answer.

Summarizing the basic knowledge of hostname, I hope that beginners like me will no longer be confused about this problem.

Deep understanding of hostname

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.