之前写过一篇博客“深入理解Linux修改hostname”,里面总结了RHEL 5.7下面如何修改hostname,当然这篇博客的内容其实也适用于CentOS 6,但是自CentOS 7开始,已经跟CentOS 6已经有很大不同了,一直没有总结CentOS 7下修改hostname的相关知识点,今天恰好遇到了这个问题,处理完后遂总结一下。

CentOS 7中的启动跟CentOS 6有所区别,CentOS 6启动过程中,会执行/etc/rc.d/rc.sysinit init执行的第一个脚本 这个脚本主要是初始化工作,如设置系统字体,启动swapping,设置主机名等等。CentOS7和CentOS6启动流程差不多,只不过到init程序时候,改为了systemd启动了(并行启动),也就是说CentOS 7不会去执行/etc/rc.d/rc.sysinit这个文件(当然也没有这个文件了)读取hostname的配置,CentOS 7新增了配置文件/etc/hostname,系统启动的时候会读取/etc/hostname这个配置文件来初始化内核主机名。

另外,我们可以通过配置/etc/hostname修改hostname。也可以通过新增的hostnamectl命令修改。在CentOS 7中,主机名可以分为下面三种类型:

·         静态主机名(static):静态主机名也称为内核主机名,是系统在启动时初始化内核的主机名,默认从/etc/hostname读取配置自动初始化静态主机名

·         瞬态主机名(transient):瞬时主机名是在系统运行时临时分配的主机名,例如,由DHCP等一些系统临时分配的主机名,如果系统存在静态主机名且有效,则不会用到瞬态主机名。

·         灵活主机名(pretty):静态和瞬态主机名都是要符合域名的字符串,而pretty主机名则可以包含其他一些特殊字符。

There are three 3 types of hostnames.

  1. The static hostname is the most important one, and it’s stored in the /etc/hostname file. This hostname is used among machines to identify a particular server.
  1. The pretty hostname got its name because it allows for more characters and punctuation. It’s more user-friendly, but since it uses non-standard characters, it is not permitted for machine code. The pretty hostname is stored in the /etc/machine-info directory.
  1. The transient hostname is one maintained in the Linux kernel. It is dynamic, meaning it will be lost after a reboot. This approach might be useful if you have a minor job requiring a temporary hostname, but you don’t want to risk making a permanent change that might be confusing.

The static (configured) host name is the one configured in /etc/hostname or a similar file. It is chosen by the local user. It is not always in sync with the current host name as returned by the gethostname() system call. If no host name is configured this property will be the empty string. Setting this property to the empty string will remove /etc/hostname. This hostname should be an internet-style hostname, 7bit ASCII, no special chars/spaces, lower case.

The transient (dynamic) host name is the one configured via the kernel's sethostbyname(). It can be different from the static hostname in case DHCP or mDNS have been configured to change the name based on network information. This property is never empty. If no host name is set this will default to "localhost". Setting this property to the empty string will reset the dynamic hostname to the static host name. If no static host name is configured the dynamic host name will be reset to "localhost". This hostname should be an internet-style hostname, 7bit ASCII, no special chars/spaces, lower case.

The pretty host name is a free-form UTF8 host name for presentation to the user. UIs should ensure that the pretty hostname and the static hostname stay in sync. I.e. when the former is "Lennart's Computer" the latter should be "lennarts-computer". If no pretty host name is set this setting will be the empty string. Applications should then find a suitable fallback, such as the dynamic hostname.

如上英文介绍,静态主机名保存在/etc/hostname中,灵活主机名保存在/etc/machine-info,而瞬态主机名一般由内核参数维护,重启后会丢失。

查看主机名(hostname)

我们可以有很多方式查看主机名,但是我们只能使用命令hostnamectl查看静态、瞬态或灵活主机名,分别使用--static,--transient或--pretty参数。

[root@MyDB ~]# hostname

MyDB

[root@MyDB ~]# cat /etc/hostname

MyDB

[root@MyDB ~]# hostnamectl status

   Static hostname: localhost.localdomain

Transient hostname: MyDB

         Icon name: computer-desktop

           Chassis: desktop

        Machine ID: 955dde0d8f7341ebb19a1e247577c410

           Boot ID: 4f2df049135e41c795a655cdf36c1c40

  Operating System: CentOS Linux 7 (Core)

       CPE OS Name: cpe:/o:centos:centos:7

            Kernel: Linux 3.10.0-862.el7.x86_64

      Architecture: x86-64

 

