Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-2-修改主机名和hostnamectl工具的使用

联系:QQ(5163721)

标题:Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-2-修改主机名和hostnamectl工具的使用

作者:Lunar©版权所有[文章允许转载,但必须以链接方式注明源地址,否则追究法律责任.]

测试目的:
熟悉Linux7中修改主机名的机制和hostnamectl工具的使用

测试环境:CentOS Linux release 7.2.1511 (Core)

[root@lunar bin]# uname -a
Linux lunar.oracle.com 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@lunar bin]# ll /etc/*releas*
-rw-r--r--. 1 root root  38 Dec  9 17:59 /etc/centos-release
-rw-r--r--. 1 root root  51 Dec  9 17:59 /etc/centos-release-upstream
-rw-r--r--. 1 root root 393 Dec  9 17:59 /etc/os-release
lrwxrwxrwx. 1 root root  14 Jan 16 02:48 /etc/redhat-release -> centos-release
lrwxrwxrwx. 1 root root  14 Jan 16 02:48 /etc/system-release -> centos-release
-rw-r--r--. 1 root root  23 Dec  9 17:59 /etc/system-release-cpe
[root@lunar bin]# cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core) 
[root@lunar bin]# 

主机名的配置,在Linux7以下的版本主机名一般是看:
1,配置文件/etc/sysconfig/network
2,命令hosntame
但在Linux7中(这里是CentOS 7.2)中,很多系统管理工具都被替换了,包括主机名的配置。
.
Linux7中采用新的配置文件/etc/hostname,替代了Linux7以前(Linux2~Linux6)中使用的/etc/sysconfig/network文件
不过,/etc/sysconfig/network文件还在,只是其内容只剩下一行标注为Linux安装管理程序的“# Created by anaconda”:

[root@localhost ~]# ll /etc/sysconfig/network
-rw-r--r--. 1 root root 22 1月  16 03:08 /etc/sysconfig/network
[root@localhost ~]# cat /etc/sysconfig/network
# Created by anaconda
[root@localhost ~]# 

这里看到的Anaconda是RedHat、CentOS、Fedora等Linux的安装管理程序的名字,其大部分模块用Python编写,有少许的载入模块用C编写。
.
从Linux7开始使用/etc/hostname来管理主机名的配置信息:

[root@localhost ~]# ll /etc/hostname
-rw-r--r--. 1 root root 22 1月  16 03:08 /etc/hostname
[root@localhost ~]# cat /etc/hostname
localhost.localdomain
[root@localhost ~]# 

在Linux中,有三种定义的主机名:
静态的(static),“静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名。
瞬态的(transient),“瞬态”主机名是在系统运行时临时分配的主机名,例如,通过DHCP或mDNS服务器分配。
灵活的(pretty)。“灵活”主机名则允许使用自由形式(包括特殊/空白字符)的主机名,以展示给终端用户(如Dan’s Computer)。
静态主机名和瞬态主机名都遵从作为互联网域名同样的字符限制规则。
.
只查看静态、瞬态或灵活主机名,分别使用“–static”,“–transient”或“–pretty”选项。
例如:

[root@localhost ~]# hostnamectl status --static
localhost.localdomain
[root@localhost ~]# hostnamectl status --transient
localhost.localdomain
[root@localhost ~]# hostnamectl status --pretty

但是在Linux7中,有个叫hostnamectl的命令行工具,它允许你查看或修改与主机名相关的配置。

[root@localhost ~]# cat /etc/hostname
localhost.localdomain
[root@localhost ~]# hostnamectl			
   Static hostname: localhost.localdomain	--------缺省只有静态主机名:localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@localhost ~]# 

我们看一下hostnamectl的帮助信息:

[root@localhost ~]# hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...

Query or change system hostname.

  -h --help              Show this help
     --version           Show package version
     --no-ask-password   Do not prompt for password
  -H --host=[USER@]HOST  Operate on remote host
  -M --machine=CONTAINER Operate on local container
     --transient         Only set transient hostname
     --static            Only set static hostname
     --pretty            Only set pretty hostname

