1. 程式人生 > >Centos 7修改hostname淺析

Centos 7修改hostname淺析

之前寫過一篇部落格“深入理解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"
            
           

相關推薦

Centos 7修改hostname淺析

之前寫過一篇部落格“深入理解Linux修改hostname”,裡面總結了RHEL 5.7下面如何修改hostname,當然這篇部落格的內容其實也適用於CentOS 6,但是自CentOS 7開始,已經跟CentOS 6已經有很大不同了,一直沒有總結CentOS 7下修改hostname的相關知識點,今天恰好遇

CentOS 7 修改網卡名為eth0 eth1 的兩種方法

recover 文件 bios mas centos 7 nal rmi release ddr 修改網卡名稱為eth0、eth11.1 方法11.1.1 修改網卡名稱cd /etc/sysconfig/network-scripts/mv ifcfg-eno1677773

CentOS 7 修改網卡名為eth0

網卡1 修改網卡名稱cd /etc/sysconfig/network-scripts/ mv ifcfg-ens33 ifcfg-eth02 修改網卡配置文件[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=&quo

centos 7修改啟動級別修改小記

init安裝GNOME桌面後想設置默認啟動桌面環境,結果發現centos 7的/etc/inittab文件和centos 6不一樣了,特記錄centos 7設置開機啟動級別方法。 cat /etc/inittab # inittab is no longer used when using systemd.

CentOS 6 和 CentOS 7修改網卡名的方法

基礎知識CentOS 6 和 CentOS 7修改網卡名的方法 一.CentOS6 step 1:vim /etc/udev/rules.d/70-persistent-net.rules 在 NAME=“ ***” 處 改為所要改的網卡名 ![] step2: cd /etc

CentOS 7 修改日誌時間戳格式

times efault 默認 pre systemctl oot ESS hostname for 默認的時間戳格式是Jul 14 13:30:01 localhost systemd: Starting Session 38 of user root.看著不是很方便,現

centos 7 修改系統螢幕解析度

   centos 7 修改系統螢幕解析度,命令方式和圖形方式的修改方法。 命令:xrandr         通過命令 xrandr 修改系統的解析度,輸入xrandr: bash [

centos 7 修改host檔案

centos7與之前的版本都不一樣,修改主機名在/ect/hostname 和/ect/hosts 這兩個檔案控制 首先修改/etc/hostname vi /etc/hostname 開啟之後的內容是: localhost.localdomain 把它修改成你想要的名字就可以,比如: niukou

[LINUX]CentOS 7修改主機名

先檢視當前的主機名,一般是預設的主機名 [root@localhost etc]# hostname localhost.localdomain 然後使用vi命令修改network的配置檔案(這裡省

CentOS 7修改root 密碼

1.在啟動 CentOS 作業系統後,當顯示如下頁面時,根據提示快速按【E】鍵; 2.在這個頁面, 找到linux16這一行,將之前的【ro】,改為【rw init=sysroot/bin/sh】,

Centos 7 修改yum源為國內的yum源

國外地址yum源下載慢,下到一半就斷了,就這個原因就修改它為國內yum源地址 國內也就是ali 與 網易 以centos7為例 ,以 修改為阿里的yum源 1.備份本地yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.rep

CentOS 7 修改時區

Linux 系統(我特指發行版, 沒說核心) 下大部分軟體的風格就是不會仔細去考慮向後 的相容性, 比如你上個版本能用這種程式配置, 沒準到了下一個版本, 該程式已經不見了. 比如 sysvinit 這種東西. 設定時區同樣, 在 CentOS 7 中, 引入了一個叫

centos 7修改開啟檔案數限制

3、加大開啟檔案數的限制(open files) 檢視 ulimit -n ulimit -a vi /etc/security/limits.conf 最後新增 * soft nofile 1024000 * hard nofile 1024000 h

Centos 7 修改時間問題

更改時間遇到報錯處理: [[email protected] ccj]# timedatectl set-time 2018-08-06 Failed to set time: Automatic time synchronization is enabled [[emai

CentOS 7修改主機名【hostnamectl】

網上的教程多是修改/etc/sysconfig/network中的hostname及/etc/hosts檔案,但該方法在CentOS 7中行不通。 還有方法讓使用sysctl kernel.hostname=node1,該命令也只是更改了後文中介紹的tra

CentOS 7 修改root密碼

1、開機,在啟動選單上選擇CentOS Linux (3.10**.**.x86**) 7 (Core) 按下e,進入編輯模式 2、將游標一直移動到 LANG=en_US.UTF-8 後面,空格再追加init=/bin/sh。這裡特別注意,需要寫在UTF-8後,保持在同一行

CentOS-7修改主機名

方法一(修改靜態主機名): vi /etc/hostname 注:由於靜態主機名是系統初始化時從/etc/hostname中讀取的,所以修改其中的內容為自己想要的主機名即可實現對靜態主機名的修改。 之後reboot以生效。 方法二(修改瞬

CentOS 7 修改IP地址和主機名

一、進入網路配置檔案目錄確保在root使用者下進行操作。進入/etc/sysconfig/network-scripts目錄下。二、重啟網路服務使用service network restart命令,重啟網路服務。修改主機名:hostnamectl set-hostname

Centos 7修改系統預設編碼

centos 6修改 /etc/sysconfig/i18n 立即生效 source /etc/sysconfig/i18n由於centos 7 沒有這個檔案:修改 /etc/locale.conf 立

centos 7修改系統時間

1.檢視時間[[email protected] ~]# date Wed May 30 22:24:35 EDT 2018   #和顯示時間有誤差2.同步網路伺服器時間[[email protected] ~]# ntpdate 218.186.3.36