Linux hostname modification details

Source: Internet
Author: User
Article Title: linux hostname modification details. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

The hostname of the Linux operating system is a kernel variable. You can run the hostname command to view the hostname of the local machine. You can also view it in cat/proc/sys/kernel/hostname.

# Hostname

# Cat/proc/sys/kernel/hostname

The above two outputs are the same.

Modify the hostname of the Linux system at runtime, that is, you do not need to restart the system.

The hostname command can be used to set the system's hostname.

# Hostname newname

Newname is the new hostname to be set, which takes effect immediately after running, but changes will be lost after the system is restarted. To change the hostname of the system permanently, modify the relevant settings file.

Change the Linux hostname permanently

In 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 ). "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.

Relationship between hostname and/etc/hosts

Once mentioned, the first thought of modifying the hostname is to modify the/etc/hosts file. The configuration file of hostname 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.

127.0.0.1 localhost. localdomain localhost

The hosts file is in the format of one record per line, namely, the IP address hostname aliases. The three are separated by blank characters. aliases is optional.

We recommend that you do not modify the configuration from 127.0.0.1 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 192.168.1.61, modify the hosts file as follows:

# Hostname blog.infernor.net

# Cat/etc/hosts

127.0.0.1 localhost. localdomain localhost

192.168.1.61 blog.infernor.net blog

In this way, I can access the local machine through blog or blog.infernor.net.

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 192.168.1.61 blog.infernor.net 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 blog.infernor.net. 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.

The rc. sysinit file sets the hostname from the very beginning.

If [-f/etc/sysconfig/network]; then

./Etc/sysconfig/network

Fi

If [-z "$ HOSTNAME"-o "$ HOSTNAME" = "(none)"]; then

HOSTNAME = localhost

Fi

The hostname value in/etc/sysconfig/network is used. However, there is another section about setting the hostname

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

Fi

The script checks whether the hostname is localhost or localhost. localdomain. If yes, it uses the hostname corresponding to the interface IP address 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 192.168.1.61, And the/etc/hosts contains the records of 192.168.1.61. So we replaced the hostname with the record 192.168.1.61.

It is estimated that this is why many people mistakenly think of/etc/hosts as the hostname configuration file.

Hostname with option Query

Hostname-s-f-I and other options 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: 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.