Use Azure CLI to Change VM Networking

2 hours
  • 2 Learning Objectives

About this Hands-on Lab

In this lab we will walkthrough how to use the Azure CLI (`az cli`) to reconfigure the networking settings of a VM that already exists.

**Our primary focus is on Azure CLI**, with VM Networking being the example used to help demonstrate concepts. As such, we expect you the have familiarity with VM Networking.

This intermediate level hands-on lab covers several concepts, including the following:
* Using Bash variables with Azure CLI
* Outputting and querying information with Azure CLI

This hands-on lab uses the Azure Cloud Shell. You can follow along at work or at home using your web browser and don’t need to install any specific software.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create New Network Resources

Create new network resources using Azure CLI and the following settings:

  • Public IP Address
    Resource Group: Existing resource group
    Name: pubip1
    SKU: Basic
    Allocation method: Dynamic

  • Network Interface
    Resource Group: Existing resource group
    Name: nic2
    IP Address: 10.1.0.10
    Assigned VNet: vnet1
    Assigned Subnet: subnet1

Use the below instructions as a guide:

Note: The resource group has already been created for you.

  1. Login to the Azure portal using the credentials provided.
  2. Navigate to the Cloud Shell, using the >_ icon in the toolbar.
  3. Ensure you are using Bash for the Azure CLI.
  4. Identify the existing resource group: az group list
  5. Create variables (see below): for example nic2Name="nic2"
  6. Create the public IP address: az network public-ip create
  7. Create a NIC: az network nic create

Note: az login is not required when using Cloud Shell within the Azure Portal. The session authenticates as the user you are logged in as. You will need to use this command if connecting to Azure using Azure CLI on your own machine.

The variables used in this lesson are as follows:
rg="paste your resource group name here"
pubName="pubip1"
pubSku="basic"
pubAlloc="dynamic"
nic2Name="nic2"
nic2Address="10.1.0.10"
vnetName="vnet1"
subnetName="subnet1"
vm="vm1"
loc="westus"

Update the Existing VM Network Settings

Update the virtual machine to use the new network resources using Azure CLI and the following settings:

  • VM1
    Add new NIC: NIC2, created earlier
    Public IP address: pubip1, attached to NIC2

Use the below instructions as a guide:

  1. Login to the Azure portal using the credentials provided.
  2. Navigate to the Cloud Shell, using the >_ icon in the toolbar.
  3. Ensure you are using Bash for the Azure CLI.
  4. Identify the id of the new NIC: az network nic show -n $nic2Name -g $rg
  5. Update the current VM NIC to not be primary: az vm update -n $vm -g $rg --set networkProfile.networkInterfaces[0].primary=false
  6. Add the new NIC to the VM: az vm update -n $vm -g $rg --add networkProfile.networkInterfaces primary=true id=$nicId
  7. Remove the existing NIC: az vm nic remove -g $rg --vm-name $vm --nics vm1-nic1

Note: az login is not required when using Cloud Shell within the Azure Portal. The session authenticates as the user you are logged in as. You will need to use this command if connecting to Azure using Azure CLI on your own machine.

The following Azure CLI arguments are provided to help you:

  • Output information about a resource using show command: e.g. az network nic show -n $nic2Name -g $rg
  • Change the Azure CLI output format: e.g. to table format az network nic show -n $nic2Name -g $rg -o table
  • Assign command output to a variable: e.g. NIC id nicId=$(az network nic show -n $nic2Name -g $rg --query id -o tsv)

Additional Resources

PLEASE NOTE: The region used within this lab has been changed from South Central US to West US. Please ensure you use West US throughout this lab.

Please go ahead and log in to the live Azure hands-on lab environment using the credentials provided for you above.

The following information will help you throughout this hands-on lab:

  • Make sure you are using West US as your region throughout the lab.

  • This hands-on lab uses the Cloud Shell within the Azure Portal, but you may also use Azure CLI if you have it installed on your computer.

  • Once you are familiar with the process, consider completing the lab using the steps first and using the videos to verify your solution.

  • You can expand the objectives to see more information on the tasks this lab undertakes.

  • If you'd like to view the diagram, you can access this using the Diagram button on the upper-right-hand side of this hands-on lab.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?