Commands:
  status                 Show current hostname settings
  set-hostname NAME      Set system hostname
  set-icon-name NAME     Set icon name for host
  set-chassis NAME       Set chassis type for host
  set-deployment NAME    Set deployment environment for host
  set-location NAME      Set location for host
[root@localhost ~]# 

修改配置文件/etc/hostname,注释掉缺省的第一行,添加我们以前习惯的不带域名的主机名lunar:

[root@localhost ~]# cat /etc/hostname 
#localhost.localdomain
lunar1
[root@localhost ~]# 

[root@localhost ~]#
再次查看:

[root@localhost ~]# hostnamectl status
   Static hostname: #localhost.localdomain	-------Static hostname以前的“localhost.localdomain”变成注释的,也就是/etc/hostname中被注释的一行
Transient hostname: #localhost.localdomain	-------Transient hostname以前没有,这里出现了/etc/hostname中的两行内容
lunar1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@localhost ~]#

修改完配置文件/etc/hostname后,这里看到我们修改后的信息:
Static hostname以前的“localhost.localdomain”变成注释的,也就是/etc/hostname中被注释的一行
Transient hostname以前没有,这里出现了/etc/hostname中的两行内容
.
当我们把/etc/hostname中的内容都注释掉后,再次检查发现:

[root@localhost ~]# cat /etc/hostname
#localhost.localdomain
[root@localhost ~]#
[root@localhost ~]# hostnamectl status
   Static hostname: #localhost.localdomain	-------Static hostname的信息没有变化,仍然是/etc/hostname中被注释的一行
         Icon name: computer-vm			-------而Transient hostname的信息没有了
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@localhost ~]# 

而我们输入Linux7以前常用的命令hostname,发现hostname命令的作用相当于读取/etc/hostname:

[root@lunar1 ~]# hostname
#localhost.localdomain
lunar1
[root@lunar1 ~]# 

这里我们看到hostname的作用类似于cat /etc/hostname
再次查看:

[root@lunar1 ~]# hostnamectl status --static
#localhost.localdomain
[root@lunar1 ~]# hostnamectl status --transient
#localhost.localdomain
lunar1
[root@lunar1 ~]# hostnamectl status --pretty

[root@lunar1 ~]# 

可见,hostname命令还可以使用,但是起作用是读取/etc/hostname的内容和设置瞬态的hostname:

[root@lunar1 ~]# hostname lunar2	-
[root@lunar1 ~]# hostname
lunar2
[root@lunar1 ~]# 

此时并没有改变/etc/hostname的内容

[root@lunar1 ~]# cat /etc/hostname
#localhost.localdomain
lunar1
[root@lunar1 ~]# hostnamectl status
   Static hostname: #localhost.localdomain	---------Static hostname仍然显示为/etc/hostname中的第一行内容
Transient hostname: lunar2			----------Transient hostname已经显示为我们设置的lunar2
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@lunar1 ~]# 

修改/etc/hostname的内容,再次查看:

[root@lunar2 ~]# cat /etc/hostname
lunar1
[root@lunar2 ~]# 
[root@lunar2 ~]# hostnamectl status
   Static hostname: lunar1		---------------Static hostname应该变为/etc/hostname中的设置
         Icon name: computer-vm		
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@lunar2 ~]# 
[root@lunar2 ~]# hostname
lunar1
[root@lunar2 ~]# 
[root@lunar2 ~]# hostnamectl status --static
lunar1
[root@lunar2 ~]# hostnamectl status --transient
lunar1
[root@lunar2 ~]# hostnamectl status --pretty

[root@lunar2 ~]# 

同时修改所有三个主机名:静态、瞬态和灵活主机名:

[root@lunar1 ~]# hostnamectl set-hostname lunar2
[root@lunar1 ~]# hostnamectl status --static
lunar2
[root@lunar1 ~]# hostnamectl status --transient
lunar2
[root@lunar1 ~]# hostnamectl status --pretty

[root@lunar1 ~]# hostname
lunar2
[root@lunar1 ~]# cat /etc/hostname
lunar2
[root@lunar1 ~]# hostnamectl status
   Static hostname: lunar2
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@lunar1 ~]# hostnamectl
   Static hostname: lunar2
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@lunar1 ~]# 
[root@lunar1 ~]# exit
登出

