1. 程式人生 > 實用技巧 >Linux修改主機名稱

Linux修改主機名稱

碰到這個問題的時候,是在安裝Zookeeper叢集的時候,碰到如下問題

java.net.UnknownHostException: XXXX Name or service not known
java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
............

這裡猜想到是主機的地址沒有配對,網上給了方案是重新設定linux的主機名。

Step1:檢視當前的主機名

[root@sxl133 Desktop]# hostname
sxl133

這裡的主機名是sxl133,如果沒有設定,則是localhost.localdomain,這是linux預設的主機名。

Step2:更新network檔案

[root@sxl133 Desktop]# vi /etc/sysconfig/network
[root@sxl133 Desktop]# 

將HOSTNAME=localhost.localdomain改成sxl133(我們想要設定的主機名)

退出編輯。

Step3:更新hosts檔案

輸入:vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

改成

127.0.0.1   localhost sxl133 localhost4 localhost4.localdomain4
::1         localhost sxl133 localhost6 localhost6.localdomain6

退出編輯。

Step4:reboot重啟伺服器,同時查詢主機名

1 [root@sxl133 Desktop]#  reboot
2 [root@sxl133 Desktop]#  hostname

參考文件

https://jingyan.baidu.com/article/574c52192a1d8d6c8d9dc1ce.html