Use Azure CLI to Change VM Networking

Hands-On Lab

 

Photo of James Lee

James Lee

Training Architect

Length

01:00:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Use Azure CLI to Change VM Networking

Introduction

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.

Solution

  1. Log in to the Azure Portal using the credentials provided on the lab instructions page.

Initialize the Azure CLI

  1. Click on the Cloud Shell icon at the top of the window in the Microsoft Azure portal.

  2. Click Bash on the weclome page at the bottom of the screen.

  3. Click Show advanced settings.

  4. Under Cloud Shell region, select South Central US.

  5. Under Storage account, choose the Use existing option.

  6. Under File share, type "shell" in the box provided.

  7. Click Create storage.

  8. Click All resource in the left-hand menu of the Azure portal to verify the existence of the pre-created resources.

Create the Network Resources

  1. If you are using your own installation of Azure CLI, then login. This is not required if you are using the Cloud Shell provided through the Azure portal.

    az login
  2. List all of the groups we have access to.

    az group list
  3. Copy the "name" of the available resource group and assign to a variable rg.

    rg=COPIED_RESOURCE_GROUP
  4. Verify the variable is set correctly.

    echo ${rg}
    echo ${rg}textgoeshere
  5. Set some public variables for use during this lab.

    pubName="pubip1"
    pubSku="basic"
    pubAlloc="dynamic"
    nic2Name"nic2"
    nic2Address="10.1.0.10"
    vnetName="vnet1"
    subnetName="subnet1"
    vm="vm1"
  6. Create the public IP address we will need for later steps.

    az network public-ip create -n $pubName -g $rg --allocation-method $pubAlloc --sku $pubSku
  7. Navigate back to the Azure portal and click Refresh on the All resources page.

  8. Click pubip1 to see some the public IP settings.

  9. Navigate back to the Cloud Shell and create the network interface card.

    az network nic create -n $nic2Name -g $rg --public-ip-address $pubName --vent-name $vnetName --subnet $subnetName
    1. Nagvigate back to the All resources page in the Azure portal. Click on Refresh.
  1. Click on nic2 to verify the existence and settings of the exercise.

Update the Existing VM Network Settings

  1. Navigate back to the Cloud Shell and display the help for the az vm update command.

    az vm update -h
  2. Shut down and reallocate the virtual machine.

    az vm deallocate -n $vm -g $rg --no-wait
  3. Get the current settings for the virtual machine.

    az vm show -n $vm -g $rg
  4. Show the information associated with nic2name created earlier.

    az network nic show -n $nic2Name -g $rg
  5. Store the id for the nic2Name object in a variable.

    nicId=$(az network nic show -n $nic2Name -g $rg --query id -o tsv)
  6. Verify the nicId variable.

    echo $nicId
  7. Change the properties of NIC1 so it is no longer considered to be the primary NIC.

    az vm update -n $vm -g $rg --set networkProfile.networkInterfaces[0].primary=false
  8. Add the new NIC to the virtual machine.

    az vm update -n $vm -g $rg --add networkProfile.networkInterfaces primary=true id=$nicId
  9. Confirm both NICs are attached to the virtual machine.

    az vm nic list --vm-name $vm -g $rg -o table
  1. Navigate to the All resources page in Azure portal and click vm1.

  2. Click Networking.

  3. Click the nic2 tab to verify the public IP address.

  4. Navigate back to the Cloud Shell and remove vm1-nic1.

    az vm nic remove -g $rg --vm-name $vm --nics vm1-nic1

Conclusion

Congratulations — you've completed this hands-on lab!