Thursday, September 5, 2024

Simplifying Azure Login on Ubuntu Using the Command Line (Bash / SSH)

Managing Azure from the command line can enhance your workflow, making it more efficient and flexible. For Ubuntu users, logging into Azure using the Azure CLI (Command-Line Interface) is both simple and powerful. This guide walks you through the steps in a straightforward manner.

Logging in to Azure

To get started, open your terminal and log in by running the following command:

az login

Authenticate Through the Browser

When you run the az login command, a browser window will either open automatically or provide a link and a code for you to use. You can then securely enter your Azure credentials. Once logged in, your terminal will show details about your Azure subscription.
 

Selecting Your Subscription

If you have multiple subscriptions tied to your Azure account, you'll be prompted to select which one you'd like to use during your session. This helps ensure you're working within the correct environment.

Verifying Your Azure Account

To ensure that you’ve logged in successfully and to see a list of your Azure subscriptions, you can run the following command:

az account list --output table

This will display your subscriptions in a clear, tabular format, confirming that you're logged in and ready to begin managing your resources on Azure.
 

Logging Out

Once you're done with your session, you can safely log out from Azure by running:

az logout

This ensures your session is closed and your credentials are no longer active on the command line.

A Simple Guide to Checking, Installing, and Verifying Azure CLI on Ubuntu

Before diving into the installation process, it's a good idea to first check if the Azure CLI is already installed on your machine. This can save you time and avoid unnecessary installations.

Step 1: Check if Azure CLI is Already Installed

To check if Azure CLI is installed, simply run the following command to check the version:

az --version

If the command returns the version of Azure CLI, you're all set! This means Azure CLI is already installed, and there's no need to reinstall it. However, if you see the message az: command not found, it means Azure CLI isn't installed yet, and you'll need to proceed with the installation.

Step 2: Update Your Package Lists

If Azure CLI isn't installed, let's start by updating your system’s package lists:

sudo apt-get update

Step 3: Install the Necessary Prerequisites

To ensure a smooth installation, we'll need to install a few supporting packages:

sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg

Step 4: Add the Microsoft GPG Key

Next, we need to download and add the Microsoft GPG public key. This key is essential for verifying the packages we'll install:

curl -sL https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null

Step 5: Add the Azure CLI Repository

Now, let’s add the official Azure CLI repository to your sources list:

AZ_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list


Step 6: Install the Azure CLI

With the repository in place, you can now install Azure CLI by running the following commands:

sudo apt-get update
sudo apt-get install azure-cli


Step 7: Verify the Installation

Finally, to ensure the installation was successful, verify the version of Azure CLI installed on your system:

az --version

This command should display the installed version of the Azure CLI along with other helpful details.