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?

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.