在 Linux 和 Mac 上更改 hostname

歡迎轉(zhuǎn)載,但請(qǐng)?jiān)陂_(kāi)頭或結(jié)尾注明原文出處【blog.chaosjohn.com】

前言

看過(guò)我文章的朋友都可能發(fā)現(xiàn),從我的終端截圖可以看出,我使用的是 oh-my-zsh,并且對(duì) RPROMPT 進(jìn)行了自定義

oh-my-zsh 自定義 PROMPT

筆者設(shè)置的是: 日期 + 時(shí)間 + 主機(jī)名(hostname)

因?yàn)楣P者有很多電腦 + 虛擬機(jī) + 云主機(jī),所以在終端的命令提示符顯示 hostname 對(duì)于筆者就格外重要,可以方便筆者一目了然的知道當(dāng)前終端連接的是哪臺(tái)機(jī)子。

當(dāng)然,前提是,每臺(tái)機(jī)子都要預(yù)先設(shè)定 hostname

設(shè)定方法

通用

*nix 系統(tǒng)(Linux & macOS & FreeBSD & ...)通用的設(shè)定 hostname 的方法為

# hostname <NEW HOSTNAME>

Linux

如果安裝了 systemd 的話,可以借助 hostnamectl

我們來(lái)看一下它的幫助:

$ hostnamectl -h
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

所以我們?cè)O(shè)定 hostname,可以用:

# hostnamectl set-hostname <NEW HOSTNAME>

macOS

macOS 的話,還可以借助于 scutil

看一下幫助:

$ scutil --help
usage: scutil
    interactive access to the dynamic store.

   or: scutil --prefs [preference-file]
    interactive access to the [raw] stored preferences.

   or: scutil [-W] -r nodename
   or: scutil [-W] -r address
   or: scutil [-W] -r local-address remote-address
    check reachability of node, address, or address pair (-W to "watch").

   or: scutil -w dynamic-store-key [ -t timeout ]
    -w  wait for presense of dynamic store key
    -t  time to wait for key

   or: scutil --get pref
   or: scutil --set pref [newval]
   or: scutil --get filename path key
    pref    display (or set) the specified preference.  Valid preferences
        include:
            ComputerName, LocalHostName, HostName
    newval  New preference value to be set.  If not specified,
        the new value will be read from standard input.

   or: scutil --dns
    show DNS configuration.

   or: scutil --proxy
    show "proxy" configuration.

   or: scutil --nwi
    show network information

   or: scutil --nc
    show VPN network configuration information. Use --nc help for full command list

   or: scutil --allow-new-interfaces [off|on]
    manage new interface creation with screen locked.

   or: scutil --error err#
    display a descriptive message for the given error code

我們可以看到,scutil 可以設(shè)置 ComputerName / LocalHostName / HostName。那三者有什么區(qū)別呢?

  • ComputerName:電腦名稱,如果用過(guò) Time Machine 的話,可以在其備份文件夾內(nèi)看到以 ComputerName.backupbundle 命名的文件,其存的就是對(duì)應(yīng)電腦的歷史備份
    Time Machine 備份
  • LocalHostName:局域網(wǎng)(Bonjour)內(nèi)對(duì)外宣告的主機(jī)名,例如 machine007.local,則局域網(wǎng)內(nèi)其他機(jī)子,可以通過(guò) machine007.local 直接解析出局域網(wǎng)IP,進(jìn)而直接訪問(wèn)到本機(jī)
  • HostName:通常是 LocalHostName 去除尾部的 .local,也就是我們要設(shè)定的 hostname
“系統(tǒng)設(shè)置->共享”

所以我們?cè)O(shè)定 hostname,可以用:

# scutil --set HostName <NEW HOSTNAME>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容