[root@MyDB ~]# hostnamectl 

   Static hostname: MyDB

   Pretty hostname: kerry's db

         Icon name: computer-desktop

           Chassis: desktop

        Machine ID: 955dde0d8f7341ebb19a1e247577c410

           Boot ID: 459eb877eeb34d7e910f4eec8ef4a42f

  Operating System: CentOS Linux 7 (Core)

       CPE OS Name: cpe:/o:centos:centos:7

            Kernel: Linux 3.10.0-862.el7.x86_64

      Architecture: x86-64

[root@MyDB ~]# hostnamectl --static    #查看静态主机名

MyDB

[root@MyDB ~]# hostnamectl --transient #查看瞬时主机名

MyDB

[root@MyDB ~]# hostnamectl --pretty    #查看灵活主机名

kerry's db

设置静态主机名

[root@MyDB ~]# hostnamectl set-hostname kerrydb

[root@MyDB ~]# hostnamectl set-hostname --static kerrydb

设置静态主机名立对新连接的会话立即生效,但是对于当前连接则不生效(例如,SecureCRT新开一个窗口就能看到修改结果)。如下测试所,修改静态主机名会立即修改内核中的kernel.hostname

[root@MyDB ~]# hostnamectl set-hostname MyDB

[root@MyDB ~]# 

[root@MyDB ~]# cat /proc/sys/kernel/hostname

mydb

[root@MyDB ~]# hostnamectl set-hostname kerrydb

[root@MyDB ~]# cat /proc/sys/kernel/hostname

kerrydb

[root@MyDB ~]# hostnamectl set-hostname --static yourdb

[root@MyDB ~]# cat /proc/sys/kernel/hostname

yourdb

[root@MyDB ~]# 

另外,hostnamectl命令是永久修改hostname,这个命令修改静态主机名,不关会设置内核参数kernel.hostname,它还会立即修改配置文件/etc/hostname,有兴趣可以自己测试一下。

[root@MyDB ~]# more /etc/hostname

yourdb

设置瞬态主机名

 

[root@yourdb ~]# hostnamectl set-hostname --transient "KerryDB"

 

注意:如果系统存在静态主机名且有效,则不会用到瞬态主机名。

 

设置灵活主机名

 

[root@yourdb etc]# hostnamectl set-hostname --pretty "kerry's db"

没有设置灵活主机名前,此文件可能不存在(如下所示)

[root@yourdb ~]# cat /etc/machine-info

cat: /etc/machine-info: No such file or directory

设置后,就能查看此文件(如下所示)

[root@yourdb etc]#  cat /etc/machine-info

PRETTY_HOSTNAME="kerry's db"

一些问题测试

问题:如果同时设置了/etc/hosts和/etc/hostname,那么服务器重启时,它会读取哪个文件? 我们设置一下这两个后(如下所示),然后重启服务器

[root@MyDB ~]# more /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

10.20.57.24  MyDB  MyDB.localdomain

[root@MyDB ~]# more /etc/hostname

YourDB

重启过后,你会发现静态主机名为YourDB,也就是说重启时读取/etc/hostname这个配置文件来初始化内核主机名,那么我们将/etc/hostname中的静态主机名置空,然后重启服务器。

[root@YourDB ~]# cat /dev/null > /etc/hostname

[root@YourDB ~]# more /etc/hostname

[root@YourDB ~]# reboot

测试验证发现,静态主机名变为了n/a,但是显示的主机名为MyDB,那么是否读取了/etc/hosts中的配置呢?我们查看日志,发现下面一些信息。

[root@MyDB ~]# hostname

MyDB

[root@MyDB ~]# hostnamectl

   Static hostname: n/a

   Pretty hostname: kerry's db

Transient hostname: MyDB

         Icon name: computer-desktop

           Chassis: desktop

        Machine ID: 955dde0d8f7341ebb19a1e247577c410

           Boot ID: dfdaa6d51f3942b18d2de98dea2b8906

  Operating System: CentOS Linux 7 (Core)

       CPE OS Name: cpe:/o:centos:centos:7

            Kernel: Linux 

我们发现系统启动的时候,在NetworkManager中重新设置hostname的值。具体如下所示:

[root@MyDB ~]#  journalctl -xb | grep hostname

Nov 04 16:03:03 localhost.localdomain systemd[1]: Set hostname to <localhost.localdomain>.

Nov 04 16:03:15 localhost.localdomain dbus[716]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'