Last login: Sat Jan 16 05:06:26 2016 from 192.168.56.1
[root@lunar2 ~]# 

这是我们看到,修改配置文件影响hostnamectl的结果(需要重新登录才能看到)
而使用hostnamectl set-hostname修改主机名(主要是修改了静态主机名)同样会自动更新/etc/hostname配置文件。
这个在Linux7和以前的Linux2~Linux6的不同之处,后面会看到,网卡等其他的配置信息也都是类似道理。
.
如果只想修改特定的主机名(静态,瞬态或灵活),可以使用“–static”,“–transient”或“–pretty”选项。
例如,修改静态主机名:

[root@localhost media]# hostnamectl --static set-hostname lunar1
[root@localhost media]# hostnamectl status
   Static hostname: lunar1
         Icon name: computer
           Chassis: n/a
        Machine ID: 7d3a3130ee07444c9142069b0dc0ec13
           Boot ID: 017583256da7406d9329f8bb1c9d914b
    Virtualization: kvm
  Operating System: Oracle Linux Server 7.1
       CPE OS Name: cpe:/o:oracle:linux:7:1
            Kernel: Linux 3.8.13-55.1.6.el7uek.x86_64
      Architecture: x86_64
[root@localhost media]# 

注意:
1,不必重启机器以激活永久主机名修改,上面的命令会立即修改内核主机名
2,通常我们用注销并重新登入的方法观察命令行的提示来观察新的静态主机名。
.
现在,我们测试大写主机名:
我们将主机名修改为大写的LUNAR2:

[root@lunar1 ~]# cat /etc/hostname
LUNAR2
[root@lunar1 ~]# hostnamectl
   Static hostname: LUNAR2
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@lunar1 ~]# hostname
LUNAR2
[root@lunar1 ~]# 
[root@lunar1 ~]# exit
登出

Last login: Sat Jan 16 04:51:04 2016 from 192.168.56.1
[root@LUNAR2 ~]# 

我们查看一下相关日志,Linux7中systemd使用journald来管理日志系统,替换了sysVinit中的syslog,可以使用命令journalctl用来读取日志。
通常,应用程序和服务可以通过将相关信息按照需要的格式传递给 systemd journald 服务,然后由来journald来生成项目(在日志中记录相关信息)。
我们查看一下hostnamectl.service的相关日志:

[root@lunar1 ~]# journalctl _SYSTEMD_UNIT=hostnamectl.service
-- Logs begin at 六 2016-01-16 03:15:00 CST, end at 六 2016-01-16 05:40:01 CST. --
[root@lunar1 ~]#

这里看到,该服务只有一个启动的信息,没有其他修改信息。
那么时谁将上述修改信息记录到日志中呢?
这里我们看到主机名修改的信息,实际上由NetworkManager.service服务来管理,并计入了日志:

[root@lunar1 ~]# journalctl _SYSTEMD_UNIT=NetworkManager.service _PID=818
-- Logs begin at 六 2016-01-16 03:15:00 CST, end at 六 2016-01-16 05:40:01 CST. --
1月 16 03:15:37 localhost.localdomain NetworkManager[818]: <info>  NetworkManager (version 1.0.6-27.el7) is starting...
1月 16 03:15:37 localhost.localdomain NetworkManager[818]: <info>  Read config: /etc/NetworkManager/NetworkManager.conf and conf.d: 10-ibft-plugin.conf
1月 16 03:15:37 localhost.localdomain NetworkManager[818]: <info>  VPN: loaded org.freedesktop.NetworkManager.libreswan
1月 16 03:15:37 localhost.localdomain NetworkManager[818]: <info>  Loaded settings plugin ifcfg-rh: (c) 2007 - 2015 Red Hat, Inc.  To report bugs please use the NetworkManager mailing list. (/
。。。。。。。。。。。。。。。。
1月 16 04:07:00 #localhost.localdomain
lunar1 NetworkManager[818]: <info>  Setting system hostname to '#localhost.localdomain
                                                                    lunar1' (from system configuration)
1月 16 04:08:35 #localhost.localdomain NetworkManager[818]: <info>  Setting system hostname to '#localhost.localdomain' (from system configuration)
1月 16 04:09:03 lunar1 NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from address lookup)
1月 16 04:11:58 #localhost.localdomain
lunar1 NetworkManager[818]: <info>  Setting system hostname to '#localhost.localdomain
                                                                    lunar1' (from system configuration)
