1

In our company, I'm creating an Ubuntu Autoinstaller (v. 20.04). For some company reasons, its required modify initramfs. (initrd file).

I'm created a script, and added a link to init file in main initrd folder. Its showing correctly, but I did not find a solution, how from this script, set hostname for currently installed system. Hostname after autoinstallation is ubuntu always.

Question is: How to set hostname from custom initrd script?

I'm tried a lot attempts, but neither works.:

netcfg/get_hostname=$hostnam

or

mdadm --assemble --scan --homehost='$hostnam' --auto=yes --auto-update-homehost

or

echo "export HOST=\"$hostnam\"" | tee -a /etc/casper.conf >/dev/null

or

echo $hostnam | tee /etc/hostname >/dev/null

or

hostnamectl set-hostname $hostnam 

Thanks.

1 Answers1

1

Answering the question in the title (I came here searching for this):

The hostname for the currently running kernel (or namespace) can be set via:

echo "machine1" > /proc/sys/kernel/hostname

assuming you want your hostname to be machine1. This also works in the initrd (where tools like hostname are not available).

The original question seems to ask about modifying the installation procedure, though. For debian and older ubuntu versions, preseed was the correct way of setting the hostname. You would need to provide

netcfg/get_hostname=machine1

as a kernel command line argument (from the bootloader), and also set it in preseed.cfg.

Newer Ubuntu versions (20.04 and later) have a completely different installer that uses cloud-init to set things like hostname: https://ubuntu.com/server/docs/install/autoinstall-quickstart

There, you need to serve (via http(s)) a user-data YAML file that can set the hostname:

#cloud-config
autoinstall:
  version: 1
  identity:
    hostname: machine1
# (...)

and you boot the installer kernel with arguments:

autoinstall ds=nocloud-net;s=http://webserver/

where webserver is the machine that hosts the user-data file.