Deep understanding of Linux modification hostname (recommended) _linux

Source: Internet
Author: User
Tags aliases require sessions

When I think of the Linux system modified hostname already very familiar with the time, today encountered a few problems, these several questions for me a good lesson, a lot of knowledge points, when you think you have mastered the time, in fact, you know is still only fur. Technical life, do not have to be tasted then stop!

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

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

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

[Root@po132345806-a ~]# more/etc/hosts

 # does not remove the following line, or various programs

# that require net Work functionality would fail. 

127.0.0.1 localhost.localdomain localhost 

:: 1 localhost6.localdomain6 localhost6 

[root@po132345806-a ~]# More/etc/sysconfig/network

networking=yes 

networking_ipv6=yes

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, plus your own practice to figure it out.

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

Question 3: How many ways are there to modify hostname?

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

Question 5: How to view the value of the hostname, whichever is that?

Question 1 Answer: I always thought hostname's value is configured in/etc/sysconfig/network, this file inside hostname Configure why, hostname value is what. But why did the above happen? Isn't/etc/sysconfig/network

Not a hostname profile, is there another configuration file? So I experimented with it. Modified the/etc/sysconfig/network file hostname as Db-server, found that the value of hostname is still unchanged, and then restarted the computer

  "/etc/sysconfig/network" 3L, 66C written
   132345806-a ~]# hostname 806-a.gfg1.esquel.com 132345806-a ~]#
   More/proc/sys/kernel/hostname
   806-a.gfg1.esquel.com
   132345806-a ~]# sysctl kernel.hostname ostname
   = po132345806-a.gfg1.esquel.com
   132345806-a ~]#
   132345806-a ~]# reboot

After the reboot found unexpectedly hostname changed into db-server, that is to modify the configuration file/etc/sysconfig/network under the hostname effective. So that means someone should have modified the kernel.hostname, please see the following experiment

 [Root@db-server ~]# more/etc/sysconfig/network
   
   networking=yes
   
   networking_ipv6=yes
   
   HOSTNAME= Db-server.localdomain
   
   [Root@db-server ~]# echo Test >/proc/sys/kernel/hostname
   
  [root@db-server ~]# more/ Etc/proc/sys/kernel/hostname
   
  /etc/proc/sys/kernel/hostname No such file or directory
   
  [root@db-server ~]# more /proc/sys/kernel/hostname
   
  Test
   
  [root@db-server ~]#/etc/init.d/network restart
   
  shutting down interface ETH [OK]
   
  shutting down loopback interface [OK]
   
  bringing up loopback interface [OK]
   
  bringing up Interf Ace ETH
   
  determining IP information for eth0 ... done.
   
  [OK]
   
  [Root@db-server ~]# hostname
   
  Test
   
  [Root@db-server ~]#   

Note: In fact/etc/init.d/network Restart is of little use. It was only at the time that the experiment thought that the network service must be restarted.

New clone in SecureCRT a session found that Hostanme had changed from Db-server to test, but the/etc/sysconfig/network value was db-server.localdomain and did not change to test.

 [Root@test ~]# more/etc/sysconfig/network
   
   networking=yes
   
   networking_ipv6=yes
   
   hostname= Db-server.localdomain
   
   [root@test ~]# hostname
   
  Test
   
  [root@test ~]# # does not
   
  remove the Following line, or various programs
   
  # that require network functionality would fail.
   
  127.0.0.1 localhost.localdomain localhost
   
  1 localhost6.localdomain6 localhost6
   
  [root@test ~]# more/proc/ Sys/kernel/hostname
   
  Test
   
  [Root@test ~]#   

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

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

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

And the value of the hostname in the/etc/rc.d/rc.sysinit comes from the hostname under the/etc/sysconfig/network, the code is as follows, so 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, the hostname value is related to the hostname in the configuration file, but it is not necessarily related, the hostname value comes from kernel parameter/proc/ Sys/kernel/hostname, if I modify 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 modifying the hostname under/etc/sysconfig/network, then use echo servername >/proc/sys/kernel/hostname.

[Root@db-server ~]# Echo Test >/proc/sys/kernel/hostname

Note that the current session is still unchanged, but subsequent new sessions will take effect.

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

    [Root@db-server ~]# sysctl kernel.hostname=test2

    kernel.hostname = Test2

Note that the current session is still unchanged, but subsequent new sessions will take effect.

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

 [Root@test ~]# hostname db-server

Note that the current session is still unchanged, but subsequent new sessions will take effect.

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

Question 3: How many ways are there to modify hostname?

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

2:echo db-server >/proc/sys/kernel/hostname--effective immediately after run (new session takes effect), but changes will be lost after system reboot

3:sysctl Kernel.hostname=db-server-effective immediately after run (new session takes effect), but changes will be lost after the system restarts

4: Modify the/etc/sysconfig/network under the hostname variable-need to restart the effective, permanent changes.

Is the problem 4:hostname related to the/etc/hosts configuration?

If I see 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 function quite like DNS, provide IP address to hostname correspondence. The number of early Internet computers was small, and the single hosts file was sufficient to store all networked computers. But with the development of the Internet, this is far from enough. The distributed DNS system is then present. By the DNS server to provide a similar IP address to the domain name of the corresponding. You can see the information in the man hosts.

The Linux system queries the/etc/hosts file before sending a domain resolution request to the DNS server, and if it has a corresponding record, it will use the records in the hosts. /etc/hosts files usually contain this record

127.0.0.1 localhost.localdomain localhost

Hosts file format is a row of records, respectively, IP address, hostname, aliases, the three separated by white space characters, aliases optional.

127.0.0.1 to localhost this one is not recommended, because many applications use this, such as SendMail, which may not work properly after modification.

But, in fact, hostname is not said to have a little relationship with/etc/hosts. In/etc/rc.d/ Rc.sysinit, the following logical judgment, when hostname is localhost localhost.localdomain, will use the interface IP address corresponding 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 ' r e booting from.

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

    # must is done even if we have the 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&G t;/dev/null)

            hostname ${hostname}

        fi

    fi

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

[Root@localhost ~]# 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 

[root@localhost ~]# more/etc/sysconfig/network 

Yes 

Networking_ipv6=yes 

hostname=localhost.localdomain

After restarting the system, let's take a look at the situation:

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

Question 5: How to view the value of the hostname, whichever is that?

[Root@db-server ~]# hostname 

db-server

[root@db-server ~]# more/proc/sys/kernel/hostname-Db-server 

[Root@db-server ~]# more/etc/sysconfig/network 

Networking=yes 

Networking_ipv6=yes 

hostname=localhost.localdomain

With that in view, if you understand the first 4 questions, it is easy to understand the problem.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.