Hostname modification of "turn" Linux

Source: Internet
Author: User
Tags aliases


Original link http://soft.chinabyte.com/os/281/11563281.shtml

 The Linux operating system's hostname is a kernel variable that can be viewed by hostname commands to view the hostname of the machine.  You can also view it directly cat/proc/sys/kernel/hostname.  #hostname #cat/proc/sys/kernel/hostname The above two output results are the same. Modify the runtime Linux system hostname, that is, do not need to restart the system hostname command can set the system hostname #hostname newname newname is to set the new hostname, run immediately after the effective,  However, after the system restarts will lose the changes, if you want to permanently change the system's hostname, you need to modify the relevant settings file. Permanently change Linux hostname man hostname There's a saying, "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 have this 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. Therefore, if you want to permanently modify the Redhat hostname, modify the/etc/sysconfig/network file, the hostname in the line to change into Hostname=newname,  Where newname is the hostname you want to set up.  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 is very extraordinary. Hostname the first thought of modifying the/etc/hosts file, the hostname configuration file is/etc/hosts.  Actually, it's not. The Hosts file works quite like DNS, providing the IP address to hostname correspondence. Early Internet computers were few, stand-alone HThe OSTs file is 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. /etc/hosts files usually contain this record 127.0.0.1 localhost.localdomain localhost Hosts file format is one line of records, respectively, IP address hostname aliases,  The three are separated by white space characters, aliases 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.1.61, I will change the hosts file as follows: #hostname blog.infernor.net # cat/etc/hosts 127.0.0.1 localhost.localdomain L  Ocalhost 192.168.1.61 blog.infernor.net Blog This way, I can access the computer via blog or blog.infernor.net. From the above,/etc/hosts is not directly related to setting up hostname, only when you want to use the new hostname on this machine to access your own time to use the/etc/hosts file.  There is no definite connection between the two.  There is another problem with Rhel. I started in the test, only modify/etc/hosts, add 192.168.1.61 blog.infernor.net Blog, and/etc/sysconfig/network maintain the original, that is, the hostname= inside Localhost.localdomain. I restarted the system unexpectedly found hostname to change into a blog.infernor.net. In this way, I really think/etc/hosts is a hostname configuration file. Finally, I found the problem in/etc/rc.d/rc.sysinit this startup script.  hostname if [-f/etc/sysconfig/network] is set at the beginning of the Rc.sysinit file; Then. /etc/sysconfig/network fi if [-z] $HOSTNAME "-o" $HOSTNAME "=" (none) "]; Then Hostname=localhost Fi did use the HOSTNAME value in/etc/sysconfig/network. But then there's a ipaddr= if ["$HOSTNAME" = "localhost"-o "$HOSTNAME" = "localhost.localdomain"] for setting HOSTNAME; Then ipaddr=$ (IP addr Show to 0/0 scope Global | awk '/[[:space:]]inet/{print gensub ("/.*", "", "G", $ $)} ') if [-N "$ IPAddr "]; Then eval $ (ipcalc-h $ipaddr 2>/dev/null) hostname ${hostname} fi fi script to determine if hostname is localhost or localhost.localdom Ain, if so, will use the hostname of the interface IP address to reset the system's hostname. The problem is here, my/etc/sysconfig/network default hostname is Localhost.localdomain,eth0 IP is 192.168.1.61, and/etc/ The hosts have 192.168.1.61 records.  So he replaced the hostname with the 192.168.1.61 record.  It is also estimated that this is why many people mistakenly think of/etc/hosts as a hostname configuration file. Hostname with the option query hostname-s-f-I and so on options are used in the/etc/hosts or DNS system, and we discussed the hostname a bit far away, but also prone to misunderstanding. Specific can be man hostname view.

Hostname modification of "turn" Linux

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.