Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Use Azure CLI to Change VM Networking

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.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 2h 0m
Published
Clock icon Jun 04, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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"

  2. Challenge

    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)

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans