Linux on Azure

Azure CLI Linux: The Basics, Tips, and a Quick Tutorial

December 1, 2020

Topics: Azure NetApp Files Elementary 8 minute read

What is Azure Command Line?

Azure Command Line Interface (Azure CLI) is a convenient way to manage, update, and create resources on the Microsoft Azure cloud platform. Azure CLI can be used for any Azure service, and it is designed with automation in mind.

The Azure CLI allows you to easily work in all popular programming languages and environments. In this article we focus on using the Azure CLI in an Azure Linux environment.

  • It can be installed in Windows, MacOS and Linux environments.
  • You can run it from Docker and Azure Cloud Shell.

It also provides powerful features you can use to automate resources on Azure:

  • Support long-term operation.
  • Supports one or multiple Azure subscriptions for each command
  • Can return query results in an output format of your choice
  • Can work in a multicloud environment
  • Offers configurable logging, data collection, and default arguments
  • Can be deployed using ResourceManager templates

In this article, you will learn:

Install Azure CLI with apt

If you use the distribution provided by apt (such as Debian or Ubuntu), or use the x86_64 package in the Azure CLI—note the package was tested and is compatible with the following products:

  • Ubuntu—trusty, bionic, xenial, eoan, focal
  • Debian—jessie, buster, stretch

There are two ways to install Azure CLI using apt-compatible deployment. One is to run a general installation command script, and the other is a step-by-step process that can be run directly. 

Install with One Command

Azure provides and maintains scripts that can run all installation commands using one line of code. You can run these one line scripts using curl. An alternative is downloading the script into a file, where you can check it before actually running. Here is a one-line script for installing the Azure CLI:

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

Manual Install Instructions

If you are not interested in running the script as root, or the one-line script does not work, follow the steps below to install Azure CLI. 

  1. Get the software packages required for the installation process.
sudo apt-get update
sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg
  1. To get access to the CLI, you’ll need to download a security key provided by Microsoft
curl -sL https://packages.microsoft.com/keys/microsoft.asc |
   gpg --dearmor |
   sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null
  1. Add the Azure CLI repo.
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
  1. Update the repository data and install the package.
sudo apt-get update
sudo apt-get install azure-cli
  1. Use the az command to start the Azure CLI. Use the azlogin command to log in.

3 Tips for Using Azure CLI Effectively

The following three tips will help you make the most out of your use of Azure CLI.

Output Formatting

  • The default output format of the Azure CLI is JSON, and is intended to provide as much information as possible. If other formats are needed, use the --output option to overwrite a single command call, or use az configure to update global output configuration.
  • The JSON format contains double quotes and is usually not suitable for scripting.
  • The table command is particularly useful for getting summarized information from the CLI. If you need to see different fields in the table, use --query to specify your own format.
  • tsv can help you create brief outputs and is useful for scripts, because it strips double quotes. Use the --query parameter to specify the required format for the TSV files. 

Generic Update Arguments

Most CLI update commands have three common parameters: --add, --set and --remove. Although these options are powerful, they are generally not as useful as the strongly typed options commonly used in update commands. The CLI provides strongly typed arguments for the most common situations for ease of use. However, if the property you need is not listed, you can rely on generic update arguments, to avoid waiting for a new release of the CLI.

The generic update syntax is not the easiest to use and requires a little patience. You must make sure that the Generic Update Arguments group exists. If not, you need to create a support ticket to make this group available as part of the update operation.

Once you have the group, try the following:

  • The show command displays the path to be used in the shared arguments
  • The update operation’s dot syntax can be useful to get lists of attributes or available commands
  • Run az vm update -h to see working examples.

Managing Concurrent Users

Azure CLI is often used on machines used to build software packages using CI/CD systems such as Jenkins. These types of machines commonly run several builds at the same time, with different users running different builds. This creates a need to log into the CLI several times with different users.

To avoid an issue where the same login token will be shared between all the builds, on each build system, set the AZURE_CONFIG_DIR environment variable to a directory that contains a correct login token for that build system.

 

