You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建Azure VM时cloud-init未运行的问题排查求助

Troubleshooting Why Your cloud-init Config Isn't Running on Azure VM

Let’s walk through the most common reasons your cloud-init-web-server.txt isn’t executing, and how to diagnose and fix them— I’ve debugged this exact scenario multiple times for Azure VMs.

1. First, Validate Your cloud-init File Format & Content

Cloud-init is picky about syntax and structure— small mistakes here are the #1 culprit:

  • Must start with the right header: If you’re using cloud-config YAML, the file must begin with #cloud-config (no extra spaces before it). For example, a valid web server config looks like this:
    #cloud-config
    packages:
      - apache2
    runcmd:
      - systemctl start apache2
      - systemctl enable apache2
    
  • Check for YAML errors: YAML relies on consistent spacing (use 2 spaces per indent, never tabs). A misplaced tab or missing indent will break the entire config.
  • Avoid Windows-style encoding: If you created the file on Windows, make sure it’s saved as UTF-8 without BOM (Notepad defaults to adding a BOM, which cloud-init can’t parse).

2. Confirm the Custom Data Is Being Passed Correctly

When you run az vm create --custom-data cloud-init-web-server.txt, Azure needs to access that file from your local working directory:

  • Verify the file exists in the directory where you ran the az command (run ls cloud-init-web-server.txt to check). If it’s in another folder, use the full absolute path (e.g., --custom-data /home/you/scripts/cloud-init-web-server.txt).
  • Ensure there are no permission issues preventing the az CLI from reading the file (run cat cloud-init-web-server.txt to confirm you can access it).

3. Check Cloud-Init Logs (The Most Critical Step)

SSH into your VM and inspect the cloud-init logs— they’ll tell you exactly what went wrong:

# Detailed cloud-init startup/execution logs
cat /var/log/cloud-init.log
# Output from your config's commands (e.g., package installs)
cat /var/log/cloud-init-output.log

Look for errors like:

  • "Invalid YAML syntax" (points to a formatting issue in your file)
  • "Failed to fetch user data" (means Azure didn’t pass the custom data to the VM)
  • "Package install failed" (indicates a problem with the package repository or command in your config)

4. Verify the Custom Data Reached the VM

Check if your cloud-init file was correctly delivered to the VM:

cat /var/lib/cloud/instance/user-data.txt

If this file is empty or doesn’t match your cloud-init-web-server.txt content, the issue is with how you’re passing the --custom-data parameter. Double-check the file path, and ensure the --secrets "$vm_secret" part of your command isn’t causing syntax conflicts (e.g., unclosed quotes).

5. Rule Out Compatibility or Environment Issues

  • Check cloud-init version: Ubuntu LTS images come with a compatible cloud-init version, but you can confirm with:
    cloud-init --version
    
    If you’re on an older version (pre-21.0), updating it might resolve bugs, but this is rarely necessary for recent Ubuntu LTS releases.
  • Password auth vs. SSH keys: While using --admin-password shouldn’t break cloud-init, if all other checks pass, try creating the VM with an SSH key instead (using --ssh-key-value), just to rule out any edge cases with password-based authentication.

Start with the logs— they’ll give you the clearest insight into why your config isn’t running. Once you spot the error, fixing it is usually straightforward!

内容的提问来源于stack exchange,提问作者kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:27:40