Azure: SSH to a VM on Azure
A Brief Overview of SSH
Secure SHell (SSH) is a protocol on the application layer of the OSI model. It is used for remote encrypted connection between two devices. It uses asymmetric key cryptography techniques to safely create a shared key for both devices without needing to send the key across the network. It then uses the shared (symmetric) key for further encryption over the channel. SSH runs on TCP port 22 by default.
Creating a VM on Azure
- Login to your Azure portal and click on the "Create a Resource" option on the dashboard (you can also find that option on the navigation bar).
- In the create resource page, select Virtual machine, you will be navigated to the Create Virtual Machine page.
- Enter the required information to create your VM.
A few things to pay attention to in regards to this article:
- In the Authentication Type section of the Administrator Account, select SSH public key as the means to authenticate.
- Enter your VM username.
- Generate a new key pair (unless you require you to use an existing keypair).
- For the key type, I recommend Ed25519 because it is faster, and then enter your keypair name.
- Another important thing is the inbound port rule: make sure port 22 is open.
Once you're done with all your network, storage, and other configurations, you can go ahead and create the VM.
Connecting to the Virtual Machine
When you created the VM, a key.pem file was downloaded to your computer; this will be used for authentication.
Move this file to the
~/.sshfolder (create one if it's not already there).Change the permissions of the key.pem file to give readonly access:
chmod 400 ~/.ssh/key.pemGet the public IP address of your virtual machine from the Azure portal.
To connect to your cloud VM, run this command on your terminal:
ssh -i ~/.ssh/key.pem testvm@102.113.114.200
In general, the syntax is:
ssh -i <path_to_key_file> <vm_name>@<vm_ip_addr>
Since this is the first time connecting to the VM, you will get a message like this:
The authenticity of host '102.133.144.206 (102.133.144.206)' can't be established.
ED25519 key fingerprint is SHA256:bbZQo2Ph2ZV77Koob9iQT7i0G+oDNZVo9Q3fMg89T64.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes to proceed.
What this means is we should verify the host (the VM) fingerprint before connecting. Even though we just created the VM, we should still verify the returned fingerprint to make sure this response is actually from the VM. We need to make sure the fingerprint on the server matches the one returned to us.
To verify the fingerprint, run this command on the VM through the Azure portal:
ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub | awk '{print $2}'
To run a command on your VM through the Azure portal:
- Go to the VM dashboard.
- In the sidebar, navigate to Operations, and click Run command.
- On the current page, click Run shell script and enter the command above.
Once the server fingerprint has been verified, proceed with the connection and now you are connected to your remote VM through SSH.
Comments
No comments yet. Start the discussion.