Azure PowerShell (12) 通过Azure PowerShell创建SSH登录的Linux VM

时间:2022-12-08 02:11:56

  《Windows Azure Platform 系列文章目录

  本章将介绍如何使用Azure PowerShell,创建SSH登录的Linux VM

  前提要求:

  1.安装Azure PowerShell

  2.准备好Linux SSH Key:

  Windows Azure Virtual Machine (25) 使用SSH登录Azure Linux虚拟机

  具体的PowerShell命令如下:

#在弹出的界面中,输入Azure China用户名和密码
Add-AzureAccount -Environment AzureChinaCloud #选择当前订阅名称
Select-AzureSubscription '[YourSubscriptionName]' -Current #创建高级存储
New-AzureStorageAccount -StorageAccountName "[YourStorageAccountName]" -Location "China East" -Type "Premium_LRS" #云服务名称
$AzureCloud='[YourDNSName]' #Upload the cert to the cloud service
$Cert = Add-AzureCertificate -ServiceName $AzureCloud -CertToDeploy "C:\myCert.pem" #Get the thumbprint from the uploaded cert
$ThumbPrint = (Get-AzureCertificate -ServiceName $AzureCloud).Thumbprint #The local user to create on the linux vm
$adminName = "azureuser" #Create the ssh key to be placed in the vm during deployment (inside the specified user's home dir)
$sshkey = New-AzureSSHKey -PublicKey -Fingerprint $ThumbPrint -Path "/home/$adminName/.ssh/authorized_keys" =================================================
#创建VM $storageAccount = "[YourStorageAccountName]" $vmName ="[YourVMName]"
$location = "China East"
$imageList = Get-AzureVMImage | where {$_.ImageName -like "*CentOS-66*"}
$imageName=$imageList[$imageList.Length -1 ].ImageName
$vmSize ="Standard_DS13"
$vnetName = '[YourVNetName]'
$IPAddress = '[YourPrivateIP]'
$SubnetNames = '[VNetSubnet]' $OSDiskPath = "https://" + $storageAccount + ".blob.core.chinacloudapi.cn/vhds/" + $vmName + "_OS_P10.vhd" $vm = New-AzureVMConfig -Name $vmName -ImageName $imageName -InstanceSize $vmSize -MediaLocation $OSDiskPath -AvailabilitySetName 'DBAvbSet' Add-AzureProvisioningConfig -Linux -VM $vm -LinuxUser $adminName -NoSSHPassword -SSHPublicKeys $sshkey -NoSSHEndpoint Set-AzureSubnet -SubnetNames $SubnetNames -VM $vm | Set-AzureStaticVNetIP -IPAddress $IPAddress New-AzureVM -ServiceName $AzureCloud -VM $VM -VNetName $vnetName -Location $location ##创建SSD磁盘
$vm = Get-AzureVM -ServiceName $AzureCloud -Name $vmName
$LunNo = 1
$path = "http://" + $storageAccount + ".blob.core.chinacloudapi.cn/vhds/" + "myDataDisk_" + $LunNo + "_P20.vhd"
$label = "Disk " + $LunNo
Add-AzureDataDisk -CreateNew -MediaLocation $path -DiskSizeInGB 500 -DiskLabel $label -LUN $LunNo -HostCaching None -VM $vm | Update-AzureVm