DEV Community

Cover image for Connecting to Windows and Linux VMs in Azure
Michael Bender for Microsoft Azure

Posted on • Edited on

Connecting to Windows and Linux VMs in Azure

We've selected our favorite tips and tricks created by Michael Crump and are delivering fresh technical content on Azure all April! Miss a day (or more)? Catch up with the series.

Don't have Azure? Grab a free subscription.

When you deploy virtual machines to Azure, there is a good chance you'll need to connect to them. This can be done via the portal as well as from the command line in Azure Cloud Shell.

Connecting to a Windows VM in the portal

Accessing a Windows VM through the portal is straightforward. You browse to the appropriate VM, and choose Connect. This will download an RDP client configuration file for your VM. When launched, you will be connected into your VM.

Connect to Windows VM in Azure Portal

Connect to a Windows VM Using PowerShell

But let's say you want to connect using PowerShell in Azure Cloud Shell? No problem. Simply Open PowerShell in Azure Cloud Shell and follow the process below:

- 1 - Enable Azure PSRemoting on the VM.

This will open the necessary ports in the Network Security Group, and ensure that WinRM is running on the VM. This command works on Linux VMs as well using SSH.




PS Azure:\> Enable-AzVMPSRemoting -Name 'vm-win-01' -ResourceGroupName 'azure-cloudshell-demo' -Protocol https -OsType Windows



Enter fullscreen mode Exit fullscreen mode

- 2 - Connect to VM using Enter-AzVm

With Enter-AzVm cmdlet, an interactive session with a single Azure VM is started. During the session, the commands that you type are run on the Azure VM, just as if you were typing directly on the Azure VM’s PowerShell console. This is perfect for those real-time ad-hoc scenarios.




PS Azure:\> Enter-AzVM -name 'vm-win-01' -ResourceGroupName 'azure-cloudshell-demo' -Credential (get-credential)



Enter fullscreen mode Exit fullscreen mode

- 3 - Execute a code block on the remote machine with Invoke-Command

This cmdlet allows you to run commands and script blocks against a remote system. Sometimes called 'Fan Out Remoting,' it allows you to perform 1: Many remoting, allowing you can perform the task, say get the Windows services running on a remote system, on 1 or more systems. This tooling is best used in a situation where you don't need to be providing continuous input such as when you are running commands in the shell.




PS Azure:\> Invoke-AzVMCommand -Name 'vm-win-01' -ResourceGroupName 'azure-cloudshell-demo' -ScriptBlock {get-service win*} -Credential (get-credential)



Enter fullscreen mode Exit fullscreen mode

- 4 - Disabling Azure PSRemoting

One important note is that this method relies on your VMs having Public IP addresses and ports open to your VMs; it does not work for private IPs. This means SSH and WinRM are open ports. To resolve that, just close them down when you are done with Disable-AzVMPSRemoting.




PS Azure:\> Disable-AzVMPSRemoting -Name vm-win-02 -ResourceGroupName azure-cloudshell-demo



Enter fullscreen mode Exit fullscreen mode

When executed, the cmdlet will

  • Remove the ports from the Network Security Group
  • For Windows VMs, Remove PowerShell Remoting from Windows VMs and reset UAC
  • For Linux VMS, Restore to original SSH Daemon Config & restart sshd service to pick the config

And that is all you need for connecting with Azure VMs using Powershell in Azure Cloud Shell.

Connecting to a Linux VM

For Linux VMs, the choice is pretty simple: use the CLI. To do this, you browse to the appropriate VM and choose Connect. From the menu, copy the Login using VM Local Account string into Azure Cloud Shell, either Bash or PowerShell will work, to connect to the VM.




Azure:/
PS Azure:\> ssh michael@vm-linux-02.westus2.cloudapp.azure.com
Warning: Permanently added the ECDSA host key for IP address '13.66.200.165' to the list of known hosts.
Enter passphrase for key '/home/michael/.ssh/id_rsa':
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.18.0-1014-azure x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Apr 11 10:46:28 UTC 2019

  System load:  0.24              Processes:           121
  Usage of /:   9.1% of 28.90GB   Users logged in:     0
  Memory usage: 11%               IP address for eth0: 10.1.0.5
  Swap usage:   0%

 * Ubuntu's Kubernetes 1.14 distributions can bypass Docker and use containerd
   directly, see https://bit.ly/ubuntu-containerd or try it now with

     snap install microk8s --classic

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

18 packages can be updated.
0 updates are security updates.


Last login: Tue Apr  9 02:51:07 2019 from 104.42.62.28
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

michael@vm-linux-02:~$


Enter fullscreen mode Exit fullscreen mode

As you can see above, this command can be run in Azure Cloud Shell in PowerShell as well as in Bash. This is a great feature and one of the reasons I recommend using PowerShell in Azure Cloud Shell

And that's it. Now you can connect to ALL your VMs in Azure!

Want more PowerShell in Azure Cloud Shell? Check out our quickstarts and tutorials!


We'll be posting articles every day in April, so stay tuned or jump ahead and check out more tips and tricks now.

Top comments (0)