Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to find SSH keys for multipass? #913

Closed
garethr opened this issue Jul 12, 2019 · 23 comments
Closed

Where to find SSH keys for multipass? #913

garethr opened this issue Jul 12, 2019 · 23 comments
Labels

Comments

@garethr
Copy link

garethr commented Jul 12, 2019

I'm able to access a multipass VM using the built-in shell command, but would like to also be able to access directly using SSH, mainly to use multipass with automation tools which connect via SSH. Where can I find the SSH key to connect to the VM?

@ricab ricab added the question label Jul 12, 2019
@ricab
Copy link
Collaborator

ricab commented Jul 12, 2019

Hi @garethr, if you installed via snap, it should be in /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa.

@garethr
Copy link
Author

garethr commented Jul 12, 2019

@ricab apologies, this was on macOS and installed via Homebrew.

@gerboland
Copy link
Contributor

Keys are located in /var/root/Library/Application Support/multipassd/ssh-keys on MacOS

@garethr
Copy link
Author

garethr commented Jul 13, 2019

Mmm, interesting. I don't find that directory, but I have found: ~/Library/Application Support/multipass/client-certificate

This contains multipass_cert.pem and multipass_cert_key.pem.

However, trying to use the key doesn't appear to work:

$ multipass ls
Name                    State             IPv4             Release
aroused-grebe           RUNNING           192.168.64.3     Ubuntu 18.04 LT
$ ssh [email protected] -i ~/Library/Application\ Support/multipass/client-certificate/multipass_cert_key.pem
[email protected]: Permission denied (publickey).

@Saviq
Copy link
Collaborator

Saviq commented Jul 15, 2019

@garethr instead of using Multipass's keys, it's better to bootstrap your instances with cloud-init (see their docs) with whatever users/keys you need.

Also see our docs for a suggestion on how to use launch with --cloud-init. We plan to have a better write-up on this, soon.

I'll close this issue for now.

@aemadrid
Copy link

I've been trying to use cloud init files but you still need to pass an ssh_authorized_keys for multipass so you can run multipass info node_name. Otherwise it cannot communicate with the machines it created. Right now I have it hardcoded in my cloud-init file but it would be better to dynamically create the cloud init file with the right multipass cert.

@felipecruz91
Copy link

I am having the same issue - There is no way to access a Multipass VM directly using SSH?

@setrar
Copy link

setrar commented Feb 15, 2020

@gerboland is right, keys are located under /var/root/Library/Application Support/multipassd/ssh-keys

I was able to access the VM using the private key.

% sudo ssh -i /var/root/Library/Application\ Support/multipassd/ssh-keys/id_rsa [email protected]
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-76-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Feb 15 11:05:08 EST 2020

  System load:  0.0               Processes:             102
  Usage of /:   24.1% of 4.67GB   Users logged in:       0
  Memory usage: 12%               IP address for enp0s2: 192.168.64.7
  Swap usage:   0%


28 packages can be updated.
11 updates are security updates.


Last login: Sat Feb 15 11:04:53 2020 from 192.168.64.1
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

multipass@holy-halibut:~$ 

That allowed me to use multipass VMs with docker-machine using the generic driver

% sudo docker-machine create --driver generic \
   --generic-ip-address=192.168.64.7 \
   --generic-ssh-user=multipass \
   --generic-ssh-key /var/root/Library/Application\ Support/multipassd/ssh-keys/id_rsa \
   MY-VM

Running pre-create checks...
Creating machine...
(MY-VM) Importing SSH key...
(MY-VM) Couldn't copy SSH public key : unable to copy ssh key: open /var/root/Library/Application Support/multipassd/ssh-keys/id_rsa.pub: no such file or directory
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env MY-VM

The only issue is to manage root rights for the private key (i.e. sudo)

@Hailong
Copy link

Hailong commented Mar 1, 2020

Actually instead of finding out the public key that recognized by multipass instance, we could simply append the public key of local user (e.g. ~/.ssh/id_rsa.pub) to the file ~/.ssh/authorized_keys on multipass instance. They we can login to the instance with ssh command: ssh [email protected] (use the multipass info to find the right IP address of the instance).

@tekumara
Copy link

The ubuntu user works for me using the keys in /var/root which require sudo, eg:

sudo ssh -i /var/root/Library/Application\ Support/multipassd/ssh-keys/id_rsa [email protected]

@oleg-andreyev
Copy link

JFYI location of id_rsa on Windows: ./System32/config/systemprofile/AppData/Roaming/multipassd/ssh-keys/id_rsa

@novicevative
Copy link

novicevative commented Feb 5, 2021

Hi
May be this method will be helpful to someone searching for id_rsa or other files in future. Although there are many methods in Linux to achieve same end result.
Hence posting the method I could think of instantly.
I was running ubuntu 18.4 then decided to run multipass with 20.4 LTS
couldn't login using ssh bcz of key.
I ran command
cd /
tree >tree-slash.txt
once completed
searched the file for id_rsa and traced the path back in reverse order.
So the full path for multipass's id_rsa is
/var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa."
to find the ip address of multipass ubuntu instance use command
multipass list

