background

under normal conditions Linux The host name is set at installation time ,root Password and other related parameters , However, you may need to change the host name after installation , This article describes how to modify the host name , Including temporary modification and permanent modification .

View host name

Protoecological Linux Generally, it comes with two commands :hostname,hostnamectl For viewing ( It can also be modified ) host name , as follows :
[[email protected] ~]<20180919 11:18:05># hostname zavier2 [[email protected]
~]<20180919 11:18:08># hostnamectl Static hostname: zavier2 Icon name:
computer-vm Chassis: vm Machine ID: e3db3fd526054e28be654ab8fd75d995 Boot ID:
1ad233c0110e4035b14e13729677b7e0 Virtualization: vmware Operating System: Red
Hat Enterprise Linux Server 7.2 (Maipo) CPE OS Name:
cpe:/o:redhat:enterprise_linux:7.2:GA:server Kernel: Linux
3.10.0-327.36.3.el7.x86_64 Architecture: x86-64
Host name classification

Based on the above command , We can further consider whether we can modify the host name ? Is it used hostname,hostnamectl These two commands can change the host name . actually ,Linux Host names are divided into
Static host name (static hostname), Temporary host name (transient hostname) as well as pretty host name
( Understood as : Elegant host name ), The differences are described in detail below :( Excerpt from man hostnamectl)

* The high-level "pretty" hostname which might include all kinds of special
characters (e.g. "Lennart's Laptop");--pretty Host names have almost no restrictions on characters , It can be set to any host name you like ;
* The static hostname which is used to initialize the kernel hostname at boot
(e.g. "lennarts-laptop");-- The static host name is used to set kernel parameters at system startup ; Stored in /etc/hostname in .
* The transient hostname which is a default received from network
configuration. If a static hostname is set, and is valid (something other than
localhost), then the transient hostname is not
used.— The temporary host name is obtained from the network configuration by default , If the static host name is in effect , The temporary host name is useless .
Modify temporary host name

*
By viewing hostname Of man manual , We can see that hostname It has its own options to modify the host name .
hostname [-b|--boot] [-F|--file filename] [hostname]
therefore , We try with this command ,
[[email protected] ~]<20180919 10:22:05># echo $HOSTNAME omp-xygkt01kf
[[email protected] ~]<20180919 10:22:09># hostname testname
[[email protected] ~]<20180919 10:22:21># cat /proc/sys/kernel/hostname
testname [[email protected] ~]<20180919 10:22:25># hostname testname
[[email protected] ~]<20180919 10:22:28># hostnamectl Static hostname:
omp-xygkt01kf Transient hostname: testname Icon name: computer-vm Chassis: vm
Machine ID: e3db3fd526054e28be654ab8fd75d995 Boot ID:
811ad648437a4884bfef8d36824d2f55 Virtualization: vmware Operating System: Red
Hat Enterprise Linux Server 7.2 (Maipo) CPE OS Name:
cpe:/o:redhat:enterprise_linux:7.2:GA:server Kernel: Linux
3.10.0-327.36.3.el7.x86_64 Architecture: x86-64 [[email protected] ~]<20180919
10:23:02>#
explain :

         From above hostnamectl The display information of ,hostname Only changed Transient hostname( Temporary host name ). The principle is temporary modification
/proc/sys/kernel/hostname file ( It can be used cat
/proc/sys/kernel/hostname see ), Those who understand the kernel may know , The document is Linux Virtual files generated at system startup (proc file system ), After system restart hostname It will be static again hostname cover .

Modify static host name

The previous section describes how to modify the temporary host name and how it works , But after the modification, the system will restart and return to the original host name . If you want to change the host name, it will take effect permanently , You need to modify the static host name configuration file :/etc/hostname
, As shown in the figure below : Originally, my host name was xygzavier01kf, After modification, it will be changed to zavier2





principle :/etc/hostname Is the configuration file for the host name ,Linux Read the contents of this file when the system starts , Set the static host name and related kernel parameters .

/etc/hosts,/etc/sysconfig/network The role of

There are some articles <https://www.cnblogs.com/kerrycode/p/3595724.html>
It is mentioned that modifying the host name is achieved by modifying the above two files , Test it today , In fact that was not the case. .

/etc/hosts Is the configuration host name static query table , It is mainly used for address mapping when accessing domain name through network access . Its configuration includes three parts :

IP address      host name     Host name alias



/etc/sysconfig/network For the , It's a name for the computer , Is an identification of the computer .

summary

Modification of static host name , Not all of them Linux It's available in all distributions /etc/hostname file , My system (redhat 7.2) Is configured through this file
, When readers set their own host name , Be sure to think about all types of host names , Various orders (hostname,systemctl etc. ), Attempts at various kernel parameters .
Until you get a satisfactory answer . My system information is as follows :
[[email protected] ~]<20180919 11:29:59># uname -a Linux zavier2
3.10.0-327.36.3.el7.x86_64 #1 SMP Thu Oct 20 04:56:07 EDT 2016 x86_64 x86_64
x86_64 GNU/Linux