深入理解Linux修改hostname

当我觉得对linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛。技术活,切勿浅尝则止!
实验环境:red hat enterprise linux server release 5.7 (tikanga) ,其它版本linux可能有所不同。请以实际环境为准。
其实我多次修改过hostname,一般只需要修改 /etc/hosts 和 /etc/sysconfig/network 两个文件下相关配置即可。但是,今天我遇到了两个问题:
问题1: 为什么/etc/sysconfig/network配置文件中hostname为localhost.localdomain,但是显示的hostname为po132345806-a,那到底hostname的配置值放在哪里?
1
1: [root@po132345806-a ~]# more /etc/hosts 2: # do not remove the following line, or various programs 3: # that require network functionality will fail. 4: 127.0.0.1 localhost.localdomain localhost 5: ::1 localhost6.localdomain6 localhost6 6: [root@po132345806-a ~]# more /etc/sysconfig/network 7: networking=yes 8: networking_ipv6=yes 9: hostname=localhost.localdomain
有图有真相,免得大家不相信这个现象,当我第一次碰到这种特殊情况时,我也非常纳闷。google了一些资料加上自己的实践才弄明白
问题2: 修改了hostname后,如何使其立即生效而不用重启操作系统。
问题3: 修改hostname有几种方式?
问题4: hostname跟/etc/hosts 下配置有关系吗?
问题5: 如何查看hostname的值,以那个为准?
问题1解答:我一直以为hostname的值配置在/etc/sysconfig/network中,这个文件里面hostname配置为啥,hostname值就是啥。但是为什么出现上面那种情况呢?难道/etc/sysconfig/network
不是hostname的配置文件,难道还另有其它配置文件?于是我当时实验了一下修改了/etc/sysconfig/network文件中hostname为db-server,发现
hostname的值依然没有变化,于是重启了计算机
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
重启过后发现居然hostname变为db-server了,也就是说修改配置文件/etc/sysconfig/network下的hostname生效了。那么也就是说应该是有人修改过 kernel.hostname,请看下面实验
1: [root@db-server ~]# more /etc/sysconfig/network
 2:
 3: networking=yes
 4:
 5: networking_ipv6=yes
 6:
 7: hostname=db-server.localdomain
 8:
 9: [root@db-server ~]# echo test > /proc/sys/kernel/hostname
 10:
 11: [root@db-server ~]# more /etc/proc/sys/kernel/hostname
 12:
 13: /etc/proc/sys/kernel/hostname: no such file or directory
 14:
 15: [root@db-server ~]# more /proc/sys/kernel/hostname
 16:
 17: test
 18:
 19: [root@db-server ~]# /etc/init.d/network restart
 20:
 21: shutting down interface eth0: [ ok ]
 22:
 23: shutting down loopback interface: [ ok ]
 24:
 25: bringing up loopback interface: [ ok ]
 26:
 27: bringing up interface eth0:
 28:
 29: determining ip information for eth0... done.
 30:
 31: [ ok ]
 32:
 33: [root@db-server ~]# hostname
 34:
 35: test
 36:
 37: [root@db-server ~]#
 38:
注意:其实 /etc/init.d/network restart 没有什么用。只是当时实验时以为必须重启网络服务。
在securecrt新建克隆一个会话发现hostanme已经从db-server变为test了,但是/etc/sysconfig/network的值还是db-server.localdomain,并没有变为test。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@ test ~]# more /etc/sysconfig/network
   2: 
   3: networking=yes
   4: 
   5: networking_ipv6=yes
   6: 
   7: hostname=db-server.localdomain
   8: 
   9: [root@ test ~]# hostname
  10: 
  11: test
  12: 
  13: [root@ test ~]# more /etc/hosts
  14: 
  15: # do not remove the following line, or various programs
  16: 
  17: # that require network functionality will fail.
  18: 
  19: 127.0.0.1 localhost.localdomain localhost
  20: 
  21: ::1 localhost6.localdomain6 localhost6
  22: 
  23: [root@ test ~]# more /proc/sys/kernel/hostname
  24: 
  25: test
  26: 
  27: [root@ test ~]#
  28:
