Skip to main content

Accessing VMs Securely with Azure Bastion

Deploy Azure Bastion to reach VMs over SSH or RDP in the browser with no public IP and no open management ports. Includes a CLI lab.

Overview and What You Will Learn

Azure Bastion is a managed service that gives you SSH and RDP access to VMs through the Azure Portal over HTTPS, so the VMs need no public IP address and no open management port. In this lab, you will deploy Bastion into a VNet, create a VM with no public IP and no inbound rules, connect to it from the browser, and confirm its management port was never exposed to the internet.

Why This Matters in Production

Bastion removes the temptation to expose a management port, because secure access is already available. A team at a company like Atlassian opens SSH to the internet on a production VM "just for now, I'll close it later," and that port becomes exactly where a real compromise starts. Automated scanners find open management ports within hours, not weeks.

Core Principles

Azure Bastion is deployed once per VNet and reaches VMs over their private IP addresses, so the VM's own port 22 or 3389 is never published. Your browser talks to Bastion over HTTPS on port 443, and Bastion talks to the VM privately inside Azure.

flowchart TD U([You in a browser]) -->|HTTPS 443| B subgraph VNET[vnet-bastion-lab] B[Azure Bastion
AzureBastionSubnet] V[VM, private IP only
subnet-vms] B -->|Private SSH| V end classDef person fill:#8B5CF6,color:#FFFFFF classDef service fill:#326CE5,color:#FFFFFF classDef vm fill:#10B981,color:#FFFFFF class U person class B service class V vm

Bastion requires its own dedicated subnet named exactly AzureBastionSubnet, with a /26 range or larger, inside the same VNet as the VMs it serves.

Option Management port exposed? Best for
VM public IP with open port Yes Nothing in production
Azure Bastion No Browser access to many VMs
VPN or ExpressRoute No Private access to the whole network
Remember

Bastion bills by the hour for as long as it exists, not per connection. Delete it when you finish the lab.

Detailed Step-by-Step Practical Lab

How to Create the Resource Group and VNet
Bash
export RG="rg-bastion-lab-mumbai"
az group create --name $RG --location centralindia
az network vnet create \
--resource-group $RG \
--name vnet-bastion-lab \
--address-prefix 10.0.0.0/16 \
--subnet-name subnet-vms \
--subnet-prefix 10.0.1.0/24
How to Add the Required AzureBastionSubnet

The subnet name must be exactly AzureBastionSubnet, because Azure looks for that name when it deploys Bastion. A differently named subnet with the same range will not work.

Bash
az network vnet subnet create \
--resource-group $RG \
--vnet-name vnet-bastion-lab \
--name AzureBastionSubnet \
--address-prefix 10.0.2.0/26
How to Create a Public IP for Bastion

This is the only public IP in the whole setup, and it belongs to Bastion, not to any VM. Bastion requires a Standard SKU public IP.

Bash
az network public-ip create \
--resource-group $RG \
--name pip-bastion-lab \
--sku Standard
How to Deploy Azure Bastion with the CLI

The Basic SKU is enough for browser-based SSH and RDP. Deployment typically takes 5 to 10 minutes.

Bash
az network bastion create \
--resource-group $RG \
--name bastion-lab \
--public-ip-address pip-bastion-lab \
--vnet-name vnet-bastion-lab \
--location centralindia \
--sku Basic
How to Create a VM With No Public IP and No Inbound Rules

Create an NSG with no custom rules first. The default AllowVnetInBound rule is enough for Bastion to reach the VM from inside the VNet, so you never add an allow rule for the internet. Read the password at the prompt so it does not land in your shell history.

Bash
az network nsg create --resource-group $RG --name nsg-vms
read -s -p "Choose a VM admin password (12+ characters): " VM_PASS; echo
az vm create \
--resource-group $RG \
--name vm-private-lab \
--image Ubuntu2204 \
--size Standard_B1s \
--vnet-name vnet-bastion-lab \
--subnet subnet-vms \
--public-ip-address "" \
--nsg nsg-vms \
--nsg-rule NONE \
--authentication-type password \
--admin-username azureadmin \
--admin-password "$VM_PASS"
How to Confirm the VM Has No Public IP

Check the public IP and the NSG rules. Both should come back empty.

Bash
az vm show \
--resource-group $RG \
--name vm-private-lab \
--show-details \
--query "publicIps" --output tsv
# Expected: empty output, no custom inbound rules
az network nsg show \
--resource-group $RG \
--name nsg-vms \
--query "securityRules[].name" --output tsv
How to Connect to the VM Through Bastion

In the Azure Portal, open the VM, select Connect, then Connect via Bastion. Choose Password as the authentication type, enter azureadmin and the password you set, and select Connect. A browser SSH session opens. Inside it, check that the VM only has a private address:

Bash
hostname -I
# Expected: a private address such as 10.0.1.4
Tip

You need Reader on the VM, its NIC, and the Bastion resource, plus a valid login on the VM, to connect. Bastion does not bypass RBAC.

How to Clean Up

Delete the resource group to stop Bastion's hourly billing.

Bash
az group delete --name $RG --yes --no-wait

Production Best Practices & Common Pitfalls

Common Mistake

Skipping Bastion "just for a quick one-time fix" and temporarily opening RDP or SSH to the internet instead. The moment that thought appears is the moment to deploy Bastion, because "just for now" is where real compromises begin.

Security

If you attach an NSG to AzureBastionSubnet, it must include the rules Bastion needs, such as inbound 443 from the internet and from the GatewayManager service tag, and outbound 22 and 3389 to the VNet. A missing rule breaks deployment or connections.

Tip

One Bastion deployment serves every VM in its VNet, and can also reach VMs in peered VNets. It is a shared entry point, not a per-VM cost. For low-cost dev and test, the Developer SKU in supported regions connects to one VM at a time without a dedicated subnet.

  • Keep the AzureBastionSubnet name exact. Azure specifically looks for this name, and a different name will not work even with an identical range.
  • Use a /26 or larger for the Bastion subnet. Smaller ranges are rejected, and a larger one leaves room for scale units.
  • Bastion complements, not replaces, access controls. RBAC and NSG rules on the destination VM's subnet still apply, because Bastion replaces the public IP and open port, not the need for permissions.
  • Prefer SSH keys over passwords in real environments. Store keys in Azure Key Vault and select them in the Bastion connect pane instead of typing a password.
  • Delete lab Bastion hosts promptly. The hourly charge continues whether or not anyone is connected.

Quick Reference & Troubleshooting Commands

Command Description
az network vnet subnet create --name AzureBastionSubnet Create the required dedicated subnet
az network bastion create --sku Basic Deploy Azure Bastion into a VNet
az vm create --public-ip-address "" Create a VM with no public IP at all
az vm show --show-details --query "publicIps" Confirm a VM has no public IP assigned
az network nsg show --query "securityRules[].name" Confirm no custom inbound rules exist
az network bastion list --output table Check that Bastion shows Succeeded provisioning

Explore More in Azure Networking and Traffic Management

All 6 Topics

Frequently Asked Questions

Is Accessing VMs Securely with Azure Bastion free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Accessing VMs Securely with Azure Bastion topic cover?

Deploy Azure Bastion to reach VMs over SSH or RDP in the browser with no public IP and no open management ports. Includes a CLI lab.

Found a mistake, an outdated step or a broken link?