温馨提示×

如何查linux hostname ip

小樊
49
2026-08-31 21:10:03
栏目: 智能运维

在 Linux 中,查看 主机名(hostname)IP 地址 有多种常用方法,下面按场景说明:


一、查看主机名(Hostname)

1. 使用 hostname 命令

hostname

2. 使用 hostnamectl(推荐,CentOS 7+/Ubuntu 16+)

hostnamectl

或只看名字:

hostnamectl --static

3. 查看配置文件

cat /etc/hostname

二、查看 IP 地址

1. 使用 ip 命令(推荐)

ip addr

或简写:

ip a

一般关注:

  • inet 192.168.x.x → IPv4 地址
  • lo 是回环地址,不是对外 IP

2. 查看某个网卡的 IP

ip addr show eth0

3. 使用 ifconfig(老命令,需安装 net-tools)

ifconfig

三、查看公网 IP(对外 IP)

1. 使用 curl

curl ifconfig.me

或:

curl ipinfo.io/ip

2. 使用 wget

wget -qO- ifconfig.me

四、主机名 ↔ IP 对照查看

1. 查看本机 hosts

cat /etc/hosts

2. 通过 hostname 查 IP

getent hosts $(hostname)

五、常见组合示例

echo "Hostname: $(hostname)"
echo "IP: $(hostname -I)"

如果你告诉我:

  • 使用的 Linux 发行版(CentOS / Ubuntu / Alpine 等)
  • 是想查 内网 IP 还是 公网 IP
  • 是否在 容器 / 云服务器

我可以给你更精确的命令。

0