Install software with a VM extension
In this task you'll add an extension to a VM.
VM extensions are small helper programs that Azure installs inside a running VM for you. They can run a setup script, install monitoring or security agents, or reset a forgotten password, without you signing in to the machine.
The Custom Script Extension is the one you'll use most: it downloads and runs a script once, which is handy for installing software on a new VM.
Example. Larkfield Group's new test VMs all need the same web server. Instead of signing in to each one, the team adds the Custom Script Extension with a one-line install command when each VM is created.
Before you start
You need: Virtual Machine Contributor or Contributor. Time: 10 minutes.
Where to look
- Virtual machines
- vm-practice
- Settings
- Extensions + applications
Steps
- Start the VM from the Create a small Linux VM task if it's stopped, and open it.
- Select Extensions + applications under Settings. The Extensions tab lists what's already installed: often nothing on a new VM.
- Select + Add and browse the list. Find Custom Script For Linux and select Next.Other common ones are the Azure Monitor agent and the password reset extension (VMAccess).
- In Command, type
sudo apt-get update && sudo apt-get install -y nginx, then select Review + create and Create.The VM needs outbound internet access to download the packages. - When the extension shows Provisioning succeeded, open Run command, choose RunShellScript and run
systemctl status nginx --no-pagerto check the web server is running.Run command is another way to run a script inside the VM from the portal, without signing in. - Back in Extensions + applications, select the extension and look at its Status details. That's where errors from your script appear.
Commands
# Run an install command with the Custom Script Extension
az vm extension set --resource-group rg-portal-practice --vm-name vm-practice \
--name CustomScript --publisher Microsoft.Azure.Extensions \
--settings '{"commandToExecute":"sudo apt-get update && sudo apt-get install -y nginx"}'
# List a VM's extensions
az vm extension list --resource-group rg-portal-practice --vm-name vm-practice --output table
Watch out for
- Cost
- Extensions are free; the VM costs what it always does. Stop or delete the VM when you've finished.
- Security
- Extensions run as root (or SYSTEM on Windows), so only install ones you trust, and never put passwords inside a script command. Who can add extensions can control the VM.
- Common mistakes
- Expecting the script to run again on every restart: Custom Script runs once. Adding a second Custom Script Extension: a VM can only have one, so update the existing one instead.
Clean up
- vm-practice
- Extensions + applications
Select the extension and Uninstall, or delete the resource group at the end of the virtual machines section.
Words to know
- Extension
- A helper program Azure installs and runs inside a VM.
- Custom Script Extension
- Runs a script you give it, once, inside the VM.
- VM agent
- The small service inside every Azure VM that installs and runs extensions.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On