2 Local machines 1 Dev VM
Tags: azure, ssh, azurevm
Published: 2021-01-16 19:53:36

---
title: 2 Local machines 1 Dev VM
published: true
description: Azure Remote Development VM: Adding SSL connection
tags: Azure, SSH, AzureVm
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/cr10g59i03ssyv05sbpg.png
---


I recently wanted to connect a new laptop to one of the Azure virtual machines that I use for remote development. I realized this isn't something easy to do within the azure portal and I have limited knowledge in Linux (ubuntu) and SSL. So after I accomplished this I decided to share what I learned. 

This blog post isn't just to learn how to connect a local machine to a VM. It will also give you a basic understanding of how to set up an SSH connection 

## Content: 
- [What i am using](#what-i-am-using)
- [1. Create new VM (Optional)](#1-create-a-new-vm-optional)
- [2. Create new ssh keys on new local machine](#2-create-new-ssh-keys-on-the-new-local-machine) 
  - [NOTE: SSH understanding](#note-ssh-understanding)
- [3. Add your public key to your Azure VM](#3-add-your-public-key-to-your-azure-vm)
  - [Integrity check!](#integrity-check)
- [4. Get ready for SSH connection](#4-get-ready-for-ssh-connection)
- [5. Actually do the SSH connection](#5-actually-do-the-ssh-connection)

## What I am using
* Azure VM (Ubuntu Release 18.XX / bionic) 
* [Azure cli](https://docs.microsoft.com/en-us/cli/azure/) 
* VS code [Remote Development](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) extention pack
* Windows 10 local machine

## 1. Create a new VM (Optional)

If you already have a VM you wish to do this on then you can skip this step.
Create a Linux VM in azure. Choose whichever size you need and in this article, I used Ubuntu Release 18.

// TODO az create VM code with ssh keys. what user does it create?
set a user varable to make it easier later `$user = "%myuser%"`
// TODO remember the user you created for this VM

## 2. Create new ssh keys on the new local machine
1. On windows I used this command `ssh-keygen -t rsa` 

2. I add the path where to store the keys. i used `C:\Users\UserName\.ssh\my-vs-code-vm`

3. I then add in my passphrase (this is optional but adds security. If you choose to use it REMEMBER THE PASSPHRASE)

    ![ssh keygen command execution](https://dev-to-uploads.s3.amazonaws.com/i/kgzou5f9phxkifqitgc1.png)

    Great now we have our keys.

### NOTE: SSH understanding
`my-vs-code-vm` 
* Your new private key 
* This stays on your machine and you need to keep it safe

`my-vs-code-vm.pub` 
* Your new public key 
* This is going onto your VM so your private key has something to unlock... like the ssh connection we want to establish

## 3. Add your public key to your Azure VM
Here we will be using the [azure cli](https://docs.microsoft.com/en-us/cli/azure/) to add our new public key remotely execute commands remotely on our virtual machine.

Specifically, we will be using the [az vm run-command](https://docs.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest).

1. Make sure you logged in `az login`. 
2. Make sure you have the right Subscription active. `az account show` change with `az account set -s //*correct subscription guid*//`
3. Get our public key and put it into a variable

   `$publickey = cat ~/.ssh/my-vs-code-vm.pub`

### Integrity check!
make sure your `$publickey` value looks like this:

![Publickey Integrity Check](https://dev-to-uploads.s3.amazonaws.com/i/3mjd409w0q8a4euuwt9u.png)

4. now that we have our public key ready let's send it off to our VM.
We need to store our public key in the `authorized_keys` file for the user we will connect to.

`az vm run-command invoke --name my-vs-code-vm --resource-group my-dev-vm-rg --command-id RunShellScript --scripts "echo '$publickey' >> /home/$user/.ssh/authorized_keys"` 

## 4. Get ready for SSH connection

1. We will need the `ssh` command to make the connection to the VM. The easiest way I generate it is from the Azure portal. all you need to do is add you **Private Key** Path in the Azure UI:

![Get remote connect for add new remote ssh](https://dev-to-uploads.s3.amazonaws.com/i/uuy01ad1oqwgxya1seej.png)

2. Copy the command from **4. Run the example command below to connect to your VM.** 
   * NOTE: 
you have what you need to just ssh with your local terminal right now but let's keep going. 

3. Now in VS Code open the Command Palette (`Ctrl+Shift+P`).

4. Select `Remote-SSH: Add New SSH Host...` and paste the `ssh` command we got from azure (if you don't see it you need to make sure you have the extension installed/enabled) // TODO make sure the remote extension is linked 

5. Next you will be prompted to `Select SSH configuration file to update'. I use the first option that will store it under my local user

![Select SSH configuration file to update](https://dev-to-uploads.s3.amazonaws.com/i/kjn7j5jy3r0jsnk0cg0e.png)

6. You will see this pop-up. Select `Open Config` and change the `Host` from the ip address to a friendly name like `my-vs-code-vm`. I highly recommend doing this!
   
   ![](https://dev-to-uploads.s3.amazonaws.com/i/yawbfy1k1z21pojh0o1e.png)

   Config file before:

   ![](https://dev-to-uploads.s3.amazonaws.com/i/dkr655s1vo3j9b559kof.png)

   Config file after:

   ![](https://dev-to-uploads.s3.amazonaws.com/i/axkwfidfo2c02di5fi48.png)

## 5. Actually do the SSH connection


1. Open the VS Code Command Palette again (`Ctrl+Shift+P`).

2. Select `Remote-SSH: Connect to Host...` this time and select our config `my-vs-code-vm` and the rest should be straight forward.