Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
9.6k views
in Technique[技术] by (71.8m points)

windows - Add-AzVMSshPublicKey to vmConfig fails when calling New-AzVM

I'm following the windows quickstart for creating a VM in azure powershell

I'm stuck here:

# Configure the SSH key
$sshPublicKey = cat ~/.ssh/id_rsa.pub
Add-AzVMSshPublicKey `
  -VM $vmconfig `
  -KeyData $sshPublicKey `
  -Path "/home/azureuser/.ssh/authorized_keys"

First of all I think the following code is wrong, as cat returns System.String[] and running this verbatim results in Add-AzVMSshPublicKey : Cannot convert 'System.Object[]' to the type 'System.String'

So... I instead use Get-Content "./path/to/file" -raw which just returns a string and the command runs without errors

Now when I run

New-AzVM `
  -ResourceGroupName $resourceGroupName `
  -Location $location -VM $vmConfig

I get the following error, meaning the keyData I set earlier wasn't set correctly. New-AzVM : The value of parameter linuxConfiguration.ssh.publicKeys.keyData is invalid.

I've found the issue - So Azure key vault gives me a PEM public key in the form -----BEGIN PUBLIC KEY----- MIIBojANBgkqhkiG9w0BAQEFAAO... ... ... ... ...0CS94AFAgMBAAE= -----END PUBLIC KEY-----

Whereas the VM is expecting it in OpenSSH format

ssh-rsa ..........

I've tried to convert it with ssh-keygen -i -m PKCS8 -f ./key.pem but nothing gets output

UPDATE Aaaand it's a powershell issue


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, the tutorial is a guide to create Linux VM via Azure PowerShell, not Windows. Second, the command cat just outputs the content of the file. And command $sshPublicKey = cat ~/.ssh/id_rsa.pub creates a variable in string:

enter image description here

The parameter -KeyData of the command Add-AzVMSshPublicKey also expect a string:

enter image description here

So there is no problem with the PowerShell command and all the commands work fine on my side. And the error shows the value of the key data is invalid, what you need to do is to make sure if the SSH public key is no problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...