Connect to Your VM via SSH
Learn how to securely connect to your Azure VM using SSH. Step-by-step guide with troubleshooting and next steps.
Focus: connect to your vm via ssh
So you've provisioned an Azure Linux VM — maybe through the portal, maybe with az vm create — and now you're staring at a public IP address wondering what to do next. The painful truth is that a VM without a secure way to reach its shell is just a monthly bill, and the fastest path to that shell is SSH. In this lesson, you'll learn how to connect to your VM via SSH — from generating keys on your local machine to running your first remote command — and you'll come away with the confidence to manage any Azure Linux VM from your terminal, whether you're on macOS, Windows, or Linux.
The problem this lesson solves
You've deployed a VM, but now what? Without SSH, you're locked out of the command line — you can't install packages, configure services, or debug application failures. The portal's "Run command" feature is clunky and limited; it's not a real terminal. And the most common mistake beginners make is trying to use a password instead of key-based authentication, which is both less secure and surprisingly harder to set up on Azure.
Pro tip: Azure disables password authentication by default for Linux VMs created via the CLI. If you try to SSH with a password and get
Permission denied (publickey), that's why.
Core concept / mental model
Think of SSH as a secure tunnel between your laptop and your Azure VM. The tunnel is authenticated using a key pair: a private key that stays on your machine (like your house key) and a public key that you place on the VM (like a lock that only your key can open). When you attempt to connect, the server checks that your private key matches the public key it holds — no passwords sent over the wire.
Azure makes this easy by letting you inject the public key during VM creation, or you can add it later. Once SSH is working, you have a full remote shell — you can run sudo apt update, edit config files with vim, or tail application logs, all as if you were sitting at the machine's keyboard.
How it works step by step
- Generate a key pair on your local machine using
ssh-keygen— this createsid_rsa(private) andid_rsa.pub(public) in~/.ssh/. - Provide the public key to Azure — either at VM creation time with
--ssh-key-valuesor by later appending it to~/.ssh/authorized_keyson the VM. - Connect with
ssh azureuser@<public-ip>— the SSH client and server negotiate encryption, verify your private key, and open the tunnel. - Run commands — once connected, every keystroke is sent securely to the remote shell.
The first time you connect, you'll see a host authenticity prompt — type yes to accept the VM's fingerprint. This is your protection against man-in-the-middle attacks, so don't ignore it.
Hands-on walkthrough
Step 1: Generate your SSH key pair
Open a terminal (or PowerShell with OpenSSH client) on your local machine and run:
ssh-keygen -t rsa -b 4096 -C "you@example.com" -f ~/.ssh/azure_vm_key
Expected output (paraphrased):
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/you/.ssh/azure_vm_key
Your public key has been saved in /home/you/.ssh/azure_vm_key.pub
Pro tip: Add a passphrase to encrypt your private key at rest. You'll type it each time you connect, but it prevents misuse if your laptop is lost.
Step 2: Create a VM with your public key
Use the az CLI to create the VM, passing the public key content directly:
az vm create \
--resource-group MyResourceGroup \
--name MyVM \
--image Ubuntu2204 \
--admin-username azureuser \
--ssh-key-values ~/.ssh/azure_vm_key.pub
Expected output: a JSON blob with publicIpAddress — note it down.
Step 3: Connect to your VM via SSH
Now the moment you've been waiting for — connect:
ssh -i ~/.ssh/azure_vm_key azureuser@<public-ip>
If it's your first time, you'll see:
The authenticity of host '40.118.90.45 (40.118.90.45)' can't be established.
ED25519 key fingerprint is SHA256:...
Are you sure you want to continue connecting? Type 'yes' and hit Enter.
Type yes, and you're in! You should see a prompt like azureuser@MyVM:~$. Try a quick command:
uname -a
Output shows the kernel info — proof you're connected to the VM.
Step 4: Add the public key to an existing VM
If you already created a VM without SSH keys, use az vm user update to inject your key:
az vm user update \
--resource-group MyResourceGroup \
--name MyVM \
--username azureuser \
--ssh-key-value ~/.ssh/azure_vm_key.pub
Then connect as above.
Compare options / when to choose what
| Method | When to use | Pros | Cons |
|---|---|---|---|
| Key-based SSH | Default for any Linux VM | Secure, scriptable, no passwords | Must manage private keys |
| Password-based SSH | Legacy or test-only VMs | Easy for a quick demo | Less secure, Azure disables by default |
| Azure Bastion | Enterprise environments | Browser-based, no public IP needed | Costs extra, requires setup |
Azure CLI az ssh |
Quick, browser-based access | No client config needed | Requires Azure AD login, less flexible |
For most scenarios, key-based SSH is the way to go. Azure Bastion shines when you need to restrict public exposure entirely. az ssh is handy for on-the-go troubleshooting from a web browser.
Troubleshooting & edge cases
| Error | Cause | Fix |
|---|---|---|
Permission denied (publickey) |
Wrong key or key not in authorized_keys |
Re-run az vm user update with the correct public key; verify you used the -i flag with the right private key |
Connection timed out |
Network security group (NSG) blocks port 22 | Add an inbound rule for TCP port 22 with az vm open-port or via portal |
Host key verification failed |
VM's fingerprint changed (e.g., VM recreated) | Edit ~/.ssh/known_hosts and remove the old entry manually, or use ssh-keygen -R <ip> |
WARNING: UNPROTECTED PRIVATE KEY FILE |
File permissions too open on your private key | Run chmod 400 ~/.ssh/azure_vm_key |
No such file or directory |
Path to key file is wrong | Use the full path or cd to ~/.ssh before connecting |
Pro tip: If you're on Windows, use the built-in OpenSSH client (available in PowerShell). If you encounter issues, ensure the
ssh-agentservice is running and your key is added withssh-add.
What you learned & what's next
You now own the core skill of remote administration: connect to your VM via SSH. You can generate key pairs, provision a VM with your public key, tackle common connectivity errors, and decide when a password, Bastion, or az ssh makes more sense. This unlocks the next lesson in the Azure Tutorial track, where you'll use this SSH connection to configure your VM for real workloads — perhaps installing a web server or connecting to a managed database.
Keep your private key safe, practice connecting a few times, and remember: every cloud engineer's journey starts with a single ssh command.
Practice recap
To solidify what you just learned, create a new VM with a fresh key pair and connect three times in a row. Then, deliberately cause one of the common errors (like a typo in the IP or a chmod 777 key) and debug it. Tomorrow, move on to the next lesson in the Azure Tutorial path — your SSH skills will be the foundation for everything you do on the VM.
Common mistakes
- Forgetting to specify the
-iflag when your key isn't in the default~/.ssh/id_rsalocation. - Creating a VM without a public key and then trying to password-auth — Azure blocks it by default.
- Not opening port 22 in the NSG, leading to timeouts instead of a clear error.
- Ignoring the
UNPROTECTED PRIVATE KEY FILEwarning —chmod 400your key immediately.
Variations
- Use
ssh-copy-idto push your public key to an existing VM'sauthorized_keysfile for a quick setup. - Set up an SSH config file (
~/.ssh/config) to store the host alias, user, and key path for faster connections. - Use Azure Bastion for a browser-based shell when you want zero public IP exposure.
Real-world use cases
- Deploying a Python Flask app to an Azure VM and debugging it via SSH by tailing logs.
- Automating server configuration with Ansible, which uses SSH to run playbooks against Azure VMs.
- Setting up a cron job on a VM to pull data from an Azure Blob Storage container every night via SSH.
Key takeaways
- SSH uses a private/public key pair — your private key stays local, your public key goes on the VM.
- Always generate a unique key pair per project and protect the private key with a passphrase.
- Azure requires you to pass the public key at creation or use
az vm user updatefor existing VMs. - TCP port 22 must be open in the NSG for SSH to reach your VM.
- Troubleshoot with systematic checks: key permissions, port open, and correct user/IP.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.