How to use the command 'az sshkey' (with examples)
The az sshkey
command is part of the azure-cli
(also known as az
) and is used to manage ssh public keys with virtual machines on Azure. With this command, users can create new SSH keys, upload existing SSH keys, list all SSH public keys, and view information about a specific SSH public key.
Use case 1: Create a new SSH key
az sshkey create --name name --resource-group resource_group
Motivation: Creating a new SSH key is essential for securely accessing virtual machines on Azure. By generating a new SSH key, users can define the access permissions and securely authenticate to their virtual machines.
Explanation:
--name
: This argument specifies the name of the SSH key.--resource-group
: This argument specifies the resource group under which the SSH key will be created.
Example output:
{
"keyName": "mySSHKey",
"location": "westus",
"publicKey": "ssh-rsa AAAAB..."
}
Use case 2: Upload an existing SSH key
az sshkey create --name name --resource-group resource_group --public-key "@path/to/key.pub"
Motivation: Users may already have existing SSH keys that they want to use for accessing their virtual machines on Azure. By uploading an existing SSH key, users can easily configure their access without having to generate a new key.
Explanation:
--name
: This argument specifies the name of the SSH key.--resource-group
: This argument specifies the resource group under which the SSH key will be created.--public-key
: This argument specifies the path to the existing public key file that needs to be uploaded.
Example output:
{
"keyName": "mySSHKey",
"location": "westus",
"publicKey": "ssh-rsa AAAAB..."
}
Use case 3: List all SSH public keys
az sshkey list
Motivation: When managing multiple SSH keys, it can be helpful to list all the existing SSH public keys associated with the Azure account. This allows users to quickly view and validate their SSH keys.
Explanation: This command does not require any arguments.
Example output:
[
{
"keyName": "mySSHKey",
"location": "westus",
"publicKey": "ssh-rsa AAAAB..."
},
{
"keyName": "anotherSSHKey",
"location": "eastus",
"publicKey": "ssh-rsa BBBBC..."
}
]
Use case 4: Show information about an SSH public key
az sshkey show --name name --resource-group resource_group
Motivation: At times, users may need to retrieve specific information about a particular SSH public key, such as its location or the actual key value. This use case allows users to retrieve detailed information about the specified SSH public key.
Explanation:
--name
: This argument specifies the name of the SSH key.--resource-group
: This argument specifies the resource group under which the SSH key is located.
Example output:
{
"keyName": "mySSHKey",
"location": "westus",
"publicKey": "ssh-rsa AAAAB..."
}
Conclusion:
The az sshkey
command is a useful tool for managing SSH public keys with virtual machines on Azure. Whether creating new keys, uploading existing ones, listing all keys, or viewing specific key information, this command provides users with the necessary functionality to securely access their virtual machines.