How to Create a Resource Group & Virtual Machine Using the Azure CLI
Tags: azure, devops, aws, beginners
Published: 2023-09-02 07:40:01

**What is Azure Command Line Interface?**
Azure Command-Line Interface (Azure CLI) is a set of commands used to manage and interact with resources in Microsoft Azure. It provides a command-line interface to manage various Azure services, including Virtual Machines, Storage, Networking, and more. The Azure CLI allows you to automate tasks, create and manage resources, deploy applications, and perform various administrative operations within your Azure environment, all through the command line.

In Microsoft Azure, this CLI tool enables users to interact with their Azure account via a CLI interface, this tool is called the Azure CLI. For Azure-specific syntax, you need to install the Azure CLI and log into your Azure account via the Azure CLI.

**STEP 1: Installing the Azure CLI**
Go to (https://learn.microsoft.com/en-us/cli/azure/install-azure-cli.) Select your operating system accordingly and follow the steps, click on Install and update then select the link that match your system windows Bit 32/64.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w30qmrxe5nzeqrrapj1.png)

Kindly note that installation is only required once. If you already have it installed, you can skip this process and continue to Step 2

**Step 2: Log into your Azure account via the CLI**
Open any CLI application of your choice. I am using a Windows system, and I will make use of the traditional CMD windows that came with Windows. On your Windows search bar type CDM.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3eg0l12tu0v5cyos0rif.png)

To confirm if Azure CLI is installed. Type <u>az version</u> on your CMD here our Az version is "2.50.0",

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ld4ye3b35k8vs13tb6ia.png)

Log in to your Azure account by typing <u>az login</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4qxyyqi79v9sr4182ux.png)

Your default web browser will open requesting you to select Azure account to log into. Select account with an active subscription.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdeo6dxzg96deakjxbxm.png)

Once you click, you will receive a prompt to enter password, please do.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6megs84ip42txf1ldegc.png)

You will receive the confirmation below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6a4i04ba1o8pyoxnq4tk.png)

At this stage return to your CMD and tap enter

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7y6w7z9jj4vsv7wvgyh.png)

Step 3: Create your resource group.

Create a new resource group where your VM will be deployed with the following command.

az group create --name <ResourceGroup Name> -- location <input the Azure location you want your resource to be> and press enter.

For this example, we will have our ResourceGroup name - demoRG and Location - UKSouth

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jx40bmsdqlys1mar1pak.png)

Resource group has been successfully created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlci36fehbfl8gn0qhyl.png)

**Step 4: Create a Linux VM in the demoRG resource group.**

To create the VM via the CLI use the syntax-

az vm create --resource-group MyResourceGroup --name MyVM --image UbuntuLTS --admin-username azureuser --admin-password MyPassword123  --size Standard_DS2_v2

**Note**
-- resource-group: Here we add the Resource group we created earlier on {demoRG}

-- name: Provide a name for your VM. I will call mine demolinux1

-- image: Choose the operating system you want to use I will be using the UbuntuLTS.

--admin-username: This is the admin username for the VM- I will call mine azureuser

--admin-password: Set the password for the VM, you have to remember this to be able to log into the VM. Remember your password must meet the requirements for VM password as stipulated by Azure.

--size Standard_DS2_v2

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncsj91ej21yptj6k7zr1.png)

demolinux1 successfully created. Verify on your Azure portal. virtual machine has been successfully created, please check your Azure portal to confirm this.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2hoow5ufkg3ze67n1hc.png)

We need to log into the machine.
Please note that our VM username is azureuser and password, we can use the command: ssh username@ipaddress

Run the command
ssh azureuser@20.254.51.82

Login successful

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0lmof3thyu4pqwhq3m0.png)

demolinux1 successfully created. Verify on your Azure portal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uivyvhzi2cj7ki0l8c2d.png)

INSTALL AZURE CLI ON AZURE LINUX VIRTUAL MACHINE [newly deployed].
Once the Azure Linux Virtual Machine has been connected successfully, Then, we can now install the Azure CLI on the Linux Virtual Machine [newly deployed] for more management activities.

1). Input the command:

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

A. Run the Azure CLI command to install.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6187u0isijscn0rxvx0.png)
Azure CLI successfully installed.

MANAGE AZURE LINUX VIRTUAL MACHINE (demolinux1) USING AZURE CLI.

Azure CLI has been installed successfully on the Azure Linux Virtual Machine, Then, we can now manage the VM via running certain Key commands on the VM.

To get list of usable commands, double click on you Virtual Machine from Azure portal, then at the top right conner click CLI/PS to pick list of commands should in case you are new to this environment.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkir5e9r5ou5isidsabt.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2276jvgtx3tagpxa71d.png)

1). STOP VM
Input the command: az vm stop --resource-group demoRG --name demolinux1 --subscription 748cf767-a92e-4581-a901-b786a393544a

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66mdxtw31b174k02efr3.png)

2). START VM
Input the command:az vm start --resource-group demoRG --name demolinux1 --subscription 748cf767-a92e-4581-a901-b786a393544a

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y576xl6riwz7z09xjq0s.png)

3). DEALLOCATE VM
Input the command: az vm deallocate --resource-group demoRG --name demolinux1 --subscription 748cf767-a92e-4581-a901-b786a393544a

Deallocated

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hwjlfvl8lsjv3unvzyq.png)

demolinux1 Deallocation confirmation from Azure portal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07lff8vnwiv6ozvv83f2.png)

4). RESTART VM.
Input the command: az vm restart --resource-group demoRG --name demolinux1 --subscription 748cf767-a92e-4581-a901-b786a393544a

5). VM IP ADDRESS LIST
Input the command: az vm list-ip-addresses [--ids] [--name] [--resource-group] [--subscription]

_Thank you for reading my article till the end. I hope you learned something special today. If you enjoyed this article, then please share to your friends and if you have suggestions or thoughts to share with me then please write in the comment box._