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

使用QEMU/KVM启动Ubuntu云镜像时SSH服务无法正常启动求助

QEMU/KVM云镜像虚拟机SSH启动失败且cloud-localds凭据无法登录

问题现象

  1. 启动基于Ubuntu云镜像的QEMU/KVM虚拟机后,SSH服务反复启动失败,系统日志如下:
See 'systemctl status ssh.service' for details.
[  OK  ] Started Dispatcher daemon for systemd-networkd.
[  OK  ] Stopped OpenBSD Secure Shell server.
Starting OpenBSD Secure Shell server...
[FAILED] Failed to start OpenBSD Secure Shell server.
See 'systemctl status ssh.service' for details.
[  OK  ] Stopped OpenBSD Secure Shell server.
Starting OpenBSD Secure Shell server...
[FAILED] Failed to start OpenBSD Secure Shell server.
See 'systemctl status ssh.service' for details.
[  OK  ] Started Snap Daemon.
Starting Wait until snapd is fully seeded...
[  OK  ] Stopped OpenBSD Secure Shell server.
Starting OpenBSD Secure Shell server...
[FAILED] Failed to start OpenBSD Secure Shell server.
See 'systemctl status ssh.service' for details.
[  OK  ] Stopped OpenBSD Secure Shell server.
Starting OpenBSD Secure Shell server...
[FAILED] Failed to start OpenBSD Secure Shell server.
See 'systemctl status ssh.service' for details.
[  OK  ] Stopped OpenBSD Secure Shell server.
[FAILED] Failed to start OpenBSD Secure Shell server.
  1. 通过串口TTY登录时,无法使用此前cloud-localds设置的凭据完成登录。

已尝试操作

  • 更换多个不同版本的Ubuntu云镜像
  • 切换至不同的QEMU/KVM宿主机
    上述操作均未解决问题。

排查思路与解决方案

一、先解决串口登录问题(获取系统底层日志)

因为当前无法通过SSH或预设凭据登录,首先绕过cloud-init的凭据限制直接进入系统:

  • 启动虚拟机时添加内核参数 init=/bin/bash,强制进入单用户模式:
    qemu-system-x86_64 ... -append "init=/bin/bash"
    
  • 进入单用户后,挂载根文件系统为可读写:
    mount -o remount,rw /
    
  • 重置root密码:
    passwd root
    
  • 重启虚拟机,使用root密码通过串口TTY登录。

二、排查SSH启动失败的具体原因

登录后执行以下命令获取SSH服务的详细错误日志:

systemctl status ssh.service -l
# 或查看更完整的系统日志
journalctl -u ssh.service --no-pager

常见故障对应解决:

  1. SSH配置文件错误:用sshd -t验证/etc/ssh/sshd_config语法,修复错误后重启服务。
  2. 端口占用:检查22端口是否被其他进程占用:
    ss -tulpn | grep :22
    
    若占用,修改sshd_config中的Port字段,或停止占用进程。
  3. 权限问题:确保SSH相关文件权限正确:
    chmod 700 /var/run/sshd
    chmod 600 /etc/ssh/sshd_config
    chown root:root /etc/ssh/sshd_config
    
  4. cloud-init配置异常:检查cloud-init执行状态:
    cloud-init status --verbose
    cat /var/log/cloud-init.log
    
    若执行失败,尝试重新初始化:
    cloud-init clean
    cloud-init init
    

三、排查cloud-localds凭据失效问题

  1. 检查/etc/cloud/cloud.cfg,确保disable_root: false(如需root登录)、ssh_pwauth: true(如需密码登录)。
  2. 查看/var/log/cloud-init.log中用户创建、凭据设置的相关日志,定位错误信息。
  3. 验证cloud-init镜像文件格式:
    cloud-localds --check my-user-data.img user-data.yaml
    
    确保user-data.yaml格式合规,示例:
    #cloud-config
    password: your-password
    chpasswd: { expire: False }
    ssh_pwauth: True
    

内容的提问来源于stack exchange,提问作者Sergio Giménez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 11:30:59