-- Subject: Unit systemd-hostnamed.service has begun start-up

-- Unit systemd-hostnamed.service has begun starting up.

Nov 04 16:03:15 localhost.localdomain dbus[716]: [system] Successfully activated service 'org.freedesktop.hostname1'

-- Subject: Unit systemd-hostnamed.service has finished start-up

-- Unit systemd-hostnamed.service has finished starting up.

Nov 04 16:03:15 localhost.localdomain NetworkManager[743]: <info>  [1572854595.8531] hostname: hostname: using hostnamed

Nov 04 16:03:15 localhost.localdomain nm-dispatcher[856]: req:1 'hostname': new request (2 scripts)

Nov 04 16:03:15 localhost.localdomain nm-dispatcher[856]: req:1 'hostname': start running ordered scripts...

Nov 04 16:03:20 localhost.localdomain NetworkManager[743]: <info>  [1572854600.1230] policy: set-hostname: set hostname to 'MyDB' (from address lookup)

Nov 04 16:03:20 MyDB systemd-hostnamed[836]: Changed host name to 'MyDB'

Nov 04 16:03:20 MyDB nm-dispatcher[856]: req:4 'hostname': new request (2 scripts)

Nov 04 16:03:20 MyDB nm-dispatcher[856]: req:4 'hostname': start running ordered scripts...

然后我们从NetworkManager/src/nm-policy.c找到如下代码,简单撸了一下代码,虽然还没有找到直接读取/etc/hosts中hostname的直接证据(C语言已经遗忘的七七八八了,也不想花太多时间深入!),但是实验测试,当/etc/hostname为空时,确实会读取/etc/hosts下面的主机名信息。另外,有点可以确认的是,当/etc/hostname为空时,代码里面首先会将hostname设置为“localhost.localdomain”, 然后,调用_set_hostnam重新设置。

#define FALLBACK_HOSTNAME4 "localhost.localdomain"

 

static void

_set_hostname (NMPolicy *self,

               const char *new_hostname,

               const char *msg)

{

    NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);

    gs_free char *old_hostname = NULL;

    const char *name;

 

    /* The incoming hostname *can* be NULL, which will get translated to

     * 'localhost.localdomain' or such in the hostname policy code, but we

     * keep cur_hostname = NULL in the case because we need to know that

     * there was no valid hostname to start with.

     */

 

    /* Clear lookup addresses if we have a hostname, so that we don't

     * restart the reverse lookup thread later.

     */

    if (new_hostname)

        g_clear_object (&priv->lookup.addr);

 

    /* Update the DNS only if the hostname is actually

     * going to change.

     */

    if (!nm_streq0 (priv->cur_hostname, new_hostname)) {

        g_free (priv->cur_hostname);

        priv->cur_hostname = g_strdup (new_hostname);

 

        /* Notify the DNS manager of the hostname change so that the domain part, if

         * present, can be added to the search list.

         */

        nm_dns_manager_set_hostname (priv->dns_manager, priv->cur_hostname,

                                     all_devices_not_active (self));

    }

 

     /* Finally, set kernel hostname */

    if (!new_hostname)

        name = FALLBACK_HOSTNAME4;

    else if (!new_hostname[0]) {

        g_warn_if_reached ();

        name = FALLBACK_HOSTNAME4;

    } else

        name = new_hostname;

 

    /* Don't set the hostname if it isn't actually changing */

    if (   (old_hostname = _get_hostname (self))

        && (nm_streq (name, old_hostname))) {

        _LOGT (LOGD_DNS, "set-hostname: hostname already set to '%s' (%s)", name, msg);

        return;

    }

 

    /* Keep track of the last set hostname */

    g_free (priv->last_hostname);

    priv->last_hostname = g_strdup (name);

    priv->changing_hostname = TRUE;

 

    _LOGI (LOGD_DNS, "set-hostname: set hostname to '%s' (%s)", name, msg);

 

    /* Ask NMSettings to update the transient hostname using its

     * systemd-hostnamed proxy */

    nm_hostname_manager_set_transient_hostname (priv->hostname_manager,

                                                name,

                                                settings_set_hostname_cb,

                                                g_object_ref (self));

}

参考资料:

https://github.com/NetworkManager/NetworkManager/blob/master/src/nm-policy.c

