Azure – Create a new Linux VM and connect to using SSH
In this post I want to show the separate steps to create a new Linux VM in Azure and later connect to using SSH private/public keys.
If you are interested in how the same works in Google Cloud and AWS, you can read my following posts.
In the Azure portal open the Virtual Machines services and click on Create –> Azure virtual machine
When using SSH with private/public key authentication, you can choose between:
- Generate a new key pair
- Use existing key stored in Azure
- Use existing public key
You can also choose to use password authentication here.
I will create a new key pair below.
By default inbound traffic port 22 TCP will be granted from any source. You can restrict access later to allow only traffic from known IP addresses.
When the VM is created you can control access to and from the VM under the network settings as shown below.
Save the private key to your computer.
Connect to a Azure Linux VM using SSH private/public key authentication
To connect from another Linux VM to our new VM, we can use the ssh command and -i parameter (identity file) for the private key file to connect to.
$ ssh -i /path/to/<private key file> username@Host -p22
To connect to the new Linux VM from a Windows host we can use PuTTY to connect to.
In order to use the previously downloaded private SSH key in PuTTY, we first need to convert it to the .ppk format.
In PuTTYgen click on File –> Load private key
The private key file will be converted and finally you have to save it.
Now we just need to check the public IP address and we can use PuTTY to connect to the VM from Windows.
In PuTTY create a new Session with the public IP address, port number default 22 for SSH and save the session.
We also need to add the previously created private SSH key in .ppk format to use with PuTTY.
Connection –> SSH –> Auth –> Private key file for authentication
Finally I can connect to my Linux VM using the username (by default azureuser), set during the creation of the VM and the private key from the generated key pair.
Create SSH Key Store
If you frequently use the portal to deploy Linux VMs, you can make using SSH keys simpler by creating them directly in the portal, or uploading them from your computer.
You can create a SSH keys when you first create a VM and reuse them for other VMs. Or, you can create SSH keys separately, so that you have a set of keys stored in Azure to fit your organizations needs.
If you have existing keys and you want to simplify using them in the portal, you can upload them and store them in Azure for reuse.
For more detailed information about creating and using SSH keys with Linux VMs, see Use SSH keys to connect to Linux VMs.
Generate new keys
Open the Azure portal and search for SSH.
On the SSH Key page, select Create.
Enter a name for the key pair and select the source, I will create a new one here.
Download the private key to your computer you want to connect from.
Below I created separately two key pairs, ssh_keypair_azureuser and ssh_keypair_azureuser02. The last one Ubuntu-Setup1_key was generated during the creation of my VM previouly and shown further above by using Generate new key pair.
When I now create a new Linux VM, I didn’t have to generate a new SSH key pair and can use instead an existing one stored in Azure.
So I can use my existing private key also for this new Linux VM.
So now I can choose one of my stored SSH public keys in Azure to use for the VM.
The username you set for the new VM doesn’t matter regarding the SSH key pair.
How to use SSH keys with Windows on Azure
Generate and store SSH keys in the Azure portal