Linux-hostname-details

Source: Internet
Author: User
Tags aliases

Transferred from: http://www.cnblogs.com/kerrycode/p/3595724.html

When I think of the Linux system modified hostname is already very familiar with, today encountered a few problems, these questions give me a good lesson, a lot of knowledge points, when you think you have mastered, in fact, you know is only fur. Technical work, do not taste the stop!

Lab Environment: Red Hat Enterprise Linux Server release 5.7 (Tikanga), other versions of Linux may be different. Please take the actual circumstances as the subject.

In fact, I have modified hostname many times, generally only need to modify the/etc/hosts and/etc/sysconfig/network two files under the relevant configuration can be. However, today I have two questions:

Question 1: why/etc/sysconfig/ The hostname in the network configuration file is Localhost.localdomain, but the hostname shown is po132345806-a, where is the configuration value for hostname?

1: [[email protected] ~]# more/etc/hosts 2: # do not remove the following line, or various programs 3: # that Require network functionality would fail. 4:127.0.0.1 localhost.localdomain localhost 5::: 1 localhost6.localdomain6 localhost6 6: [Email protected] ~]# more/etc/sysconfig/network 7:networking=yes 8:networking_ipv6=yes 9:hostname=localhost.localdomain

There is a picture of the truth, lest people do not believe this phenomenon, when I first encountered this special situation, I was very puzzled. Google has some information to add its own practice to understand

Issue 2: After modifying the hostname, how to make it effective immediately without restarting the operating system.

3: How many different ways to modify hostname?

Does the problem 4:hostname have anything to do with/etc/hosts configuration?

Question 5: How do I see the value of hostname, whichever is that?

Question 1 Answer : I always thought that the value of hostname is configured in/etc/sysconfig/network, this file inside hostname configuration Why, hostname value is what. But why does that happen? Does/etc/sysconfig/network

Not a hostname configuration file, is there another configuration file? So I was experimenting. Modified/etc/sysconfig/network file hostname to Db-server, found

The value of the hostname is still unchanged, so the computer is restarted

   1: "/etc/sysconfig/network" 3L, 66C written
   2:132345806-A ~]# hostname
   3:806-a.gfg1.esquel.com
   4:132345806-a ~]# More/proc/sys/kernel/hostname
   5:806-a.gfg1.esquel.com
   6:132345806-a ~]# sysctl Kernel.hostname
   7:ostname = po132345806-a.gfg1.esquel.com
   8:132345806-a ~]#
   9:132345806-a ~]# reboot

Restart after the discovery incredibly hostname into db-server, that is, modify the configuration file/etc/sysconfig/network under the hostname effective. So that means someone has modified the kernel.hostname, see the following experiment

   1: [[email protected] ~]# more/etc/sysconfig/network
   
   3:networking=yes
   
   5:networking_ipv6=yes
   
   7:hostname=db-server.localdomain
   
   9: [[email protected] ~]# echo Test >/proc/sys/kernel/hostname
  
  One: [[email protected] ~]# More/etc/proc/sys/kernel/hostname
  
  :/etc/proc/sys/kernel/hostname:no such file or directory
  
  : [email protected] ~]# More/proc/sys/kernel/hostname
  
  17:test
  
  : [[email protected] ~]#/etc/init.d/network restart
  
  21:shutting down interface eth0: [OK]
  
  23:shutting down Loopback interface: [OK]
  
  25:bringing up Loopback interface: [OK]
  
  27:bringing up Interface eth0:
  
  29:determining IP information for eth0 ... done.
  
  : [OK]
  
  : [[email protected] ~]# hostname
  
  35:test
  
  
  

Note: In fact/etc/init.d/network restart no use. It was only when the experiment thought that the network service had to be restarted.

Create a new clone in SecureCRT a session found that Hostanme has changed from db-server to test, but the value of/etc/sysconfig/network is Db-server.localdomain and has not changed to test.

   1: [[email protected] ~]# more/etc/sysconfig/network
   
   3:networking=yes
   
   5:networking_ipv6=yes
   
   7:hostname=db-server.localdomain
   
   9: [[email protected] ~]# hostname
  
  11:test
  
  : [email protected] ~]# more/etc/hosts
  
  : # Do not remove the following line, or various programs
  
  : # that require network functionality would fail.
  
  19:127.0.0.1 localhost.localdomain localhost
  
  £ º: 1 localhost6.localdomain6 LOCALHOST6
  
  at: [email protected] ~]# More/proc/sys/kernel/hostname
  
  25:test
  
  
  

