About the RedHat hostname

Source: Internet
Author: User

The reason for recording this is that when JBoss is used, server startup will bind a hostname while hostname does not exist under/etc/hosts. some inexplicable problems will be reported during startup!

To put it simply, uname-A can see the hostname. The modification steps are as follows:

  1. Uname-a view hostname
  2. Modify the hostname www.ctohome.com to make the hostname take effect immediately.
  3. Change the original hostname to www.ctohome.com in VI/etc/hosts.
  4. Change the original hostname of VI/etc/sysconfig/network to www.ctohome.com, so that the reboot will take effect after restart.
  5. Reboot restart, uname-a re-check. Close

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 used to set the hostname of the system. It reads/etc.
In the text file/sysconfig/network, the hostname of RedHat 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 www.cthome.com
# Cat/etc/hosts
127.0.0.1 localhost. localdomain localhost
192.168.1.61 www.cthome.com blog
In this way, I can access the local machine through blog or www.cthome.com.

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 www.cthome.com 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 www.cthome.com. 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. This is where the default hostname of my/etc/sysconfig/network is
Localhost. localdomain. The IP address of eth0 is 192.168.1.61, And the IP address of 192.168.1.61 is recorded in/etc/hosts. 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.


Link: http://www.ctohome.com/fuwuqi/1b/41420.2.html

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.