Published on

Installing & Managing Azure CLI

Authors

Installing Azure CLI on Ubuntu

  1. Get packages needed for the install process
sudo apt-get update
sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg
  1. Download and install the Microsoft signing key
sudo mkdir -p /etc/apt/keyrings
curl -sLS https://packages.microsoft.com/keys/microsoft.asc |
    gpg --dearmor |
    sudo tee /etc/apt/keyrings/microsoft.gpg > /dev/null
sudo chmod go+r /etc/apt/keyrings/microsoft.gpg
  1. Add the Azure CLI software repository
AZ_REPO=$(lsb_release -cs)
echo "deb [arch=`dpkg --print-architecture` signed-by=/etc/apt/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" |
    sudo tee /etc/apt/sources.list.d/azure-cli.list
  1. Update repository information and install the azure-cli package
sudo apt-get update
sudo apt-get install azure-cli

Once you've run the above commands, you've installed Azure CLI. You can test it by running az to see the below response.

Shorter way to install Azure CLI

We can directly curl the install script for Azure and run the same through bash.

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

Once installation of Azure CLI is successful then you can test if az command is working fine.

az

     /\
    /  \    _____   _ _  ___ _
   / /\ \  |_  / | | | \'__/ _\
  / ____ \  / /| |_| | | |  __/
 /_/    \_\/___|\__,_|_|  \___|


Welcome to the cool new Azure CLI!

Use `az --version` to display the current version.

If you want to install Azure CLI on other operating systems like Windows/macOS then please go to the Official documentation.

Azure CLI Version

So now let us run az --version to display the version of Azure CLI we are running.

> az --version

azure-cli                         2.44.1

core                              2.44.1
telemetry                          1.0.8

Dependencies:
msal                              1.20.0
azure-mgmt-resource             21.1.0b1

Upgrade Azure CLI Manually

We can upgrade our Azure CLI by using the below command.

az upgrade

This command is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
You already have the latest azure-cli version: 2.45.0
Upgrade finished.You can enable auto-upgrade with 'az config set auto-upgrade.enable=yes'. More details in https://docs.microsoft.com/cli/azure/update-azure-cli#automatic-update

Upgrade Azure CLI Automatically

Keep your Azure CLI version updated. You can manually upgrade your Azure CLI by running az upgrade if you want to upgrade your CLI version automatically as an when you're running az commands, then you can update the configuration with the below command.

az config set auto-upgrade.enable=yes

Command group 'config' is experimental and under development. Reference and support levels: https://aka.ms/CLI_refstatus

But you'll need to also set configuration for prompts to be disabled otherwise it will interrupt you're scripts which make use for Azure CLI commands.

az config set auto-upgrade.prompt=no

Command group 'config' is experimental and under development. Reference and support levels: https://aka.ms/CLI_refstatus

Logging into Azure Account with CLI

az account show 

Please run 'az login' to setup account.

So, you'll need to first login to Azure Account if you want to use the Azure CLI. You can use az login if you're running it from a machine which has GUI. But if you're running the Azure CLI in a server environment, then you'll need to run with use-device-code. Let me show you how you can do that both ways.

If you don't have an Azure account yet, then you can signup for an Azure for a free account.

Log In To Azure CLI with a Standard Web Browser

All you have to do is type in the command below and your default browser will open the Azure Login page. Insert your credentials and CLI will send you a reply that looks like the following.

az login

A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.

[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "XXXXX",
    "id": "XXXXX",
    "isDefault": true,
    "managedByTenants": [],
    "name": "Visual Studio Professional",
    "state": "Enabled",
    "tenantId": "XXXXX",
    "user": {
      "name": "[email protected]",
      "type": "user"
    }
  }
]

Sign In To Azure CLI using the Device Code

If you're trying to sign in to Azure CLI in server or VM, then you'll need to run with use-device-code option which will give you a small random code which you'll need to type of browser page https://microsoft.com/devicelogin which you can open from any machine and login accordingly. After a few seconds of logging in, your Azure CLI should respond like below.

az login --use-device-code

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code HLDSYVG4H to authenticate.
The following tenants don't contain accessible subscriptions. Use 'az login --allow-no-subscriptions' to have tenant level access.
XXXXX 'Default Directory'
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "XXXXX",
    "id": "XXXXX",
    "isDefault": true,
    "managedByTenants": [],
    "name": "Visual Studio Professional",
    "state": "Enabled",
    "tenantId": "XXXXX",
    "user": {
      "name": "[email protected]",
      "type": "user"
    }
  }
]

Once you login successfully, you can check the same by typing az account show which will give all your details like tenantId, id, user, etc.

az account show

{
  "environmentName": "AzureCloud",
  "id": "XXXXX",
  "isDefault": true,
  "name": "Visual Studio Professional",
  "state": "Enabled",
  "tenantId": "XXXXX",
  "user": {
    "name": "[email protected]",
    "type": "user"
  }
}

You can type az logout to log out of your currently active Azure Account if you don't want to be logged in to the account, as the Azure CLI normally caches your credentials.

Handling Multiple Accounts in Azure CLI

With Azure CLI you can log in to multiple accounts but you can have only 1 active account. You've to login to these multiple accounts the same way as described above.[ Put self link to above section] You can view all the logged in accounts with az account list which will give you an array of accounts.

az account list

[
  {
    "cloudName": "AzureCloud",
    "id": "XXXXX",
    "isDefault": true,
    "name": "Visual Studio Professional",
    "state": "Enabled",
    "tenantId": "XXXXX",
    "user": {
      "name": "[email protected]",
      "type": "user"
    }
  }
]

If you want to set an account as active for Azure CLI you can run az account set --subscription [Name]. For my account, the command would be az account set --subscription "Visual Studio Professional"