The following operations are based Centos 6.8 operation.
A phenomenon
In normal operation, when the need to modify the host name, we usually do this:
The first step, temporarily modify the hostname by the hostname command.
hostname kwang-test01
The second step, modify the configuration file, restart the machine to ensure the host name will not change.
$ cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=kwang-test01 NOZEROCONF=yes
The above operation is indeed correct posture modify the host name, but also to achieve the desired results, but why does it work this way, but also know these know why, let's look at why.
Second, look beyond the surface
2.1 hostname temporarily modify the hostname
When we perform hostname <ip> command, will temporarily modify the Linux Kernel in a same kernel parameters hostname, and the hostname in Linux Kernel parameters are stored in / proc / sys / kernel / hostname in.
2.2 modify / etc / sysconfig / network configuration permanently modify the hostname
Some people may be wondering why you need to modify the host name permanently modify the HOSTNAME parameter / etc / sysconfig / network of? Before answering this question, let us look at a piece of code scripts Linux starts:
HOSTNAME=$(/bin/hostname) set -m if [ -f /etc/sysconfig/network ]; then . /etc/sysconfig/network fi if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then HOSTNAME=localhost fi
Linux can be seen that the start logic: it reads the parameters HOSTNAME / etc / sysconfig / network, and then configures the system to HOSTNAME hostname parameter acquired.
Note that we also said that only in Linux will load at startup / etc / sysconfig / network configuration, but usually perform hostname command system is how to know the host name of the temporary modification of it? Let's continue reading.
2.3 Further
Acquired by the hostname command value with / etc / sysconfig / network file HOSTNAME have some relevance, but not necessarily linked, there will only be consistent with the HOSTNAME value profile at Linux startup, startup phase of each other. Learn more, we find that the value obtained by the hostname command is not available directly from / etc / sysconfig / network, but the parameters obtained from the kernel of the Linux Kernel / proc / sys / kernel / hostname, which we can see from the following practical operation out:
# Hostname // current host name kwang_test01 # CAT / proc / SYS / Kernel / hostname // modify kernel parameters kwang_test01 # echo " kwang_test01_change " > / proc / SYS / Kernel / hostname // modify kernel parameters # CAT / proc / SYS / Kernel / hostname kwang_test01_change # hostname // after modifying the host name, host name found in the modified kwang_test01_change # CAT / etc / sysconfig / Network // and found that this configuration does not change the value of hOSTNAME NETWORKING = yes hOSTNAME = Kwangtest01 NOZEROCONF=yes
in conclusion:
- Hostname command value obtained from the / proc / sys / kernel / hostname acquired and / etc / sysconfig / network configuration is not directly related HOSTNAME;
- / Proc / sys / kernel / hostname initial value of the kernel parameter boot Linux loader from / etc / sysconfig / network configuration, this value can be modified after startup by the root account.
Third, doubts
Recently encountered a strange phenomenon, the value of / proc / sys / kernel / hostname is changed is the timing, there is no manual operation, the system does not restart, temporarily not resolved, the follow-up to solve the update.
[References]
[1]. https://jaminzhang.github.io/linux/deep-understanding-of-linux-hostname/