Azure is Microsoft's cloud platform that provides scalable, agile, and highly available IT infrastructure and services to both individuals and enterprises.
This guide will show you how to install and run Azure CLI commands on Ubuntu. Azure CLI is one of the tools that the Microsoft Azure team provides for managing your cloud infrastructure through the command line.
Why Use Azure CLI?
The Azure Command Line Interface (CLI) is a cross-platform command-line tool used for creating and managing Azure resources. The main purpose of this tool is to allow you to easily automate tasks by running interactive commands in your terminal or using scripts.
You can install Azure CLI on macOS, Windows, and Linux. You can also use Azure CLI within a browser via the Azure Cloud Shell or from inside a docker container.
Some of the advantages of Azure CLI include:
- Easily automate the provisioning and decommission of resources.
- Helps you with your DevOps operations.
- Ease the management of your cloud infrastructure.
Installing Azure CLI on Ubuntu
To make the installation easier, this guide will use the installation script maintained by the Microsoft Azure CLI team.
Note: For security, it is a good practice to inspect scripts you download from the internet before running them on your PC or server. This is to make sure that they do not contain malicious content.
Use the curl command to download the script and then pipe the output to your bash shell to initialize the installation.
sudo curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Once the installation is complete, check if Azure CLI is successfully installed by verifying the version number with the following command:
The output should be similar to the one below.
Logging In to Azure
If you do not have an Azure account, you can create a free trial account from the Azure portal.
To manage your Azure cloud infrastructure using Azure CLI, you must first log in to Azure using the following command:
The system will then redirect you to the default browser. Here, you will have to provide your account credentials to log in. Once done, you will be taken back to the terminal window.
Note: If you are using a non-graphical user interface, you will be given a token that you can use to log in on a browser from another device.
In the terminal, Azure CLI gives you a brief overview of the subscriptions tied to your Azure account in JSON format.
Azure CLI provides you with all the power and functionality that is available in the Azure portal.
Changing the Output Format
By default, Azure CLI will format your output in JSON format. This might take up a lot of your terminal space. Alternatively, you can use the table output format by using the -o table option in your commands.
For example, try to run the az login command again, but this time use the –o table option as follows:
az login -o table
Choosing a Subscription
To get a list of all the subscriptions for your Azure account, run the following command:
az account list -o table
If you have multiple subscriptions, you can change the default subscription using the following command:
az account set subscription --subscription 'your_subscription_name'
When you list your subscription again with the az account list -o table, you will have the column IsDefault set to True on the subscription that you selected.
Resource groups are a very important concept in Azure, they are the logical containers in which you group logically related resources and services to ease management.
Knowing the resources that you have is not only important for managing your infrastructure but is also vital for controlling cloud expenditure and making optimizations.
To get a list of all active resources within a subsection, you can run the following command.<br />
az resource list -o table
Getting Command-Line Help
One of the most powerful features of Azure CLI is the built-in help functionality. You can use it to learn more about commands that you can use.
To list all available Azure CLI commands, simply run the command below. The system will present you with a list of all base commands.
From this output, you can choose which command you want to learn more about. For example, to find out more about the vm (virtual machine) command:
az vm --help
It will show you commands that you can execute on a virtual machine. For example, starting and stopping a VM or listing VMs.
You can even go further by getting more help on a particular action. For example, to learn more about how to stop a VM:
az vm stop --help
Azure CLI will present you with the command options for stopping a virtual machine including example commands on how to use it.
Using Azure CLI Interactively
To help you be more efficient, Azure CLI provides you the opportunity to run commands interactively. You won't find the interactive shell installed by default but Azure CLI will automatically install it the first time you run the az interactive command.
Issue the following command to start using Azure Interactively.
Updating Azure CLI
Keeping your Azure CLI updated is not only important for security but also for getting new API functionality for interacting with your cloud infrastructure. You can easily upgrade your Azure CLI using the command below.
Note: The upgrade feature is only available for Azure CLI version 2.11.0 or later.
Configuring Azure CLI
In this guide, we have run most of the commands with the -o table flag to have a more compact output without taking up much terminal space.
If you wish to configure the table format to be your default standard output, run the following command:
Press y on your keyboard when the system asks if you want to configure Azure CLI settings. Then, enter the number displayed next to the output format of your choice. For example, to set table as the default format, enter 3.
In addition to changing the output format, you can also make changes to telemetry settings, logging, etc.
Use the command below to log out from Azure CLI.
Uninstalling Azure CLI
If you no longer need Azure CLI on Ubuntu, you can uninstall it using the following command:
sudo apt-get remove -y azure-cli
To remove Azure CLI repository information:
sudo rm /etc/apt/sources.list.d/azure-cli.list
Real-Life Applications of Azure Services
This guide has shown you how to install and set up Azure CLI for managing your cloud infrastructure. There are a lot of services that Azure provides to help drive your digital transformation and to remove the burden of managing your own IT infrastructure.
Some of the prominent services that Azure provides include artificial intelligence (AI) and machine learning (ML). You can learn more about how Volkswagen is using Azure to power its self-driving cars.