在18.04 LTS中重新引导后,主机名将恢复为旧名称


32

这可能与将18.04 LTS作为虚拟机运行(主机是Win 2016 DC)有关,但我不知道如何实现。我尝试过各种已知的更改主机名的方法,但是它始终会还原为在重新启动计算机时构建的主机名。我尝试了以下方法:

  1. hostnamectl set-hostname xxx。
  2. 直接编辑主机名。
  3. 将--static添加到hostnamectl。
  4. 编辑主机文件并添加所需的主机名。
  5. 重新启动之前,在驱动器中搜索对旧主机名的引用。
  6. 我在互联网上发现的其他一些奇怪的建议不值得一提。

我目前没有此服务器的DNS。

例:

XXXX@iwrxmail:~# hostnamectl
Static hostname: iwrxmail
Pretty hostname: Interwrx Ubuntu Mail Server
Transient hostname: ctl
     Icon name: computer-vm
       Chassis: vm
    Machine ID: 01ef0d836d2c4945b51a4fab8e506381
       Boot ID: e6608fe238d843f883cde52af7631a79
Virtualization: microsoft
Operating System: Ubuntu 18.04 LTS
        Kernel: Linux 4.15.0-20-generic
  Architecture: x86-64
XXXX@iwrxmail:~# hostnamectl set-hostname test
XXXX@iwrxmail:~# hostnamectl
Static hostname: test
     Icon name: computer-vm
       Chassis: vm
    Machine ID: 01ef0d836d2c4945b51a4fab8e506381
       Boot ID: e6608fe238d843f883cde52af7631a79
Virtualization: microsoft
Operating System: Ubuntu 18.04 LTS
        Kernel: Linux 4.15.0-20-generic
  Architecture: x86-64
root@iwrxmail:~# cat /etc/hostname
test
root@iwrxmail:~# cat /etc/hosts
127.0.0.1       localhost.localdomain   localhost
::1             localhost6.localdomain6 localhost6
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts 

However on reboot I get this again ....

XXXX@iwrxmail:~# hostnamectl
Static hostname: iwrxmail
     Icon name: computer-vm
       Chassis: vm
    Machine ID: 01ef0d836d2c4945b51a4fab8e506381
       Boot ID: 25a00676b22048eb8d43492c9de4f147
Virtualization: microsoft
Operating System: Ubuntu 18.04 LTS
        Kernel: Linux 4.15.0-20-generic
Architecture: x86-64

我在Beta版本18.04中遇到了这个问题,但是我认为我会等待发行副本。我还认为克隆虚拟机可能是一个问题,但这是一个刚刚发行的发行版从头开始构建的全新版本。

我想念什么?

提前致谢。


Answers:


49

首先编辑/etc/cloud/cloud.cfg并将参数“ preserve_hostname”从“ false”设置为“ true”,然后编辑/ etc / hostname。


4
奇迹般有效。我永远也不会从上面的注释行中得到它:“#这将导致set + update主机名模块无法运行(如果为true)”。非常感谢,网上有很多错误的方法。
安迪·怀德

18

正在重置主机名cloud-init,可通过以下方式禁用该主机名(之后,您可以按常规方式设置主机名,例如使用hostnamectl):

sudo touch /etc/cloud/cloud-init.disabled

或者,您可以使用cloud-init并创建/修改user-data文件(通常在以下位置找到/var/lib/cloud/seed/nocloud-net/user-data),以便将该hostname:条目设置为所需的主机名(preserve_hostname:未设置)。首先,您需要清理现有配置:

sudo cloud-init clean

然后从新的/修改的用户数据文件中重新初始化cloud-init的配置:

sudo cloud-init init

然后重启。有关更多详细信息,请参见cloud-init文档


3
小心运行,cloud-init clean否则cloud-init init它可以重写/ etc / netplan / yaml文件。
Michael D.

3
它还会更新您的SSH主机密钥,这可能有害(客户端需要在重新连接之前接受新的主机密钥)
Per Lundberg

5

对于像我这样的“懒人”,请使用复制粘贴解决方案:)

sudo sed -i '/preserve_hostname: false/c\preserve_hostname: true' /etc/cloud/cloud.cfg && sudo hostnamectl set-hostname ReplaceThisWithTheHostnamePreferred

第一个命令允许操作系统记住新的主机名。

第二部分(在&&之后)仅在第一部分成功完成后才运行,并将主机名设置为所需的值。

问候!大号


4

1-编辑vi /etc/cloud/cloud.cfg和更改

preserve_hostname: false

preserve_hostname: true

保存并退出。

2- vi /etc/hostname在此文件中编辑并替换您的新名称,或者您可以使用bellow命令执行此步骤。

hostnamectl set-hostname NEWNAME

好好享受 :)


0

我遇到了同样的问题,发现删除云程序包后,您可以更改主机名。

apt remove cloud-init cloud-initramfs-copymods cloud-initramfs-dyn-netconf

1
我敢肯定这会起作用,但是我想我更喜欢上面的答案。不过,感谢您的回应
Andy Wylde

如果您使用的是ubuntu-server软件包,建议不要使用此解决方案。为什么?因为上面提到的软件包是它的先决条件。ubuntu-server软件包的描述'描述:Ubuntu Server系统此软件包取决于Ubuntu Server系统中的所有软件包。它还用于帮助确保正确的升级,因此建议不要删除它。
linux64kb '18

0

如果您不满意将旧版本保留在某个地方,则只需在打开文件,然后/var/lib/cloud/seed/nocloud-net/user-data在以下行更改主机名:

hostname: cm-lc-nc

然后运行:

cloud-init clean
cloud-init init

然后它将设置/etc/hostname为新值,并且在重新启动后保持一致。这将删除先前主机名的所有痕迹,并且如果preserve_hostname由于某种原因被重置或忽略,您仍然不会丢失新的主机名。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.