1月 16 04:33:52 lunar1 NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from system configuration)
1月 16 05:03:50 LUNAR2 NetworkManager[818]: <info>  Setting system hostname to 'LUNAR2' (from system configuration)
1月 16 05:05:57 lunar1 NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from system configuration)

[root@lunar1 ~]#

在/var/log/messages 中有如下记录:

Jan 16 04:07:00 localhost NetworkManager[818]: <info>  Setting system hostname to '#localhost.localdomain#012lunar1' (from system configuration)
Jan 16 04:07:00 localhost dbus-daemon: dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:07:00 localhost dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:07:00 localhost systemd: Starting Network Manager Script Dispatcher Service...
Jan 16 04:07:00 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:07:00 localhost systemd: Started Network Manager Script Dispatcher Service.
Jan 16 04:07:00 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:07:00 localhost nm-dispatcher: Dispatching action 'hostname'
Jan 16 04:08:02 localhost dbus-daemon: dbus[653]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Jan 16 04:08:02 localhost dbus[653]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Jan 16 04:08:02 localhost systemd: Starting Hostname Service...
Jan 16 04:08:02 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.hostname1'
Jan 16 04:08:02 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.hostname1'
Jan 16 04:08:02 localhost systemd: Started Hostname Service.
Jan 16 04:08:35 localhost NetworkManager[818]: <info>  Setting system hostname to '#localhost.localdomain' (from system configuration)
Jan 16 04:08:35 localhost dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:08:35 localhost dbus-daemon: dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:08:35 localhost systemd: Starting Network Manager Script Dispatcher Service...
Jan 16 04:08:35 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:08:35 localhost nm-dispatcher: Dispatching action 'hostname'
Jan 16 04:08:35 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:08:35 localhost systemd: Started Network Manager Script Dispatcher Service.
Jan 16 04:08:36 localhost dbus[653]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Jan 16 04:08:36 localhost dbus-daemon: dbus[653]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Jan 16 04:08:36 localhost systemd: Starting Hostname Service...
Jan 16 04:08:36 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.hostname1'
Jan 16 04:08:36 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.hostname1'
Jan 16 04:08:36 localhost systemd: Started Hostname Service.
Jan 16 04:09:03 localhost NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from address lookup)
Jan 16 04:09:03 localhost dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:09:03 localhost dbus-daemon: dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:09:03 localhost systemd: Starting Network Manager Script Dispatcher Service...
Jan 16 04:09:03 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:09:03 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:09:03 localhost nm-dispatcher: Dispatching action 'hostname'

还记得文档中systemd的特点么:
支持并行化任务
同时采用socket式与D-Bus(总线式激活服务);
按需启动守护进程(daemon);
利用 Linux 的 cgroups 监视进程;
支持快照和系统恢复;
维护挂载点和自动挂载点;
各服务间基于依赖关系进行精密控制。

这里我们看到的dbus就是D-Bus(总线式激活服务),因此这里的系统日志是Linux7特有的,由dbus进程记录:

[root@lunar2 ~]# ps -ef|grep 653
dbus       653     1  0 03:15 ?        00:00:01 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root     28748 28671  0 06:01 pts/1    00:00:00 grep --color=auto 653
[root@lunar2 ~]# 
[root@lunar2 ~]# 
[root@lunar2 ~]# ps -ef|grep dbus
dbus       653     1  0 03:15 ?        00:00:01 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root     12158     1  0 03:20 ?        00:00:00 dbus-launch --sh-syntax --exit-with-session
root     12159     1  0 03:20 ?        00:00:00 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session
root     12323 12319  0 03:20 ?        00:00:00 /bin/dbus-daemon --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address 3
root     28687     1  0 06:00 ?        00:00:00 /usr/sbin/abrt-dbus -t133
root     28750 28671  0 06:01 pts/1    00:00:00 grep --color=auto dbus
[root@lunar2 ~]# 

