Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

A Step-by-Step Guide to Using Enter-AzVM for Remote Administration Over SSH

I will explore Enter-AzVM cmdlet in this blog article which is a part of az.ssh module and establishes a connection over SSH port 22. In this article I will show how to setup windows server to accept SSH connection. Before you start, make sure you've using below commands.:

  1. Installed OpenSSH Server on your Windows Server
  2. Started and enabled the sshd service
  3. Opened firewall port 22 for SSH
## Install OpenSSH Server on windows ##

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

#############################################################################
## Start and enable ssh service ##

Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'

#############################################################################
## Allow Firewall port 22 for SSH ##

if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}

There are other methods if instead of SSH you want to connect over WinRM you can check previous blog: How to Run PowerShell Commands on Azure VMs Remotely

A Windows PowerShell terminal demonstrating the installation of OpenSSH Server using the Add-WindowsCapability command. It also shows the Start-Service and Set-Service commands to automate the SSH daemon, followed by a script that checks for and creates a local Windows Firewall rule for TCP port 22

Next, update your Network Security Group (NSG) to allow inbound SSH traffic, Here is simple PowerShell script, just feed the data in variables. 

PowerShell code snippet showing variables for an Azure VM and NSG. A configuration hashtable defines an inbound security rule for Port 22 with priority 2000. The script executes using a pipeline to update the Network Security Group 'devvm-nsg' in the 'eastus' region, showing a 'Succeeded' provisioning state.

$vmName = 'devvm'
$resourceGroupName = 'vdev.vcloud-lab.com'
$nsgName = 'devvm-nsg'
$nsgRuleConfig = @{
    Name = 'Allow-SSH'
    Description = 'Allow SSH Port 22'
    Access = 'Allow'
    Protocol = '*'
    Direction = 'Inbound'
    Priority = '2000'
    SourceAddressPrefix = '*'
    SourcePortRange = '*'
    DestinationAddressPrefix = '*'
    DestinationPortRange = '22'
}

Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName | Add-AzNetworkSecurityRuleConfig @nsgRuleConfig | Set-AzNetworkSecurityGroup

I can confirm network security group rule is updated to allow port 22 SSH.

A screenshot of the Microsoft Azure Portal showing the 'Inbound security rules' for a Network Security Group named 'devvm-nsg'. The image highlights a custom rule named 'Allow-SSH' with priority 2000, configured to allow TCP traffic on port 22 from any source to any destination.

Finally use Enter-AzVM command to connect to Azure VM and you are connected to Azure Windows VM over SSH. Just to know this is interactive method for non interactive way check my previous blog: How to Run PowerShell Commands on Azure VMs Remotely

If incase command not found make sure you Install-Module az.ssh which is one of the require ment.

Enter-AzVM -Name $vmName -ResourceGroupName $resourceGroupName -LocalUser azureadmin

Screenshot of a PowerShell terminal executing Enter-AzVM with Name and ResourceGroupName parameters. The user authenticates as azureadmin and runs the hostname command, which returns devvm.

Useful Articles
Microsoft PowerShell: Check Windows license activation status
Find next available free drive letter using PowerShell
Copy Files with PowerShell Remoting WINRM Protocol
Powershell Find application window state minimized or maximized
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell fix repair The trust relationship between this workstation and the primary domain failed
Resovled issue with PowerShell - Trust relationship Rejoin computers in domain without restart
PowerShell remoting over HTTPS using self-signed SSL certificate
Configure Powershell WinRM to use OpenSSL generated Self-Signed certificate
Powershell WinRM HTTPs CA signed certificate configuration
Powershell Generate Self-signed certificate with Self-Signed Root CA Signer
 

Go Back

Comment

Protected by Mathcha

Blog Search

Page Views

1 4 6 7 6 2 7 8

Archive

Follow me on Blogarama