Home Azure Cloud Getting Started with Azure CLI on Linux (Ubuntu) and Windows (WSL2 with...

Getting Started with Azure CLI on Linux (Ubuntu) and Windows (WSL2 with Ubuntu)

359
0

The Azure Command-Line Interface (CLI) is a versatile tool that allows you to manage Azure resources directly from your terminal. Whether you’re on native Linux (Ubuntu) or running Ubuntu through Windows Subsystem for Linux (WSL2), the Azure CLI provides a consistent, powerful way to interact with Azure.

🧩 Prerequisites

Before installing Azure CLI, make sure you have a Linux environment and that it’s up to date.

If you’re on Windows and don’t already have a Linux VM or environment, you can run Ubuntu using WSL2 (Windows Subsystem for Linux). We can do this by following the setup note: Getting Started with WSL2: Run Linux Inside Windows.

💡 If you have already setup WSL2 with Ubuntu:

If you’ve already set up WSL2, run the following commands in PowerShell (as Admin) to ensure your WSL2 kernel and subsystem are current:

wsl --update
wsl --shutdown

This updates the WSL2 base environment. After shutdown, reopen Wsl (Ubuntu) to continue.

💡 WSL2 and Ubuntu (native) Users:
Next, inside your Ubuntu terminal (either native or within WSL2), update all system packages to ensure dependencies, repositories, and certificates are up to date:

sudo apt update && sudo apt upgrade -y

Keeping both your WSL kernel and Ubuntu userspace updated ensures maximum compatibility and prevents common installation or SSL-related issues.


1️⃣ Install Azure CLI

Run the following commands in your terminal:

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

This command automatically downloads and installs the latest version of Azure CLI using Microsoft’s official repository.

To verify installation, run:

az --version

You should see version details confirming successful installation.

💡 Tip: If you prefer manual steps, refer to Microsoft’s official Azure CLI on Linux guide.

⚙️ Note for Windows (WSL2): This installs Azure CLI inside your WSL Ubuntu environment (not your Windows host). You can still manage Azure resources seamlessly from there.


2️⃣ Log In Using Device Code Flow

To authenticate your CLI session, run:

az login --use-device-code

You’ll see a message like:

To sign in, use a web browser to open the page https://microsoft.com/devicelogin
and enter the code ABCDEFG123 to authenticate.

Steps:

  1. Open the link in any browser (on your computer or even your phone).
  2. Enter the displayed code.
  3. Sign in with your Azure account.
  4. Once authenticated, CLI fetches and lists your subscriptions.

Even if you have only one tenant and subscription, Azure CLI now prompts you to confirm the selection:

Retrieving tenants and subscriptions for the selection...

[Tenant and subscription selection]

No     Subscription name            Subscription ID                       Tenant
-----  ---------------------------  ------------------------------------  -----------------
[1] *  Microsoft Azure Sponsorship  f7eb6b1b-8c65-45b8-9d5d-db772e2aa27b  Default Directory

The default is marked with an *; the default tenant is 'Default Directory' and subscription is 'Microsoft Azure Sponsorship' (f7eb6b1b-8c65-45b8-9d5d-db772e2aa27b).
Select a subscription and tenant (Type a number or Enter for no changes): 1

Press 1 (or just Enter) to continue.
Azure CLI will then log in and set the selected subscription as your default context.


⚠️ (Optional) Troubleshooting: “No Subscriptions Found”

If you see this error after login:

No subscriptions found for <USER_EMAIL>.

It often means your account belongs to multiple tenants or the cached token expired.

Fix:

  1. Get your Tenant ID:
    • Go to Azure Portal → Microsoft Entra ID (Azure Active Directory)Overview
    • Copy your Tenant ID (Directory ID)
  2. Alternatively, list tenants using CLI: az account tenant list --output table
  3. Log in again with the correct tenant: az login --use-device-code --tenant <TENANT_ID>

After login, you’ll see your subscriptions and may be prompted to choose the default one.


3️⃣ Verify Your Login and Subscription

List all your subscriptions:

az account list --output table

Show the currently active subscription:

az account show --output table

4️⃣ (Optional) Set the Active Subscription 🗂️

If you have multiple subscriptions, set the one you want to use:

az account set --subscription "My Subscription ID or Name"

This ensures all further commands apply to that specific subscription.


5️⃣ List Resource Groups

To see all resource groups in your subscription:

az group list --output table

This displays a clear table view of all existing resource groups.


6️⃣ (Optional) Create Your First Resource Group 🎯

A resource group is a logical container for Azure resources.
Create one using:

az group create --name myResourceGroup --location eastus

If successful, you’ll see JSON output confirming the creation.


7️⃣ (Optional) Update or Remove Azure CLI

To update Azure CLI to the latest version:

sudo apt update && sudo apt upgrade azure-cli -y

To remove it:

sudo apt remove azure-cli -y

🎉 Conclusion

You’ve just:
✅ Installed Azure CLI on Ubuntu and WSL2
✅ Logged in securely
✅ Verified your subscription
✅ Created your first resource group

From here, you can start creating virtual machines, deploying web apps, or managing Azure resources entirely from your terminal — no need to open the Azure Portal.

💡 Once you get familiar with the CLI, you’ll realize it’s faster, scriptable, and perfect for automation.

Previous articleGetting Started with Azure CLI on Your Local Machine
Next articleDifferent Ways to Authenticate Terraform with Azure
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here