上面我们通过journal日志,还清晰的看到了修改过程:

Jan 16 04:09:03 localhost NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from address lookup)
Jan 16 04:09:03 localhost dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:09:03 localhost dbus-daemon: dbus[653]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service'
Jan 16 04:09:03 localhost systemd: Starting Network Manager Script Dispatcher Service...
Jan 16 04:09:03 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:09:03 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Jan 16 04:09:03 localhost nm-dispatcher: Dispatching action 'hostname'

也就是,如果NetworkManager.service是开启的,那么实际上修改主机名的工作由NetworkManager完成。
那么如果关闭NetworkManager.service服务,会是什么样子?

[root@lunar1 ~]# systemctl stop NetworkManager
[root@lunar1 ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 六 2016-01-16 05:54:55 CST; 5s ago
 Main PID: 818 (code=exited, status=0/SUCCESS)

1月 16 04:11:58 #localhost.localdomain
lunar1 NetworkManager[818]: <info>  Setting system hostname to '#localhost.localdomain
                                                                    lunar1' (from system configuration)
1月 16 04:33:52 lunar1 NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from system configuration)
1月 16 05:03:50 LUNAR2 NetworkManager[818]: <info>  Setting system hostname to 'LUNAR2' (from system configuration)
1月 16 05:05:57 lunar1 NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from system configuration)
1月 16 05:54:54 lunar1 systemd[1]: Stopping Network Manager...
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  caught SIGTERM, shutting down normally.
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  (virbr0): bridge port virbr0-nic was detached
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  (virbr0-nic): released from master virbr0
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  exiting (success)
1月 16 05:54:55 lunar1 systemd[1]: Stopped Network Manager.
[root@lunar1 ~]# 
[root@lunar1 ~]# 
[root@lunar1 ~]# hostname
lunar1
[root@lunar1 ~]# hostnamectl set-hostname lunar2
[root@lunar1 ~]# hostname
lunar2
[root@lunar1 ~]# hostnamectl
   Static hostname: lunar2
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 11f3b15bbc73453287d186e6aa5dda16
           Boot ID: 09514e0b4f1c458eacea8790d4e3412a
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@lunar1 ~]# cat /etc/hostname
lunar2
[root@lunar1 ~]# exit
登出

Last login: Sat Jan 16 05:11:39 2016 from 192.168.56.1
[root@lunar2 ~]# 

然后我们查看日志:

[root@lunar2 ~]# journalctl _SYSTEMD_UNIT=NetworkManager.service|tail
1月 16 04:11:58 #localhost.localdomain
lunar1 NetworkManager[818]: <info>  Setting system hostname to '#localhost.localdomain
                                                                    lunar1' (from system configuration)
1月 16 04:33:52 lunar1 NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from system configuration)
1月 16 05:03:50 LUNAR2 NetworkManager[818]: <info>  Setting system hostname to 'LUNAR2' (from system configuration)
1月 16 05:05:57 lunar1 NetworkManager[818]: <info>  Setting system hostname to 'lunar1' (from system configuration)
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  caught SIGTERM, shutting down normally.
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  (virbr0): bridge port virbr0-nic was detached
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  (virbr0-nic): released from master virbr0
1月 16 05:54:54 lunar1 NetworkManager[818]: <info>  exiting (success)
[root@lunar2 ~]# 

这里显示NetworkManager服务最后的日志信息是关闭NetworkManager服务的。
查看/var/log/messages,我们发现,关闭NetworkManager.service以后,修改主机名的工作由systemd来完成:

