Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are auto generated SSH keys stored in Windows using azure cli --generate-ssh-keys

I am trying to create linux VM with azure cli from local machine. I was able to create VM using following command but now when I want to ssh into the VM, I need to have public key on my local machine?

How can I get the required public key to connect to vm using ssh azureuser@publicIpAddress? Where are the ssh keys generated by --generate-ssh-keys and how to get it?

az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

I used PowerShell 7.0 with elevated privileges to run the above command

Solved

Update 1:

  • SSH keys are generated in c:\users\.ssh\ when you use --generate-ssh-keys switch
  • If there are already file with name id_rsa & id_rsa.pub then it might be conflicting and you can use another switch --ssh-key-values /path/to/public/key to specify different file name
  • After that if you get Permissions for 'private-key' are too open error then follow steps mentioned here
like image 557
Kundan Avatar asked Jun 06 '20 07:06

Kundan


People also ask

Where SSH keys are stored in Azure?

SSH keys are by default kept in the ~/. ssh directory.

Where SSH keys are stored in Windows?

The public part of the key is saved in the id_rsa. pub file, while the private part is saved in the id_rsa file. Both files can be accessed from this location using Explorer: C:\Users\[your user name]\. ssh .

Where are SSH keys generated?

SSH keys for user authentication are usually stored in the user's . ssh directory under the home directory. However, in enterprise environments, the location is often different. The default key file name depends on the algorithm, in this case id_rsa when using the default RSA algorithm.


2 Answers

From MSDN for --generate-ssh-keys:

Generate SSH public and private key files if missing. The keys will be stored in the ~/.ssh directory.

Which will by default create a private id_rsa and public id_rsa.pub SSH key pair in the ~/.ssh directory if they don't exist. If you already have existing SSH keys in that location, it will just use those and not overwrite them.

On Windows this is the C:\Users\username\.ssh directory.

Additionally, you could also pass in a specific SSH public key path with --ssh-key-values:

az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --ssh-key-values /path/to/public/key

This is particularly useful if you have created SSH keys in another location with ssh-keygen.

As @Ash pointed out in the comments, you could have a look at Generate keys automatically during deployment for more information.

like image 58
RoadRunner Avatar answered Oct 16 '22 21:10

RoadRunner


You can find it under

C:\Users\<<your-user-name>>\.ssh

When you create a VM for the first time you get the below message as well

SSH key files 'C:\Users\<<your-user-name>>\.ssh\id_rsa' and 'C:\Users\<<your-user-name>>\.ssh\id_rsa.pub' have been generated under ~/.ssh to allow SSH access to the VM. If using machines without permanent storage, back up your keys to a safe location.

For subsequent VM creations, the same key pair is used and you don't see the message again.

(I verfied using Windows OS, creating a linux VM with RedHat:RHEL:7-RAW:7.4.2018010506 image via Azure CLI)

like image 42
Guru Pasupathy Avatar answered Oct 16 '22 20:10

Guru Pasupathy