Ubuntu Server安装后FQDN设置异常的正确修复方案咨询
Ubuntu Server安装后FQDN设置异常的正确修复方案咨询
你遇到的这个FQDN异常问题在树莓派版Ubuntu Server 22.04的安装里确实不算少见,我来帮你梳理下规范的修复方案和背后的原因。
先确认你给出的系统环境和问题现象:
系统基础信息
# lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.3 LTS Release: 22.04 Codename: jammy
# hostnamectl Static hostname: apollo Icon name: computer Machine ID: f1d5fae30ffc434680ebcb59c2f5a582 Boot ID: af1da6b8a7384f5fa5b35896935dfbf6 Operating System: Ubuntu 22.04.3 LTS Kernel: Linux 5.15.0-1044-raspi Architecture: arm64
DNS配置情况
# resolvectl status Global Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported resolv.conf mode: stub Link 2 (eth0) Current Scopes: DNS Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported Current DNS Server: 192.168.4.1 DNS Servers: 192.168.4.1 DNS Domain: lan
# cat /etc/resolv.conf nameserver 127.0.0.53 options edns0 trust-ad search lan
核心问题现象
hostname --short返回正确短主机名:apollohostname --fqdn仅返回短名:apollo(不符合预期的apollo.lan)hostname --all-fqdns能返回正确FQDN:apollo.lan(因为该命令会查询DNS服务器的反向解析结果)- 初始
/etc/hosts中127.0.1.1行配置错误:127.0.1.1 apollo apollo,未关联域名
规范修复方案
方法一:用hostnamectl配置(推荐,符合Ubuntu systemd规范)
Ubuntu 22.04依赖systemd管理主机名配置,用官方工具设置最稳妥:
- 设置完整静态FQDN:
sudo hostnamectl set-hostname apollo.lan --static
- 验证配置生效:
hostnamectl status
你会看到Static hostname已更新为apollo.lan。
3. 确认/etc/hosts配置(systemd通常会自动更新,手动检查更放心):
确保127.0.1.1行是:
127.0.1.1 apollo.lan apollo
方法二:手动修改配置文件(传统方式)
如果你偏好手动配置:
- 编辑
/etc/hostname,仅保留短主机名:
sudo nano /etc/hostname
输入apollo后保存退出。
2. 编辑/etc/hosts,关联FQDN和短主机名:
sudo nano /etc/hosts
修改127.0.1.1行为:
127.0.1.1 apollo.lan apollo
- 重启主机名服务或系统生效:
sudo systemctl restart systemd-hostnamed # 或者直接重启系统 sudo reboot
问题原因说明
树莓派Ubuntu镜像的Imager工具在安装时,仅设置了短主机名,没有自动关联从DHCP服务器(192.168.4.1)获取的lan域名,导致/etc/hosts缺少FQDN配置。而hostname --fqdn默认优先读取/etc/hosts的本地配置,所以只返回短名;hostname --all-fqdns会同时查询DNS服务器的反向记录,因此能得到正确结果。
修复后验证
执行以下命令确认结果:
hostname --fqdn # 预期返回:apollo.lan hostname --all-fqdns # 预期返回:apollo.lan
备注:内容来源于stack exchange,提问作者Floren
相关产品推荐
相关产品推荐