Jan 16 05:54:55 localhost systemd: Stopped Network Manager.
Jan 16 05:55:01 localhost systemd: Created slice user-989.slice.
Jan 16 05:55:01 localhost systemd: Starting user-989.slice.
Jan 16 05:55:01 localhost systemd: Started Session 38 of user pcp.
Jan 16 05:55:01 localhost systemd: Starting Session 38 of user pcp.
Jan 16 05:55:01 localhost systemd: Removed slice user-989.slice.
Jan 16 05:55:01 localhost systemd: Stopping user-989.slice.
Jan 16 05:55:35 localhost dbus[653]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Jan 16 05:55:35 localhost dbus-daemon: dbus[653]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Jan 16 05:55:35 localhost systemd: Starting Hostname Service...
Jan 16 05:55:35 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.hostname1'
Jan 16 05:55:35 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.hostname1'
Jan 16 05:55:35 localhost systemd: Started Hostname Service.
Jan 16 05:55:35 localhost systemd-hostnamed: Changed static host name to 'lunar2'
Jan 16 05:55:35 localhost systemd-hostnamed: Changed host name to 'lunar2'
Jan 16 05:55:54 localhost systemd-logind: Removed session 31.
Jan 16 05:55:56 localhost systemd: Started Session 39 of user root.
Jan 16 05:55:56 localhost systemd-logind: New session 39 of user root.
Jan 16 05:55:56 localhost systemd: Starting Session 39 of user root.
Jan 16 05:55:56 localhost dbus-daemon: dbus[653]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)
Jan 16 05:55:56 localhost dbus[653]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)
Jan 16 05:55:56 localhost dbus-daemon: dbus[653]: [system] Successfully activated service 'org.freedesktop.problems'
Jan 16 05:55:56 localhost dbus[653]: [system] Successfully activated service 'org.freedesktop.problems'
Jan 16 05:58:01 localhost systemd: Created slice user-989.slice.
Jan 16 05:58:01 localhost systemd: Starting user-989.slice.
Jan 16 05:58:01 localhost systemd: Started Session 40 of user pcp.
Jan 16 05:58:01 localhost systemd: Starting Session 40 of user pcp.
Jan 16 05:58:02 localhost systemd: Removed slice user-989.slice.
Jan 16 05:58:02 localhost systemd: Stopping user-989.slice.


===================================================================
总结Linux7中关于主机名管理的改变:
===================================================================
1,主机名配置文件更改为/etc/hostname(Linux7以前是/etc/sysconfig/network)
2,主机名管理命令hostnamectl,Linux7以前的hostname仅用于读取主机名配置文件/etc/hostname或者设置瞬态主机名(transient)
3,修改/etc/hostname和使用hostnamectl更改主机名会相互同步。
例如,修改/etc/hostname的内容,则使用hostnamectl会观察到相应的变化
而使用hostnamectl修改主机名,可以看到/etc/hostname被相应的自动更新
4,查看主机名使用hostnamectl或者hostnamectl status
查看静态,瞬态和灵活主机名分别用:
hostnamectl status –static
hostnamectl status –transient
hostnamectl status –pretty
其中,hostnamectl status –static读取/etc/hostname的第一行内容
hostnamectl status –transient读取/etc/hostname的全部内容
5,修改主机名后,无需重启主机,只需要重新登录即可
简单的说:
Linux7上修改主机名,从以前的hostname命令更改为“hostnamectl set-hostname lunar1”,然后重新登录就可以了
查看当前的主机名,从以前的hostname命令更改为“hostnamectl”或者“hostnamectl status”就可以了
6,/etc/hosts文件需要手工修改,没有自动同步机制(合情合理)
7, Linux11.2 on Linux6和Linux7上的最佳实践,建议hostname使用全名称,即 hostname.domainname
===================================================================


Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列–1-简介
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-2-修改主机名和hostnamectl工具的使用
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列–3-systemd(d.bin和ohasd守护进程)
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列–4-target(图形界面和字符界面)
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列–5-防火墙
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列–6-开机自动启动或者禁用服务
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-7-网络管理之添加网
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-7-网络管理之修改IP地址
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-7-网络管理之修改网络接口名
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-8-在Linux7上安装11.2 RAC和12.1 RAC需要禁用哪些服务
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-9-Linux 7.2上的virbr0设备
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-10-ABRT-系统启动后自动检查和报告错误
Linux7(CentOS,RHEL,OEL)和Oracle RAC环境系列-11-配置VNC和常见问题处理

此条目发表在 Installation and Deinstall, Linux, ORACLE 12C, RAC 分类目录,贴了 , , , , , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注