但是如果重启系统后hostname会变为db-server,google了一些英文文档资料才知道,hostname是linux系统下的一个内核参数,它保存在/proc/sys/kernel/hostname下,但是它的值是linux启动时从rc.sysinit读取的。
hostname is a kernel parameter which stores hostname of the system. its location is”/proc/sys/kernel/hostname”
the value for this parameter is loaded to kernel by rc.sysinit file during the boot process.
而/etc/rc.d/rc.sysinit中hostname的取值来自与/etc/sysconfig/network下的hostname,代码如下所示,至此,我们可以彻底明白了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
结论:/etc/sysconfig/network 确实是hostname的配置文件,hostname的值跟该配置文件中的hostname有一定的关联关系,但是没有必然关系,hostname的值来自内核参数/proc/sys/kernel/hostname,如果我通过命令sysctl kernel.hostname=test修改了内核参数,那么hostname就变为了test了。
问题2: 修改了hostname后,如何使其立即生效而不用重启操作系统。
方法1:修改了/etc/sysconfig/network下的hostname后,然后使用echo servername > /proc/sys/kernel/hostname。
[root@db-server ~]# echo test >/proc/sys/kernel/hostname
注意当前会话还是不会变化,但是后续新建会话则会生效。
方法2:修改了/etc/sysconfig/network下的hostname后,然后使用sysctl kernel.hostname命令使其立即生效
[root@db-server ~]# sysctl kernel.hostname=test2
kernel.hostname = test2
注意当前会话还是不会变化,但是后续新建会话会生效。
方法3:修改了/etc/sysconfig/network下的hostname后,然后使用hostname命令使其生效
[root@ test ~]# hostname db-server
注意当前会话还是不会变化,但是后续新建会话会生效。
其实呢,这几种方式只是结合永久性修改和临时性修改hostname,使其不必重启linux服务器,哈哈,不知道你明白没。
问题3: 修改hostname有几种方式?
1: hostname db-server –运行后立即生效(新会话生效),但是在系统重启后会丢失所做的修改
2: echo db-server > /proc/sys/kernel/hostname –运行后立即生效(新会话生效),但是在系统重启后会丢失所做的修改
3: sysctl kernel.hostname=db-server –运行后立即生效(新会话生效),但是在系统重启后会丢失所做的修改
4: 修改/etc/sysconfig/network下的hostname变量 –需要重启生效,永久性修改。
问题4: hostname跟/etc/hosts 下配置有关系吗?
如果从我上面的实验来看,其实hostname跟/etc/hosts下的配置是没有关系的。hostname的修改、变更完全不依赖hosts文件。 其实hosts文件的作用相当如dns,提供ip地址到hostname的对应。早期的互联网计算机数量少,单机hosts文件里足够存放所有联网计算机。不过随着互联网的发展,这就远远不够了。于是就出现了分布式的dns系统。由dns服务器来提供类似的ip地址到域名的对应。具体可以man hosts查看相关信息。
linux系统在向dns服务器发出域名解析请求之前会查询/etc/hosts文件,如果里面有相应的记录,就会使用hosts里面的记录。/etc/hosts文件通常里面包含这一条记录
127.0.0.1 localhost.localdomain localhost
hosts文件格式是一行一条记录,分别是ip地址 、hostname、 aliases,三者用空白字符分隔,aliases可选。
127.0.0.1到localhost这一条建议不要修改,因为很多应用程序会用到这个,比如sendmail,修改之后这些程序可能就无法正常运行。
但是呢,其实hostname也不是说跟/etc/hosts一点关系都没有。在/etc/rc.d/rc.sysinit中,有如下逻辑判断,当hostname为localhost后localhost.localdomain时,将会使用接口ip地址对应的hostname来重新设置系统的hostname。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# in theory there should be 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 done 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,$2) }')
if [ -n $ipaddr ]; then
eval $(ipcalc -h $ipaddr 2>/dev/null)
hostname ${hostname}
fi
fi
我们来实验一下吧,修改hosts、network文件,修改后的值如下所示:
1
2
3
4
5
6
7
8
9
10
[root@ localhost~]# more /etc/hosts
# do not remove the following line, or various programs
# that require network functionality will 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
networking=yes
networking_ipv6=yes
hostname=localhost.localdomain
重启系统后,我们再截图看看情况:
所以这也是有时候人们以为hostname的值跟hosts文件有关系的缘故。
问题5: 如何查看hostname的值,以那个为准?
1
2
3
4
5
6
7
8
[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
以那个为准呢,如果你理解了前面4个问题,那么理解这个问题就很简单了。

沁恒股份:LCD显示驱动芯片——CH462
预估到2019年,全球半导体营收将挑战5,000亿美元大关
我们可以从日本LED照明植物工厂学到什么
智慧医疗是怎样促进智慧城市的发展
如何在PCB设计中实现复杂的焊盘形状?
深入理解Linux修改hostname
号称小“低配版昂科威”?新款雪佛兰探界者中型大SUV即将上市
选择AlmaLinux作为CentOS替代方案的一些原因
如何使用Arduino创建停车门禁控制系统?
8051单片机供水系统水位控制的硬件电路设计
中国推出仿人机器人 各种细节非常逼真
开关模式电源基础知识
郑州登陆全景智慧城市APP VR技术带给你如同身临其境的体验
电磁流量计参数性能介绍
未来网络的关键技术
小米1s和小米青春版的区别_小米1s和小米青春版对比
AWE2023全电智慧厨房受追捧 国爱等离子电火灶开创“电明火”时代
电池保护应用防死锁电流的保护器件
iPhone12翻车不断?许多买了iPhone12的人大呼后悔
Silicon Labs其蓝牙Mesh技术被小米用于发布的智能家居产品中