8

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

 2 years ago
source link: https://shockerli.net/post/macos-hostname-scutil/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

主机名/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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK