hostname 是在 Intranet 等网络环境下用于标识设备的 id。(而在 Internet 环境下这一职能通常由 DNS 负责)
hostname naming standard
hostname 命名规范:
RFC-1123 (which replaces RFC-952)
- contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9', and the minus sign ('-').
- could not start with or end with a minus sign.
- min length 1, max length 255 (POSIX standard)
RegExp:
(需要正则引擎支持后向匹配,javascript 里在 ES6 标准里实现支持)
^[a-zA-Z0-9][-a-zA-Z0-9]{0,254}(?<!-)$
Get / Set hostname
需要系统的 root / administrator 权限
Android
(Android 4.0+)
setprop net.hostname "name"
getprop net.hostname
另外在 "Settings" app 里有 "device name" 设置项,但其并不直接被用作 hostname,很多 Android 设备还会在这个值之前加上 "model name" 组合起来作为 hostname 使用。
Linux
cat /proc/sys/kernel/hostname
hostname # command
cat /etc/hostname
Windows
run "sysdm.cpl" to open "System Properties" window => "Computer Name" tab => "Change" button
hostname Usage
应用场景:
DHCP 协议中客户端设备提供的 "dhcp client id" 通常使用自己的 hostname 值。也就是在路由器管理界面里看到的已连接设备列表里的 "device name" 栏。