Skip to content

Multipass SSH 登录

Multipass 自带 multipass shell <实例名>,日常管理通常不需要额外开放密码登录。需要使用 IDE、SFTP 或独立 SSH 客户端时,优先配置普通用户的密钥登录,不建议开启 root 密码登录。

1. 确认 SSH 服务

进入实例:

powershell
multipass shell lpx

检查 OpenSSH Server:

bash
sudo systemctl status ssh

如果服务不存在,再安装并启动:

bash
sudo apt update
sudo apt install -y openssh-server
sudo systemctl enable --now ssh

2. 配置密钥登录

在 Windows 宿主机生成密钥;已有密钥时跳过:

powershell
ssh-keygen -t ed25519

把宿主机 ~/.ssh/id_ed25519.pub 的内容追加到实例默认用户的授权文件中:

bash
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "替换为宿主机公钥" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

获取实例地址并连接:

powershell
multipass list
ssh ubuntu@实例IP

Multipass 镜像的默认用户通常是 ubuntu;如果镜像或 cloud-init 修改过用户,请以实际配置为准。

3. 检查有效配置

OpenSSH 可能同时读取 /etc/ssh/sshd_config/etc/ssh/sshd_config.d/*.conf。不要只凭某一行配置判断,使用下面的命令查看最终生效值:

bash
sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication|pubkeyauthentication'

修改配置后先校验语法,再重载服务:

bash
sudo sshd -t
sudo systemctl reload ssh

连接失败排查

bash
sudo systemctl status ssh
sudo ss -lntp | grep ':22'
sudo journalctl -u ssh -n 100 --no-pager

依次确认实例 IP、22 端口监听、防火墙、公钥权限和用户名。若必须临时开启密码登录,应限定实验网络范围、使用普通用户并在验证结束后关闭;不要设置 PermitRootLogin yes 暴露 root 密码登录。