Using the Azure Command Line Interface with CIQ’s Rocky Linux

A previous article covered how to quickly launch a Rocky Linux instance using the web interface on the Azure portal. In this article, I summarize how to do a similar thing using the Azure CLI tools. A follow-on post will spin up virtual machines and immediately customize them with cloud-init, an interesting and rich topic in its own right.

Hello, Command Line

We saw in the previous article how to launch a virtual machine running Rocky with a few browser clicks. Of course, the command line works as well. First, you must install the az command via the Azure CLI and log into your Azure account using az login. Once there, you can create a Rocky virtual machine with a few commands from the image’s Azure designation in the form of Publisher:Offer:Sku:Version. In the stanza below, we create an environment variable AZURE_ROCKY85 which you can reuse in your own code.

Some asides: we will use the somewhat confusingly named az flag generate-ssh-keys for convenience in the examples. This switch doesn’t actually generate any new keys at all. It adds your default ssh public key on your local machine, usually ~/.ssh/, to the authorized_keys file of the target virtual machine user. In my examples, I use rocky which is a typical convention. You may be prompted to accept host keys when you ssh into your vm after its creation, depending on your ssh configuration settings for StrictHostKeyChecking and UserKnownHostsFile. I don’t clutter the examples below with that prompt.

## By convention, I’ll describe each (small) bash stanza’s intent first.
## Names starting with `your_` are meant to be substituted, e.g `your_group`

## Are you logged into your Azure account?

$ az account show > /dev/null && echo "logged in"
Logged in

## Use the export command to create a variable AZURE_ROCKY85 and set its value 
## to the Rocky image Azure id.

$ export \ AZURE_ROCKY85=ctrliqinc1648673227698:rocky-8-5-x86_64-0:rocky-8-5-x86_64-paid:8.5.20220406

## Create a new Rocky 8.5 virtual machine using the ${AZURE_ROCKY85} image.
## Coordinate your local ssh keys with the Azure vm.

$ az vm create  --resource-group your_group \
                --name your_cli --image ${AZURE_ROCKY85} \
                --admin-username rocky --generate-ssh-keys

  "fqdns": "",
  "id": "/subscriptions/your_subscription/resourceGroups/your_group/providers/Microsoft.Compute/virtualMachines/your_cli",
  "location": "eastus",
  "macAddress": "00-0D-3A-1D-37-25",
  "powerState": "VM running",
  "privateIpAddress": "",
  "publicIpAddress": "",
  "resourceGroup": "your_group",
  "zones": ""

## Ssh into the new virtual machine, accepting the host key if prompted. 
## Note the announcements. We’ll remove them in a future post.

$ ssh [email protected] 
Activate the web console with: systemctl enable --now cockpit.socket

[[email protected]_cli ~]$ 

## You’re at the new virtual machine `your_cli`. 
## Look around; take in the view!

[[email protected]_cli ~]$ uname -a
Linux your_cli 4.18.0-348.20.1.el8_5.x86_64 #1 SMP Thu Mar 10 20:59:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

## Rocky? Yup.

[[email protected]_cli ~]$ cat /etc/os-release 
NAME="Rocky Linux"
VERSION="8.5 (Green Obsidian)"
ID_LIKE="rhel centos fedora"
PRETTY_NAME="Rocky Linux 8.5 (Green Obsidian)"
[[email protected] ~]$ 

## New machine? Yup.

[[email protected]_cli ~]$ uptime
 16:39:34 up 1 min,  1 user,  load average: 0.00, 0.00, 0.00

## Passwordless sudoer? Yup.

[[email protected]_cli ~]$ sudo -i
[[email protected]_cli ~]# whoami

You can – and should – update your vm using dnf upgrade.
At this point, we now have two different ways to create a virtual machine from the same Rocky Linux 8.5 image. In the portal, we can create a virtual machine quickly without knowing a lot of details as we described in the previous article. But that approach is manual; you answer the same questions and click the same buttons for every virtual machine you create. In contrast, you can create a similar virtual machine at the command line with a single command (az create vm) but that approach required some “one-time” preparation to issue the appropriate command and required some Azure details, like Rocky’s Azure identifier, to show through. We took a simple “first step” to separate out some of the details, namely the environment variable AZURE_ROCKY85. We’ll be taking more. That comes in the next article.

Up next: Using Cloud-init with CIQ’s Rocky Linux

And if you haven’t read the first article, you can find it here: CIQ’s Rocky Linux Images Now Available on Azure

About the Author:

Mike Carifio

Mike Carifio supports Rocky Linux for CIQ. He started programming on a DEC PDP 11/45, before Unix was even a thing, and never looked back. He’s filled every high tech role imaginable, but had never written a blog post for CIQ. Mission accomplished, twice over.

You might be interested in …