Search code examples
azureazure-virtual-network

How to add an exisiting Linux virtual machine to virtual network in Azure


I have configured a CMS solution in my Linux Virtual Machine on Azure and want to add this VM with my Virtual Netork. I am unable to add my existing Linux Virual machine to Virtual Network.

Any help is greatly appreciated.


Solution

  • As your Linux VM was created first and Virtual Network (VNet) was created after, your Linux VM may not be able to see the Virtual Network.(This is a known issues with Linux VM)

    You can follow given steps to achieve your objective:

    1. Delete the current VM (this will still keep the VHD stored in Azure Storage)
    2. Now go to Virtual Machines > Disks, and find the OS disk you want to use and select Delete Disk. This does not delete the VHD, only unregisters it as a disk
    3. Now create an OS image from your Saved VHD, which was use for previous VM
    4. Now create a VM from OS image (#3)

    This way you will have your VM ready and now to add it to VNet.

    Now because you are using Linux VM to connect with VNet, there is one problem. You can not do it with your Linux VM as Windows Azure Manager Portal does not support this feature so you would need to use PowerShell command (on Windows) or Cross-Platform tools (on Mac and Linux) to achieve your objective.

    Following are the command you will use to create a new VM using an OS Image and Virtual Network:

    $vm = New-AzureVMConfig -Name $vmname -InstanceSize ExtraSmall -ImageName $image | Add-AzureProvisioningConfig -Linux –LinuxUser $linuxUser -Password $adminPassword | Set-AzureSubnet -SubnetNames $subnet

    New-AzureVM -ServiceName $cloudSvcName -AffinityGroup $affinitygroup -VNetName $vnetname -VMs $vm