在 Linux 系统中,**hostname(主机名)**和 IP 地址是两个不同但经常一起使用的概念。下面从“是什么、有什么关系、如何对应”几个方面说明。
server01、web.example.com查看 hostname:
hostname
# 或
cat /etc/hostname
192.168.1.10(IPv4)查看 IP:
ip addr
# 或
ifconfig
hostname 是人用的名字,IP 是机器用的地址;它们之间通过“解析”建立联系。
/etc/hosts(本地静态映射)✅ 最常见示例:
192.168.1.10 server01
192.168.1.11 server02
含义:
server01 时,系统会直接使用 192.168.1.10查看解析:
getent hosts server01
web.example.comnslookup web.example.com
# 或
dig web.example.com
192.168.1.10 server01
getent hosts 192.168.1.10
❌ hostname 就是 IP
✅ hostname 只是“名字”,IP 才是“地址”
❌ 改了 hostname 网络就变了
✅ 网络是否变化取决于 IP / 网卡配置
❌ 一台机器只能有一个 hostname
✅ 可以有一个 hostname,但可以对应多个 IP(多网卡)
| 场景 | 说明 |
|---|---|
| 集群环境 | node1 / node2 对应不同 IP |
| SSH 登录 | ssh user@hostname(靠解析) |
| 日志分析 | 用 hostname 区分服务器 |
| 服务绑定 | Nginx / MySQL 可绑定 IP 或 hostname |
hostname 是“谁”,IP 是“在哪”;通过
/etc/hosts或 DNS,让“谁”能找到“在哪”。
如果你愿意,我也可以讲: