6

I am new to ubunto and want to run it in a VM so have installed multipass successfully and have an ubunto VM running in Hyper V. Connecting to the VM I am asked for a login and password is ther a default? or can I set them up from the multipass command line? I can' find any information in the documentation.

I have also tried using shell on a named running instance but connection failed with message "ssh connection failed: 'Failed to resolve hostname primary.mshome.net (No such host is known.)" I then tried creating an instance with just "multipass shell" command. which created a running primary with same result.

Any suggestions welcome

6 Answers 6

7

This might be a bit late but you can login to the instance from multipass without being asked for username and password. From there, you can set the password so that you can login using Hyper V.

First find the name of your instance

multipass list

then login to the instance in multipass

multipass shell <instance name>

Next you can set your password for the default ubuntu user

sudo passwd ubuntu

Now when you try to login using hyper v, just use ubuntu as the username and whatever password you set as the password.

2

For the shell connection problem, I recommend to you check the host.ics file in windows (C:\WINDOWS\System32\drivers\etc\hosts.ics). If you find duplicate entries there, delete the last ones and try connect again. Usually this solve the problem and you see the IP for the instance is showed again using the next command:

multipass list

About the ubuntu password, I think via ssh any password is required but via "direct access" using Hyper-V is requested as you say. Probably not possible login using a password because default ubuntu password is configured as "--disabled".

2
  • this actually was my issue, the hosts.ics was all mess up,. i guess after upgrade W11 to 22H2, thanks!
    – Joyal
    Jan 20 at 14:39
  • Pointing to the hosts.ics helped me. I later discovered that there is a multipass issue about this, and that they have a document for how to debug them. See: 1) github.com/canonical/multipass/issues/1474 2) multipass.run/docs/…
    – rickumali
    Mar 26 at 18:36
2

TLDR

If somehow multipass shell doesn't work, you can connect it via ssh.

ssh ubuntu@vm-ip-address -i /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa

Multipass instance, by default doesn't have usable password.

$ passwd --status ubuntu
ubuntu L 04/03/2021 0 99999 7 -1

The L means Locked password.

This is on purpose

If you somehow lose access to the vm (e.g messing up with the network configuration, it happens to me :D).

You can login using the default ssh private key. You can find the private key at:

/var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa

If you are installing it via snap.

You can also find it using locate.

$ locate id_rsa | grep multipass

and connect to the vm using ssh:

ssh ubuntu@vm-ip-address -i /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa

You may want to change the ownership of the ssh key to your user account. Mine currently set to root.


To find the vm ip address, it depends on the backend. Mine is using libvirt.

virsh net-dhcp-leases default
1

In multipass instance, set a password to ubuntu user.

sudo passwd ubuntu
0

From command line

### to find the instance name
c:\ multipass list
### to change the root password
c:\ multipass shell <instance name> passwd
0

For automation purpose, you can install your own ssh key to a multipass VM. This is useful as password authentication is disabled on recent ubuntu images. So sshing to the VM without ssh key authentication you will get something like:

[email protected]: Permission denied (publickey).

Here is a oneliner:

cat ~/.ssh/yourssh.pub | multipass exec $vm_instance -- bash -c 'cat >> ~/.ssh/authorized_keys'

Explanations:

  1. cat ~/.ssh/yourssh.pub get the content of your favorite ssh key (you may need to create one if you don't have one yet) will be piped through stdin in the next command
  2. multipass exec $vm_instance -- bash -c execute a bash on the vm, the stdout of the previous cat is attached to the stdin of the bash. -c will execute the next command inside the vm
  3. cat >> ~/.ssh/authorized_keys the remote command executed on the vm: it gets the content of stdin (the piped ssh public key) and append >> to the local ~/.ssh/authorized_keys which is the file that stores the keys authorized to connect. As sshd is already installed, the file exists and already has the correct permission and ownership required by sshd.

Test it:

ssh ubuntu@$ip_of_the_vm

And (if your ssh-agent or ssh related config is well oiled) you're in, on your fresh VM!

Want more automation?

OK, let's go! cloud-init trick here, instead of piping the ssh pub key after the VM has been created, you can ask multipass to take care of it at creation time. And it's reusable. I won't detail all extra knowledge here, as yaml syntax and related stuff, but you get the start:

EDIT: the original posted sample was using /home/ubuntu/sshkey.pub to store the key as a local file.

Which has the nasty side effect to change parent folder ownership to root:root which is pretty wrong.

multipass exec vm -- bash -c 'ls -ld $HOME'
drwxr-xr-x 3 root root 4096 Jul 15 12:50 /home/ubuntu

create a YAML file cloud-init.yaml:

#cloud-config
# ^^ this is not a comment above, but a shebang like parser hint, keep it!
write_files:
  # the content here is the verbatim one line full content of your yourssh.pub
  # here a small fresh generated with:
  # ssh-keygen -t ed25519 -C mykey@laptop -f ~/.ssh/yourssh
-   content: |
      ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDsDOXWBOFIEoIctYaXxHAtTtfS3JHAijxHwkOb3IgoW mykey@laptop

    # may you should keep an empty line above ^^
    # the filename sshkey.pub will be created as root:root owner
    path: /tmp/sshkey.pub

# yes, cloud-init has an ssh key management code too :-)
# here a simple append will also do the trick...
runcmd:
  - cat /tmp/sshkey.pub >> /home/ubuntu/.ssh/authorized_keys

Then create your VM:

multipass launch -n vm --cloud-init ./cloud-init.yaml

Test it... and have fun with automation 😁

ssh -i ~/.ssh/mykey [email protected]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .