How to use cloud-init to set up a VM or container static IP

vm1 | RUNNING | 10.226.52.19 (enp5s0)
vm4 | RUNNING | 10.226.52.56 (enp5s0)
I want vm1 gateway set 10.226.52.56 dns 10.226.52.56 set up a route table in vm1 can be accessed after reboot The route table set will disappear

In your case, you’re looking at the network config data part (bottom of the page).

1 Like

Here’s what i use, it’s the simplest way I’ve found to deploy preconfigured machines.

incus launch images:fedora/41/cloud myVM --vm \
  -c limits.cpu=2 \
  -c limits.memory=4GB \
  -d root,size=30GiB \
  -p default \
  -c cloud-init.network-config="$(cat <<EOF
version: 2
ethernets:
  enp5s0:
    addresses:
      - 192.168.1.0/24
    gateway4: 192.168.1.10
    nameservers:
      addresses:
        - 192.168.1.1
EOF
)" \
  -c cloud-init.user-data="$(cat <<EOF
#cloud-config
locale: en_US
timezone: UTC
hostname: myVM.example.com
package_update: true
package_upgrade: true
packages:
  - curl
  - vim
  - sudo
  - python3
  - python3-pip
  - ca-certificates
  - git
  - openssh-server
ca_certs:
  trusted:
  - |
    -----BEGIN CERTIFICATE-----
    CUSTOM_ROOT_CERTIFICATE_INSTALLATION
    -----END CERTIFICATE-----
users:
  - default
  - name: john
    gecos: John Smith
    primary_group: john
    groups: users, sudo
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
      - ssh-ed25519 AAAAbbbbCCCCdddd john@example.com
  - name: ansible
    gecos: Ansible User
    primary_group: ansible
    groups: users, sudo
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
      - ssh-ed25519 AAAAbbbbCCCCdddd ansible@example.com
EOF
)"