在 Linux 下查看 hostname(主机名) 有几种常用方法,适用于大多数发行版(CentOS、Ubuntu、Debian、Rocky 等):
hostname
直接显示当前主机名。
hostnamectl(推荐,systemd 系统)hostnamectl
或只看名字:
hostnamectl | grep hostname
适用于:
uname -n
cat /etc/hostname
有些系统还会在:
cat /etc/sysconfig/network
(CentOS 6 及以前)
hostname -f
显示 FQDN(完全限定域名),如:
node1.example.com
临时:
hostname newname
永久(systemd):
hostnamectl set-hostname newname
如果你告诉我 Linux 发行版和版本(如 CentOS 7、Ubuntu 22.04),我可以给你更精确的方法。