在 Linux 中查询 hostname(主机名)有多种方式,下面按常用程度给你总结:
hostnamehostname
直接显示当前主机名。
hostnamectl(推荐,CentOS 7+/Ubuntu 16+)hostnamectl
或只查主机名:
hostnamectl --static
hostnamectl --transient
hostnamectl --pretty
✅ 能看到静态、瞬态、友好主机名,信息最全。
unameuname -n
显示网络节点的主机名。
/etc/hostnamecat /etc/hostname
当前系统设置的主机名(重启后生效)。
/etc/hostscat /etc/hosts
查看 IP 与主机名映射关系。
hostname
hostnamectl
HOST=$(hostname)
/etc/hostnamehostname 新名)如果你是 CentOS / Ubuntu / Debian / Alpine 哪一类系统,或者想修改 hostname,也可以告诉我,我给你对应命令。