温馨提示×

ubuntu hostname怎样同步

小樊
49
2026-10-10 01:47:49
栏目: 智能运维

在 Ubuntu 中,“hostname 同步”通常有以下几种常见场景。你可以根据需求选择对应方案:


一、手动修改并立即生效(单机)

sudo hostnamectl set-hostname new-hostname

立即生效,且会写入 /etc/hostname。

⚠️ 还需同步修改 /etc/hosts:

sudo nano /etc/hosts

将:

127.0.1.1  old-hostname

改为:

127.0.1.1  new-hostname

二、与 DHCP 服务器同步 hostname(自动获取)

如果希望 Ubuntu 从 DHCP 获取 hostname:

Ubuntu 18.04+(Netplan)

编辑:

sudo nano /etc/cloud/cloud.cfg

确保:

preserve_hostname: false

或设置 DHCP hostname:

sudo nano /etc/netplan/01-netcfg.yaml

添加:

dhcp4: true
dhcp4-overrides:
  hostname: my-hostname

应用:

sudo netplan apply

三、将 hostname 同步到 DNS(动态更新)

使用 nsupdate(DDNS)

nsupdate <<EOF
server dns.example.com
update delete host.example.com A
update add host.example.com 3600 A 192.168.1.10
send
EOF

四、多台服务器 hostname 集中同步(运维场景)

方案 1:Ansible 批量同步

- hostname:
    name: "{{ inventory_hostname }}"

方案 2:Cloud-Init(云环境)

# /etc/cloud/cloud.cfg
hostname: node-01

五、hostname 与系统时间/日志同步(常见误解)

如果你是指 hostname 出现在日志中但显示不一致:

  • 检查 /etc/hostname
  • 检查 /etc/hosts
  • 重启 rsyslog:
sudo systemctl restart rsyslog

六、容器 / Kubernetes 场景

Docker

docker run --hostname mycontainer ubuntu

Kubernetes

spec:
  hostname: pod-hostname

如果你能具体说一下:

  • 是 单机修改 还是 集群同步?
  • 是 DNS / DHCP / 配置管理 哪一类?

我可以给你更精确的操作步骤。

0 踩