Quick Tutorial #1: Create a Linux Virtual Machine with the Azure CLI

Next, see how to deploy a Linux virtual machine (VM) in Azure using the Azure Command Line Interface (CLI).

Related content: read our guide to Azure Linux VMs

Launch Azure Cloud Shell

Azure Cloud Shell is a free interactive shell that can be used to complete the steps in this tutorial. All popular Azure tools are pre-installed and configured to work with your account. You can open CloudShell from a browser tab using this URL: https://shell.azure.com/bash

Create a Resource Group

Use the az group create command to create a resource group. Azure resource groups are logical containers in which Azure resources are organized and managed. See an example below:

az group create --name myResourceGroup --location eastus

Create a Virtual Machine

Use the azvmcreate command to create a virtual machine. Below is an example showing how to create a VM named myVM and adds a user account to it.

The --generate-ssh-keys option is used to automatically generate SSH keys and save them in the standard key location (~/.ssh). The --ssh-key-value parameter lets you specify which set of keys to use in the vm create operation. It can take a few minutes for the VM to be launched.

az vm create --resource-group myResourceGroup --name myVM \
--image UbuntuLTS --admin-username azureuser --generate-ssh-keys

Quick Tutorial #2: Create an NFS volume with Azure NetApp Files

Azure NetApp Files is a cloud service provided by Azure and NetApp, which lets you manage file shares in Linux with enterprise-grade performance and built-in high availability.

You must first register for Azure NetApp Files and create a NetApp account - see instructions here.

Set Up a Capacity Pool for Your File Share

Use this command to create a capacity pool, which you can later use to create enterprise-grade NFS volumes and mount them to Linux VMs in Azure, or to Linux machines running on premises.

Under service-level, you can select Standard, Premium, or Ultra to define the performance and availability you need from your file shares (learn about service levels).

az netappfiles pool create \
   --resource-group {your-resource-group} \
   --location {required-location} \  
   --account-name {your-account} \
   --pool-name "mypool1" --size {size-in-TB} \
   --service-level "Premium"

Create an NFS Volume

First, create a virtual network. We will not create a subnet at this stage, in the next step we’ll create a delegated subnet.

az network vnet create --resource-group {your-resource-group} \
   --name "myvnet1" --location {required-location} \
   --address-prefix "10.7.0.0/16"

Now let’s create the delegated subnet, this will allow the NFS volume to run privately and securely.

az network vnet subnet create --resource-group {your-resource-group} \
   --vnet-name "myvnet1" --name "mysubnet1" \
   --address-prefixes "10.7.0.0/24" \
   --delegations "Microsoft.NetApp/volumes"

Finally, we can create the volume itself. Note that the file path you provide must be unique to the Azure subscription and region, you cannot create another NFS share on the same path.

Under protocol-types, select the NFS version your applications support - Azure NetApp Files support NFSv3 and NFSv4.1.

az netappfiles volume create --resource-group {your-resource-group} \
   --location {required-location} --account-name {your-account} \
   --pool-name "mypool1" --name "myvol1" \
   --service-level $SERVICE_LEVEL \
   --vnet "myvnet1" \
   --subnet "mysubnet1" \
   --usage-threshold {size-in-TB} \
   --file-path {unique-file-path} \
   --protocol-types "NFSv3"

Linux on Azure with Azure NetApp Files

Azure NetApp Files is a Microsoft Azure file storage service built on NetApp technology, giving you the file capabilities in Azure even your core business applications require.

Get enterprise-grade data management and storage to Azure so you can manage your workloads and applications with ease, and move all of your file-based applications to the cloud.

Azure NetApp Files solves availability and performance challenges for enterprises that want to move mission-critical applications to the cloud, including workloads like HPC, SAP, Linux, Oracle and SQL Server workloads, Windows Virtual Desktop, and more.

In particular, Azure NetApp Files allows you to migrate your Linux applications to Azure–even your business-critical workloads–with extreme file throughput with sub-millisecond response times.

Want to get started? See Azure NetApp Files for yourself with a free demo.

Cloud Data Services

-
X