Centos 7修改hostname浅析的更多相关文章

  1. CentOS 7 修改hostname

    centOS 7 里面修改hostname的方式有所改变,修改/etc/hosts和/etc/sysconfig/network两个文件已经不能生效.使用的新命令是 : hostnamectl set ...

  2. Centos下修改hostname

    之前安装Centos时候,没有怎么注意,将自己的名字设置为hostname,总感觉好别扭,很不习惯,如是就想把hostname改一下,用到hostname命令. 首先用hostname命令查看当前的主 ...

  3. Liunx centos 系统 修改hostname

    1 centos6下修改hostname [root@centos6 ~]$ hostname # 查看当前的hostnmae centos6.magedu.com [root@centos6 ~]$ ...

  4. Centos系统修改hostname

    1.用命令临时修改 hostname oier 这样,服务器的hostname就变成oier了,但是重启之后会变回去 2.编辑配置文件永久修改 vi /etc/sysconfig/network HO ...

  5. centos下修改hostname,ip,netmask,gateway,dns

    http://itlab.idcquan.com/linux/set/917191.html http://jingyan.baidu.com/article/e4d08ffdd417660fd3f6 ...

  6. centos修改hostname以及时间同步

    centos修改hostname 方法一: 执行命令:hostname test 则修改hostname为test 方法二: 永久修改hostname vi /etc/sysconfig/networ ...

  7. centos 修改hostname

    centos修改主机名的正确方法 1 centos6下修改hostname [root@centos6 ~]$ hostname # 查看当前的hostnmae centos6.magedu.com ...

  8. centos实现永久修改hostname

    前言 介绍一下centos的两种修改hostname的方式. 查看hostname [root@slave02 ~]# hostname slave02 临时性修改 [root@slave02 ~]# ...

  9. centos修改hostname

    1.临时修改 hostname localhost 2.永久修改 vim /etc/sysconfig/network 修改hostname的值后保存

随机推荐

  1. 在创建activiti5..22所需的25张表时 ,所用的方法和遇到的问题。

    最近在学习关于activiti流程设计的相关内容,首先第一步就需要了解25张activiti相关的表,具体的每张表的含义 请自行百度. 这里讲一下 用java代码生成所需要的25张表,很简单: pub ...

  2. __pycache__

    最近在使用python写一个串口模块的时候,偶然发现运行脚本之后,在工程文件夹下面出现了这样一个文件夹__pycache__,所以就特意到网上查了一下这个文件夹是怎么回事. 我们先在源文件中添加一些内 ...

  3. passwd、shadow、group文件格式

    [root@bogon ~]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash 登录名:密码占位符:UID:GID:注释:家目录:用户的默认shell ...

  4. MySQL数据库从复制及企业配置实践

    在实际生产中,数据的重要性不言而喻: 如果我们的数据库只有一台服务器,那么很容易产生单点故障的问题,比如这台服务器访问压力过大而没有响应或者奔溃,那么服务就不可用了,再比如这台服务器的硬盘坏了,那么整 ...

  5. hadoop之yarn详解(框架进阶篇)

    前面在hadoop之yarn详解(基础架构篇)这篇文章提到了yarn的重要组件有ResourceManager,NodeManager,ApplicationMaster等,以及yarn调度作业的运行 ...

  6. Java 添加、修改、读取、删除PPT备注

    概述 幻灯片中的备注信息是只提供给幻灯片演讲者观看的特定内容,在演讲者放映幻灯片时,备注信息可给演讲者提供讲解思路,起到辅助讲解的作用.本文将通过Java程序来演示如何操作PPT幻灯片中的备注信息,要 ...

  7. 利用Helm简化Kubernetes应用部署(2)

    目录 定义Charts  使用Helm部署Demo  Helm常用操作命令  定义Charts 回到之前的“charts”目录,我们依次进行解读并进行简单的修改. Chart.yaml 配置示例: a ...

  8. Ubuntu安装scrcpy手机投屏和控制(Ubuntu用QQ微信的另一种方法)

    Scrcpy 安装 snap install scrcpy adb服务安装 sudo apt-get install android-tools-adb adb配置 查看手机的USB识别号 手机通过U ...

  9. MYSQL中HEX、UNHEX函数

    HEX()函数:返回十六进制值的字符串表示形式.注意:并不是十进制转化为十六进制数,而是转化为字符串... UNHEX() 函数: 每对十六进制数字转化为一个字符. 下面是HEX()几个简单的例子: ...

  10. PHP get_parent_class

    <?php class D { } class A extends D { } class B extends A { } function getAncestor($class){ $pare ...