Migrating workloads to Microsoft Azure usually starts with a single virtual machine. The problem is that a freshly deployed server is often left with its default settings: SSH open to the whole internet, password authentication enabled, and no update strategy. In this guide you will create an Azure Linux virtual machine from the command line and then harden it properly, so it is ready for real production work.
Prerequisites – How to Create and Harden an
- An Azure account with an active subscription and permission to create resources
- Azure CLI 2.50 or later installed and signed in (see our guide on installing the Azure CLI)
- An SSH key pair on your local machine
- Basic familiarity with SSH — if you need a refresher, read our tutorial on how to SSH into a Linux server
Step 1: Create a Resource Group – How to Create and Harden an
Every Azure resource lives inside a resource group. Create one first:
az group create --name rg-linux-prod --location eastus
Pick a region close to your users. Resource groups make cleanup and cost tracking much easier later.
Step 2: Create the Azure Linux Virtual Machine
The following command creates an Ubuntu 22.04 LTS virtual machine named vm-web-01. It uses the burstable Standard_B2s size (2 vCPUs, 4 GB RAM), which is inexpensive and fine for small websites and services:
az vm create \
--resource-group rg-linux-prod \
--name vm-web-01 \
--image Ubuntu2204 \
--size Standard_B2s \
--admin-username azureadmin \
--ssh-key-values ~/.ssh/id_rsa.pub \
--public-ip-sku Standard
Note that we only pass the public key. The private key never leaves your machine. When the command finishes, note the publicIpAddress value in the JSON output. If you do not have a key pair yet, follow our guide on how to generate SSH keys on Linux first.
Step 3: Restrict SSH Access in the Network Security Group
By default, Azure creates a network security group (NSG) rule that allows SSH from any source. For an Azure Linux virtual machine exposed to the internet, that is an invitation to brute-force bots. Restrict port 22 to your own IP address:
az network nsg rule update \
--resource-group rg-linux-prod \
--nsg-name vm-web-01NSG \
--name default-allow-ssh \
--source-address-prefixes 203.0.113.10/32
Replace 203.0.113.10 with your real public IP. Connect to the server and confirm everything works:
ssh -i ~/.ssh/id_rsa azureadmin@<public-ip>

Step 4: Harden the Azure Linux Virtual Machine
With access confirmed, apply the standard hardening checklist.
Update the system and enable automatic updates
sudo apt update && sudo apt upgrade -y
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
Disable password authentication and root login
Edit /etc/ssh/sshd_config and set the following values, then restart SSH:
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
sudo systemctl restart ssh
Keep your current session open until you have verified that a new login still works — locking yourself out of a cloud VM is a classic beginner mistake.
Enable the host firewall and Fail2Ban
The NSG filters traffic at the Azure level, but defense in depth means also running a host firewall. Our tutorial on configuring UFW on Ubuntu and the guide to installing Fail2Ban to protect SSH cover both in detail. At minimum:
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
Step 5: Verify the VM State from Azure CLI
az vm show --resource-group rg-linux-prod --name vm-web-01 --show-details --query "{Name:name, Size:hardwareProfile.vmSize, IP:publicIps, Status:powerState}" --output table
For the official reference on these commands, see the Microsoft Learn quickstart for creating a Linux VM with Azure CLI.
Step 6: Additional Hardening Worth Doing
Once the basics are in place, a few extra measures meaningfully raise the bar. Enable Trusted Launch when you create the VM (add --enable-secure-boot true --enable-vtpm true to the create command) so Azure can attest the boot chain. Install audit logging with sudo apt install -y auditd so logins and privilege escalations are recorded. Reduce the SSH attack surface further by adding MaxAuthTries 3 and LoginGraceTime 30 to your sshd configuration, and consider moving SSH to a non-standard port in addition to — never instead of — the NSG restriction. Finally, tag the VM at creation time (--tags env=prod owner=sysadmin) so cost reports, backup coverage checks and RBAC audits can find it later. A small setup investment here saves hours during the first incident review.
Troubleshooting
- SSH times out: the NSG rule or UFW is blocking you. Check the effective rules with
az network nsg list-effectivesemantics in the portal, and confirm your current public IP matches the allow rule. - Permission denied (publickey): your local key permissions are too open (
chmod 600 ~/.ssh/id_rsa) or you are offering the wrong key. Usessh -vto debug. - VM shows “VM agent is not ready”: wait a few minutes after provisioning, then run
az vm get-instance-viewto inspect the agent and boot diagnostics.
Conclusion
You have deployed an Azure Linux virtual machine and hardened it with key-only SSH, a restricted network security group, automatic updates, UFW and Fail2Ban. From here, sensible next steps are configuring Azure Backup for the VM, attaching monitoring alerts, and documenting the build so you can reproduce it with templates instead of manual commands.