Create Azure virtual machine in one minute using Azure CLI

 In this blog post, you will learn how to create virtual machine on Azure. There are many ways to create a VM on Azure. You can create VM through Azure CLI or directly on Azure Portal.

## Azure CLI

1. Login your azure account and select azure subscription
`az login`
2. Create a new resource group. If you have existing resource group you can use that as well later. To create new Azure resource group you need to pass in a name and location
`az group create --name rg1 --location eastus`
3. Create a new Virtual Machine you need to pass in the name, image,admin-username, admin-password and authentication-type 
```
az vm create \
--resource-group rg1 \
--name vm-01 \
--image UbuntuLTS \
--admin-username defaultuser \
--admin-password "Y0urP@ssw0rd!" \
--authentication-type password

```
4.  Verify if the VM resources has been created. You should see Virtual machine named vm-01
```
az resource list --resource-group rg1
```
5.Enable ssh port(22) and RDP port (3389)
```
az vm open-port --port 22 --resource-group rg1 --name vm-01
az vm open-port --port 3389 --resource-group rg1 --name vm-01

```
6.Now, we can access our Virtual Machine and let's try to remote it but first, we need to get  the public IP Address of the VM. 
```
az vm list-ip-addresses \
--name vm-01 --resource-group rg1 \
--query "[].virtualMachine.network.publicIpAddresses[0].ipAddress" \
--output tsv

```
then try to SSH
```
ssh username@public-ip-address
ssh defaultuser@<Azure VM-public ip>
 
```

7.Optionally you can setup a Remote Desktop Protocol server in your VM. We can use xrdp as RDP server on linux and install xfce4 as a lightweight desktop environment.
```
sudo apt update
sudo apt install xfce4 xfce4-session xrdp
echo xfce4-session > ~/.xsession
sudo systemctl enable xrdp
systemctl start xrdp
systemctl status xrdp

```

Now you can access your VM using any Remote Desktop Client like Windows App on mac, Remote Desktop on Windows or Remmina on Linux.

Comments