However, if you restart the system hostname will become db-server,google some English documents to know, hostname is a kernel parameter under the Linux system, it is saved under/proc/sys/kernel/hostname, However, its value is read from Rc.sysinit when Linux is started.

hostnameis a kernel parameter which stores hostname of the system. Its location is"/proc/sys/kernel/hostname"

The value for this parameter are loaded to kernel by rc.sysinit file during the boot process.

and the value of hostname in/etc/rc.d/rc.sysinit from/etc/sysconfig/network hostname, the code is as follows, so far, we can completely understand.

Hostname= '/bin/hostname '

Hosttype= ' Uname-m '

Unamer= ' Uname-r '

Set-m

if [-f/etc/sysconfig/network]; Then

. /etc/sysconfig/network

Fi

If [-Z] $HOSTNAME "-o" $HOSTNAME "=" (none) "]; Then

Hostname=localhost

Fi

Conclusion :/etc/sysconfig/network is indeed a hostname configuration file, and the value of hostname is related to the hostname in the configuration file, but it does not have a definite relationship. The value of the hostname is from the kernel parameter/proc/sys/kernel/hostname, and if I change the kernel parameters by command sysctl kernel.hostname=test, then hostname becomes test.

Issue 2: after modifying the hostname, how to make it effective immediately without restarting the operating system.

Method 1: After you modify the hostname under/etc/sysconfig/network, then use echo servername >/proc/sys/kernel/hostname.

[Email protected] ~]# echo Test >/proc/sys/kernel/hostname

Note that the current session still does not change, but subsequent new sessions will take effect.

Method 2: After modifying the hostname under/etc/sysconfig/network, then use the Sysctl kernel.hostname command to make it effective immediately

[Email protected] ~]# sysctl Kernel.hostname=test2

Kernel.hostname = Test2

Note that the current session still does not change, but subsequent new sessions take effect.

Method 3: After modifying the hostname under/etc/sysconfig/network, then use the hostname command to make it effective

[Email protected] ~]# hostname db-server

Note that the current session still does not change, but subsequent new sessions take effect.

In fact, these methods are only combined with permanent modification and temporary modification of hostname, so that it does not have to restart the Linux server, haha, do not know you understand.

3: How many different ways to modify hostname?

1:hostname Db-server--effective immediately after operation (new session takes effect), but changes are lost after the system restarts

2:echo db-server >/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=db-server--effective immediately after operation (new session takes effect), but changes are lost after the system restarts

4: Modify the hostname variable under/etc/sysconfig/network--requires a reboot to take effect, permanent modification.

Question 4: does the hostname have anything to do with/etc/hosts configuration?

If from the above experiment, actually hostname and/etc/hosts under the configuration is not related. Hostname changes and changes are completely independent of the hosts file. In fact, the hosts file functions quite like DNS, providing 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. 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. Specifically, man hosts can view relevant information.

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

The Hosts file format is a row of records, respectively, IP address, hostname, aliases, 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.

But, in fact, hostname is not to say with/etc/hosts a little relationship is not. In/etc/rc.d/ Rc.sysinit, the following logic is determined, when the hostname is localhost localhost.localdomain, will use the interface IP address corresponding to the hostname to reset the system hostname.

# In theory there should is no more than one network interface active

# This early in the boot process – the one we ' re booting from.

# Use the network address to set the hostname of the client. This

# must be do even if we have local storage.

Ipaddr=

If ["$HOSTNAME" = "localhost"-o "$HOSTNAME" = "Localhost.localdomain"]; 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

Let's experiment with it, modify the hosts, network files, and the modified values are as follows:

[Email protected] ~]# more/etc/hosts

# don't remove the following line, or various programs

# that require network functionality would fail.

:: 1 localhost.localdomain localhost

127.0.0.1 localhost.localdomain localhost

192.168.244.128 Db-server.localdomain Db-server

[Email protected] ~]# more/etc/sysconfig/network

Networking=yes

Networking_ipv6=yes

Hostname=localhost.localdomain

After rebooting the system, let's look at the situation:

So that's why sometimes people think that the value of hostname is related to the hosts file.

Question 5: How do I see the value of hostname, whichever is that?

[Email protected] ~]# hostname

Db-server

[Email protected] ~]# More/proc/sys/kernel/hostname

Db-server

[Email protected] ~]# more/etc/sysconfig/network

Networking=yes

Networking_ipv6=yes

Hostname=localhost.localdomain

If you understand the previous 4 questions, then it is easy to understand the problem.

Linux-hostname-details

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.