then used
ssh [email protected] -i /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa
and login was successful.

Later found a comment above suggesting
"if you installed via snap, it should be in /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa."

Thanks

@pcgeek86
Copy link
Contributor

pcgeek86 commented Jun 16, 2021

On the Windows 10 platform, using PowerShell:

  1. Install scoop package manager: iwr -useb get.scoop.sh | iex
  2. Use the scoop to install the sudo utility scoop install sudo
  3. Retrieve the private key sudo Get-Content -Path "$env:windir\System32\config/systemprofile/AppData/Roaming/multipassd/ssh-keys/id_rsa"

To SSH directly into a Multipass virtual machine:

  1. Find the VM's IPv4 addresses multipass info animating-seahorse
  2. If multiple IP addresses, ping each one to find accessible interface. My VM had 3 IPv4 addresses, but only one of them was routable from my host system.
  3. Use this command to SSH directly into the correct IP address: sudo ssh -i "$env:windir\System32\config/systemprofile/AppData/Roaming/multipassd/ssh-keys/id_rsa" [email protected]

Here's a screenshot of Microsoft Terminal showing it in action:

image

@pcgeek86
Copy link
Contributor

I created a PowerShell module to simplify this for Windows 10 users. You will need to run PowerShell "As Administrator" in order to use this, since the SSH key is in a protected area of the Windows filesystem.

Find Module on PowerShell Gallery

Find-Module -Name Multipass

Install Module On Your System

Install-Module -Name Multipass -Scope CurrentUser -Force

Use Command to Establish SSH Session

Connect-MultipassVM -VMName animating-seahorse

NOTE: The -VMName parameter can be tab-completed in PowerShell.

@shahidcodes
Copy link

For future readers -
You can create a cloud-init.yaml and paste you host ssh public key.
i.e.

cloud-init.yaml

ssh_authorized_keys:
  - <your_ssh_key>

and create a vm using this config

multipass launch --cloud-init cloud-init.yaml

Once launched, you can get the ip using multipass list and ssh ssh ubuntu@ip

@yyin-dev
Copy link

yyin-dev commented Jan 7, 2022

The ubuntu user works for me using the keys in /var/root which require sudo, eg:

sudo ssh -i /var/root/Library/Application\ Support/multipassd/ssh-keys/id_rsa [email protected]

For me, I must have sudo for it to work on Mac M1.

@chee-chyuan
Copy link

chee-chyuan commented Jan 24, 2022

The ubuntu user works for me using the keys in /var/root which require sudo, eg:

sudo ssh -i /var/root/Library/Application\ Support/multipassd/ssh-keys/id_rsa [email protected]

For me, I must have sudo for it to work on Mac M1.

this was helpful. thanks!

adding this. in case we want to copy id_rsa to other files, ensure that it has wheel sudo chown -R $USER:wheel id_rsa

@iancleary
Copy link

JFYI location of id_rsa on Windows: ./System32/config/systemprofile/AppData/Roaming/multipassd/ssh-keys/id_rsa

@oleg-andreyev FYI: This location appears to have changed with 1.10.0:

  • C:\Windows\ProgramData\Multipass\data\ssh-keys\id_rsa or
  • /c/Windows/ProgramData/Multipass/data/ssh-keys

@sed-i
Copy link

sed-i commented Nov 16, 2022

fwiw, sshuttle could be used like so:

sudo sshuttle -r ubuntu@$(multipass list --format yaml | yq '.charm-dev[0].ipv4[0]') 0/0 \
  --ssh-cmd 'ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking no" -i /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa'

@pythoninthegrass
Copy link

Thanks to @sed-i's comment and this page , was able to do something similar with vanilla ssh

ssh -i user_key ubuntu@$(multipass list --format yaml | yq '.testvm[0].ipv4[0]') -o StrictHostKeyChecking=no

What tripped me up was the ssh_authorized_keys wasn't working when nested under the users block.

The relevant snippet looks like this

#cloud-config
# vim: syntax=yaml

groups:
  - docker

users:
  - name: ubuntu
    sudo: ALL=(ALL) NOPASSWD:ALL
    gecos: Default User
    groups: users,admin,wheel,docker
    shell: /bin/bash
    ssh_import_id:
      - gh:pythoninthegrass

ssh_authorized_keys:
  - ssh-rsa AAAAB3Nza<SNIP>= ubuntu

@kiprasmel
Copy link

kiprasmel commented Mar 9, 2024

To append the ssh key post-factum:

INSTANCE="primary"
PUBKEY="$(cat ~/.ssh/id_rsa.pub)"
multipass exec -n "$INSTANCE" -- bash -c "printf '%s\n' '$PUBKEY' >> ~/.ssh/authorized_keys"

@nikelborm
Copy link

For those who installed multipass on arch from AUR:
ssh private key stored here: /var/lib/multipassd/.local/share/multipassd/ssh-keys

@jcrben
Copy link

jcrben commented May 26, 2024

On Windows this is now at: C:\ProgramData\Multipass\data\ssh-keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests