1

Create VM ssh public key

I'm trying to create a virtual machine programmatically with ssh public key using azure java SDK. I saw the create vm example and there we can see:

request.getOsProfile().setAdminUsername(adminUsername);
request.getOsProfile().setAdminPassword(adminPassword);

My question is - what should I set to this OsProfile in order to create a VM with the SSH public key from the attached image?

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
s-rusev
  • 150
  • 1
  • 11

2 Answers2

2

You could set the LinuxConfiguration property in OSProfile. Here is the sample code:

// Set up SSH public key
String fullStorePath = " ~/.ssh/authorized_keys";
String sshKeyData = "you-ssh-key-data";

SshPublicKey sshPublicKey = new SshPublicKey();
sshPublicKey.setPath(fullStorePath);
sshPublicKey.setKeyData(sshKeyData);

// SShConfiguration
ArrayList<SshPublicKey> keyList = new ArrayList<SshPublicKey>();
keyList.add(sshPublicKey);

SshConfiguration sshConfig = new SshConfiguration();
sshconfig.setPublicKeys(keyList);

// Linux Configuration
Bool shouldDisablePasswordAuthentication = False;
LinuxConfiguration linuxConfig = new LinuxConfiguration();
linuxConfig.setSsh(sshConfig);
linuxConfig.setDisablePasswordAuthentication(shouldDisablePasswordAuthentication);

// set your OSProfile now
request.getOsProfile().setLinuxConfiguration(linuxConfig);

// you code goes here
Steven
  • 804
  • 4
  • 12
1

According to the property linuxConfiguration of the request json body for the REST API Create or update a VM, as @Steven said, it's correct and great for setting the SshConfiguration & LinuxConfiguration for OSProfile.

Thanks for Steven's sharing.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43