2

I am trying to configure this network its working but when I login to os its not. Second How can I configure random hostname?

Thank :)

#cloud-config
autoinstall:
  version: 1
  apt:
    disable_components: []
    geoip: true
    preserve_sources_list: false
    primary:
    - arches:
      - amd64
      - i386
      uri: http://archive.ubuntu.com/ubuntu
    - arches:
      - default
      uri: http://ports.ubuntu.com/ubuntu-ports
  identity:
    hostname: ${hostname}
    password: $6$tN8UBrsU5N4Tcqcz$ewAe9SRZlS2q6yxYtvyJ0u.Lu4l6pgSf5uiHpMFKjlOVTjBfuBb4QIQekfaToA1DtKAUCjoiiAEyc5VYj26uS1
    realname: scadmin
    username: scadmin
  kernel:
    package: linux-generic
  keyboard:
    layout: us
    toggle: null
    variant: ''
  locale: en_US.UTF-8
  network:
    version: 2
    renderer: networkd
    ethernets:
      ens160:
        dhcp4: true
        dhcp-identifier: mac
  ssh:
    allow-pw: true
    authorized-keys: []
    install-server: true
  storage:
    config:
    - ptable: gpt
      path: /dev/sda
      wipe: superblock-recursive
      preserve: false
      name: ''
      grub_device: true
      type: disk
      id: disk-sda
    - device: disk-sda
      size: 1048576
      flag: bios_grub
      number: 1
      preserve: false
      grub_device: false
      type: partition
      id: partition-0
    - device: disk-sda
      size: 30064771072
      wipe: superblock
      flag: ''
      number: 2
      preserve: false
      grub_device: false
      type: partition
      id: partition-1
    - name: vg0
      devices:
      - partition-1
      preserve: false
      type: lvm_volgroup
      id: lvm_volgroup-0
    - device: disk-sda
      size: 2144337920
      wipe: superblock
      flag: ''
      number: 3
      preserve: false
      grub_device: false
      type: partition
      id: partition-3
    - fstype: ext4
      volume: partition-3
      preserve: false
      type: format
      id: format-0
    - name: lv-swap
      volgroup: lvm_volgroup-0
      size: 4294967296B
      wipe: superblock
      preserve: false
      type: lvm_partition
      id: lvm_partition-0
    - name: lv-root
      volgroup: lvm_volgroup-0
      size: 25765609472B
      wipe: superblock
      preserve: false
      type: lvm_partition
      id: lvm_partition-1
    - fstype: ext4
      volume: lvm_partition-1
      preserve: false
      type: format
      id: format-3
    - path: /
      device: format-3
      type: mount
      id: mount-3
    - fstype: swap
      volume: lvm_partition-0
      preserve: false
      type: format
      id: format-4
    - path: ''
      device: format-4
      type: mount
      id: mount-4
    - path: /boot
      device: format-0
      type: mount
      id: mount-0
  updates: security
  packages:
    - open-vm-tools
  late-commands:
    - HOSTNAMEC="$(date +%N | md5sum | cut -f 1 -d " " | head -c 6 )" hostnamectl set-hostname "$HOSTNAMEC"
HELLBOY
  • 77

2 Answers2

2

Just an update:

I was able to make this work for Ubuntu 24.04 (Noble) recently, using Andrew's third option using user-data, and skipping the "identity" portion which doesn't allow for keys. In my case, I only had one user I wanted, "ansible," which needed the keys pre-added for the next stage of setup. I needed to make the hostname a random, but unique server name, so I just made it hh:mm. This also adds a hosts file, which some installs [later on] insisted on.

#cloud-config
autoinstall:
  version: 1
  [...]

late-commands: - wget -O /target/setupci.sh http://192.168.4.10/setupci.bash - curtin in-target -- bash /setupci.sh - | bash <<'EOF' NEWHOST="server-$(date +%I%M)" echo "${NEWHOST}" > /target/etc/hostname echo -e "127.0.0.1\tlocalhost\n127.0.1.1\t${NEWHOST}" > /target/etc/hosts EOF true

user-data: users: - default - name: ansible passwd: <salted hash> shell: /bin/bash lock_passwd: False groups: users, admin ssh_authorized_keys: - ssh-rsa <blah blah blah>

1

I am trying to configure this network its working but when I login to os its not.

That is not enough information.

How can I configure random hostname?

This could be done a bunch of ways. These are some partial user-data configs demonstrating a few solutions.

Here is a early-commands solution that will update the configuration used by the installer itself. The benefit is that the host will be installed with the random name and it will not need to be changed later by another process.

#cloud-config
autoinstall:
  identity:
    hostname: REPLACEHOSTNAME
    password: REDACTED
    realname: myuser
    username: myuser
  early-commands:
    - sed -i -e "s/REPLACEHOSTNAME/$(openssl rand -hex 3)/" /autoinstall.yaml

Here is a cloud-init solution that configures cloud-init to set the hostname. The benefit is that cloud-init can also do things like set the fqdn and update files like /etc/hosts.

#cloud-config
autoinstall:
  identity:
    hostname: my-hostname
    password: REDACTED
    realname: myuser
    username: myuser
  late-commands:
    - |
      cat <<EOF > /target/etc/cloud/cloud.cfg.d/80_my.cfg
      hostname: $(openssl rand -hex 3)
      manage_etc_hosts: true
      preserve_hostname: false
      EOF

Here is a simple solution of changing /etc/hostname on the installed system.

#cloud-config
autoinstall:
  identity:
    hostname: my-hostname
    password: REDACTED
    realname: myuser
    username: myuser
  late-commands:
    - echo $(openssl rand -hex 3) > /target/etc/hostname

notes

I tested using Ubuntu 22.04 (subiquity 22.04.2)