Centos 7修改hostname淺析

来源:https://www.cnblogs.com/kerrycode/archive/2019/11/06/11803373.html
-Advertisement-
Play Games

之前寫過一篇博客“深入理解Linux修改hostname”,裡面總結了RHEL 5.7下麵如何修改hostname,當然這篇博客的內容其實也適用於CentOS 6,但是自CentOS 7開始,已經跟CentOS 6已經有很大不同了,一直沒有總結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 ~]# 

 

clip_image001

 

另外,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

 

  設置後,就能查看此文件(如下所示)

 

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 框架是用visual studio創建的win32控制台模板,然後自己添加最小化及還原部分代碼。 需要額外包含的頭文件: #include <shellapi.h> #include <WinUser.h> 方式是在托盤區創建圖標,隱藏主視窗。 托盤創建圖標: void ToTray(HWND hW ...
  • 今天在安裝Python模塊matplotlib的時候,一直安裝不成功,老是提示“socket.timeout: The read operation timed out”或者“Read timed out.” 因為國內的網路訪問國外延遲很高,所以安裝模塊很慢就不說了,還一定安裝不上。 1 [root ...
  • Linux系統內核參數優化 在工作中,平常我們使用官方鏡像安裝的Linux系統(非自定製化的)系統內核考慮的是最通用的場景,通常設定都偏向穩定保守,比較典型的代表如紅帽系列的RHEL、CentOS等。而在正式的生成環境使用中,伺服器的CPU、記憶體等硬體配置都比較高,而安裝系統時預設的系統內核參數設定 ...
  • 安裝ntp服務的軟體包 sudo yum install ntp 將ntp服務設置為預設啟動 systemctl enable ntpd 修改啟動參數,增加-g -x參數,允許ntp服務在系統時間誤差較大時也能正常工作 sudo vi /etc/sysconfig/ntpd 啟動ntp服務 sudo ...
  • 介面協議: 請商戶對接使用對應的轉賬介面API和免轉介面API,商戶只能使用菜單對應的API,否則介面會調用失敗。左側菜單未註明的介面免轉錢包和轉賬錢包可以共同使用 所有採集均按照北京時間獲取記錄 所有游戲都支持在登錄和額度轉換時若用戶未創建會自動創建用戶 所有額度轉換介面都會返迴轉換後的餘額 所有... ...
  • 利用百度雲人臉識別API人臉對比介面,將對比分數較高的圖片存入指定的文件夾 ...
  • 最近接到一個需求,要把一個Gitlab上邊的項目全部導入到另外一個Gitlab,借鑒了網上的一個方法,成功實現。 參考鏈接:https://segmentfault.com/a/1190000015952456 1.登錄到需要遷移的Gitlab伺服器,進到/vat/opt/gitlab/git-da ...
  • 本人小白一枚正在老男孩培訓,所以從現在開始把我學到的知識都分享給大家,該隨筆會一直更新 centos7基礎命令與軟體 ps:命令與參數之間必須加上空格,安裝成功時最後一行會有 Complete! 看見這條信息 就證明安裝成功了 安裝命令與軟體之前先下載一下yum源: 下麵這兩條命令分別複製到虛擬機回 ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...