搞懂 macOS 上的主機名/hostname/ComputerName


主機名/hostname/ComputerName 的含義

主機名、hostname 是一個東西,中英文而已,指的是你本地網絡上的電腦可以通過主機名訪問你的電腦。這個與 Linux 系統是一致的。

ComputerName,這個是 macOS 才有的東西。跟 Windows 上一樣,表示的是電腦名稱,給人看的。

可以從下圖中看出來(系統偏好設置 > 共享):

 

 

對於 macOS 來說,它會按以下的順序來確定主機名,直到獲取到為止:

  • 從以下文件中讀取:/etc/hostconfig
  • 從以下系統配置項讀取:/Library/Preferences/SystemConfiguration/preferences.plist 中的 System ▸ System ▸ HostName
  • 由本機 IP 地址的反向 DNS 查詢獲取
  • 從以下系統配置項讀取:/Library/Preferences/SystemConfiguration/preferences.plist 中的 System ▸ Network ▸ HostNames ▸ LocalHostName
  • 如果以上方法都沒獲取到,就為默認的 localhost

主機名/hostname 修改

查看

hostname

或者

scutil --get LocalHostName

修改

sudo scutil --set LocalHostName XXX

示例

$ hostname
shocker.local
$ scutil --get LocalHostName
shocker
$ sudo scutil --set LocalHostName hello
Password:
$ hostname
hello.local
$ scutil --get LocalHostName
hello

ComputerName 修改

查看

scutil --get ComputerName

 

修改

sudo scutil --set ComputerName XXX

示例

$ scutil --get ComputerName
shocker
$ sudo scutil --set ComputerName MyMacBook
Password:
$ scutil --get ComputerName
MyMacBook

scutil 命令

scutil 是個可動態訪問 macOS 系統信息的交互式工具。

$ 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

 

查看 DNS 配置

$ scutil --dns

DNS configuration

resolver #1
  nameserver[0] : 61.139.2.69
  nameserver[1] : 218.6.200.139
  if_index : 5 (en0)
  flags    : Request A records
  reach    : 0x00000002 (Reachable)

resolver #2
  domain   : 8163912288.members.btmm.icloud.com
  options  : pdns
  timeout  : 5
  flags    : Request A records
  reach    : 0x00000000 (Not Reachable)
  order    : 150000

// more ...

 

查看代理配置

$ scutil --proxy
<dictionary> {
  HTTPEnable : 0
  HTTPSEnable : 0
  ProxyAutoConfigEnable : 0
  SOCKSEnable : 0
}

查看網絡配置

$ scutil --nwi

Network information

IPv4 network interface information
     en0 : flags      : 0x5 (IPv4,DNS)
           address    : 192.168.0.105
           reach      : 0x00000002 (Reachable)

   REACH : flags 0x00000002 (Reachable)

IPv6 network interface information
   No IPv6 states found


   REACH : flags 0x00000000 (Not Reachable)

Network interfaces: en0

 

修改主機名、電腦名稱等

$ scutil --set ComputerName xxx
$ scutil --set HostName xxx
$ scutil --set LocalHostName xxx

 

參考

原文地址: https://shockerli.net/post/ma...


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM