Snabb start: skapa en virtuell Linux-serverdator med hjälp av PowerShell i Azure Stack HubQuickstart: Create a Linux server VM by using PowerShell in Azure Stack Hub

Du kan skapa en Ubuntu-Server 16,04 LTS virtuell dator med hjälp av Azure Stack Hub PowerShell.You can create an Ubuntu Server 16.04 LTS virtual machine (VM) by using Azure Stack Hub PowerShell. I den här artikeln skapar du och använder en virtuell dator.In this article, you create and use a virtual machine. Den här artikeln visar också hur du:This article also shows you how to:

  • Anslut till den virtuella datorn med en fjärran sluten klient.Connect to the VM with a remote client.
  • Installera en NGINX webb server och Visa standard start sidan.Install an NGINX web server and view the default home page.
  • Rensa oanvända resurser.Clean up unused resources.

FörutsättningarPrerequisites

Skapa en resursgruppCreate a resource group

En resurs grupp är en logisk behållare där du kan distribuera och hantera Azure Stack Hub-resurser.A resource group is a logical container where you can deploy and manage Azure Stack Hub resources. Om du vill skapa en resurs grupp kör du följande kodblock:To create a resource group, run the following code block:

Anteckning

Vi har tilldelat värden för alla variabler i följande kod exempel.We've assigned values for all variables in the following code examples. Du kan dock tilldela dina egna värden.However, you can assign your own values.

# Create variables to store the location and resource group names.
$location = "local"
$ResourceGroupName = "myResourceGroup"

New-AzResourceGroup `
  -Name $ResourceGroupName `
  -Location $location

Skapa lagringsresurserCreate storage resources

Skapa ett lagrings konto som ska användas för att lagra utdata från startdiagnostik.Create a storage account that will be used for storing the boot diagnostics output.

# Create variables to store the storage account name and the storage account SKU information
$StorageAccountName = "mystorageaccount"
$SkuName = "Standard_LRS"

# Create a new storage account
$StorageAccount = New-AzStorageAccount `
  -Location $location `
  -ResourceGroupName $ResourceGroupName `
  -Type $SkuName `
  -Name $StorageAccountName

Set-AzCurrentStorageAccount `
  -StorageAccountName $storageAccountName `
  -ResourceGroupName $resourceGroupName

Skapa nätverksresurserCreate networking resources

Skapa ett virtuellt nätverk, ett undernät och en offentlig IP-adress.Create a virtual network, a subnet, and a public IP address. Dessa resurser används för att tillhandahålla nätverks anslutning till den virtuella datorn.These resources are used to provide network connectivity to the VM.

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name mySubnet `
  -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -Name MyVnet `
  -AddressPrefix 192.168.0.0/16 `
  -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -AllocationMethod Static `
  -IdleTimeoutInMinutes 4 `
  -Name "mypublicdns$(Get-Random)"

Skapa en nätverkssäkerhetsgrupp och en regel för nätverkssäkerhetsgruppCreate a network security group and a network security group rule

Nätverks säkerhets gruppen säkrar den virtuella datorn genom att använda inkommande och utgående regler.The network security group secures the VM by using inbound and outbound rules. Skapa en regel för inkommande trafik för port 3389 för att tillåta inkommande fjärr skrivbords anslutningar och en regel för inkommande trafik för port 80 för att tillåta inkommande webb trafik.Create an inbound rule for port 3389 to allow incoming Remote Desktop connections and an inbound rule for port 80 to allow incoming web traffic.

# Create variables to store the network security group and rules names.
$nsgName = "myNetworkSecurityGroup"
$nsgRuleSSHName = "myNetworkSecurityGroupRuleSSH"
$nsgRuleWebName = "myNetworkSecurityGroupRuleWeb"


# Create an inbound network security group rule for port 22
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name $nsgRuleSSHName -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 22 -Access Allow

# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzNetworkSecurityRuleConfig -Name $nsgRuleWebName -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow

# Create a network security group
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $location `
-Name $nsgName -SecurityRules $nsgRuleSSH,$nsgRuleWeb

Skapa ett nätverkskort för den virtuella datornCreate a network card for the VM

Nätverks kortet ansluter den virtuella datorn till ett undernät, en nätverks säkerhets grupp och en offentlig IP-adress.The network card connects the VM to a subnet, network security group, and public IP address.

# Create a virtual network card and associate it with public IP address and NSG
$nic = New-AzNetworkInterface `
  -Name myNic `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -SubnetId $vnet.Subnets[0].Id `
  -PublicIpAddressId $pip.Id `
  -NetworkSecurityGroupId $nsg.Id

Skapa en virtuell datorCreate a VM

Skapa en VM-konfiguration.Create a VM configuration. Den här konfigurationen inkluderar de inställningar som ska användas när du distribuerar den virtuella datorn (till exempel användarautentiseringsuppgifter, storlek och VM-avbildningen).This configuration includes the settings to use when you deploy the VM (for example, user credentials, size, and the VM image).

# Define a credential object
$UserName='demouser'
$securePassword = ConvertTo-SecureString ' ' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($UserName, $securePassword)

# Create the VM configuration object
$VmName = "VirtualMachinelatest"
$VmSize = "Standard_D1"
$VirtualMachine = New-AzVMConfig `
  -VMName $VmName `
  -VMSize $VmSize

$VirtualMachine = Set-AzVMOperatingSystem `
  -VM $VirtualMachine `
  -Linux `
  -ComputerName "MainComputer" `
  -Credential $cred

$VirtualMachine = Set-AzVMSourceImage `
  -VM $VirtualMachine `
  -PublisherName "Canonical" `
  -Offer "UbuntuServer" `
  -Skus "16.04-LTS" `
  -Version "latest"

# Set the operating system disk properties on a VM
$VirtualMachine = Set-AzVMOSDisk `
  -VM $VirtualMachine `
  -CreateOption FromImage | `
  Set-AzVMBootDiagnostics -ResourceGroupName $ResourceGroupName `
  -StorageAccountName $StorageAccountName -Enable |`
  Add-AzVMNetworkInterface -Id $nic.Id

# Configure SSH keys
$sshPublicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub"

# Add the SSH key to the VM
Add-AzVMSshPublicKey -VM $VirtualMachine `
 -KeyData $sshPublicKey `
 -Path "/home/azureuser/.ssh/authorized_keys"

# Create the VM
New-AzVM `
  -ResourceGroupName $ResourceGroupName `
 -Location $location `
  -VM $VirtualMachine

Snabb registrering för virtuell dator: fullständigt skriptVM Quick Create: Full script

Anteckning

Det här steget är i princip den föregående koden som slås samman, men med ett lösen ord i stället för en SSH-nyckel för autentisering.This step is essentially the preceding code merged together, but with a password rather than an SSH key for authentication.

## Create a resource group

<#
A resource group is a logical container where you can deploy and manage Azure Stack Hub resources. From your development kit or the Azure Stack Hub integrated system, run the following code block to create a resource group. Though we've assigned values for all the variables in this article, you can use these values or assign new ones.
#>

# Edit your variables, if required

# Create variables to store the location and resource group names
$location = "local"
$ResourceGroupName = "myResourceGroup"

# Create variables to store the storage account name and the storage account SKU information
$StorageAccountName = "mystorageaccount"
$SkuName = "Standard_LRS"

# Create variables to store the network security group and rules names
$nsgName = "myNetworkSecurityGroup"
$nsgRuleSSHName = "myNetworkSecurityGroupRuleSSH"
$nsgRuleWebName = "myNetworkSecurityGroupRuleWeb"

# Create variable for VM password
$VMPassword = 'Password123!'

# End of variables - no need to edit anything past that point to deploy a single VM

# Create a resource group
New-AzResourceGroup `
  -Name $ResourceGroupName `
  -Location $location

## Create storage resources

# Create a storage account, and then create a storage container for the Ubuntu Server 16.04 LTS image

# Create a new storage account
$StorageAccount = New-AzStorageAccount `
  -Location $location `
  -ResourceGroupName $ResourceGroupName `
  -Type $SkuName `
  -Name $StorageAccountName

Set-AzCurrentStorageAccount `
  -StorageAccountName $storageAccountName `
  -ResourceGroupName $resourceGroupName

# Create a storage container to store the VM image
$containerName = 'osdisks'
$container = New-AzureStorageContainer `
  -Name $containerName `
  -Permission Blob


## Create networking resources

# Create a virtual network, a subnet, and a public IP address, resources that are used provide network connectivity to the VM

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name mySubnet `
  -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -Name MyVnet `
  -AddressPrefix 192.168.0.0/16 `
  -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -AllocationMethod Static `
  -IdleTimeoutInMinutes 4 `
  -Name "mypublicdns$(Get-Random)"


### Create a network security group and a network security group rule

<#
The network security group secures the VM by using inbound and outbound rules. Create an inbound rule for port 3389 to allow incoming Remote Desktop connections and an inbound rule for port 80 to allow incoming web traffic.
#>

# Create an inbound network security group rule for port 22
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name $nsgRuleSSHName -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 22 -Access Allow

# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzNetworkSecurityRuleConfig -Name $nsgRuleWebName -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow

# Create a network security group
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $location `
-Name $nsgName -SecurityRules $nsgRuleSSH,$nsgRuleWeb

### Create a network card for the VM

# The network card connects the VM to a subnet, network security group, and public IP address.

# Create a virtual network card and associate it with public IP address and NSG
$nic = New-AzNetworkInterface `
  -Name myNic `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -SubnetId $vnet.Subnets[0].Id `
  -PublicIpAddressId $pip.Id `
  -NetworkSecurityGroupId $nsg.Id

## Create a VM
<#
Create a VM configuration. This configuration includes the settings used when deploying the VM. For example: user credentials, size, and the VM image.
#>

# Define a credential object
$UserName='demouser'
$securePassword = ConvertTo-SecureString $VMPassword -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($UserName, $securePassword)

# Create the VM configuration object
$VmName = "VirtualMachinelatest"
$VmSize = "Standard_D1"
$VirtualMachine = New-AzVMConfig `
  -VMName $VmName `
  -VMSize $VmSize

$VirtualMachine = Set-AzVMOperatingSystem `
  -VM $VirtualMachine `
  -Linux `
  -ComputerName "MainComputer" `
  -Credential $cred

$VirtualMachine = Set-AzVMSourceImage `
  -VM $VirtualMachine `
  -PublisherName "Canonical" `
  -Offer "UbuntuServer" `
  -Skus "16.04-LTS" `
  -Version "latest"

$osDiskName = "OsDisk"
$osDiskUri = '{0}vhds/{1}-{2}.vhd' -f `
  $StorageAccount.PrimaryEndpoints.Blob.ToString(),`
  $vmName.ToLower(), `
  $osDiskName

# Set the operating system disk properties on a VM
$VirtualMachine = Set-AzVMOSDisk `
  -VM $VirtualMachine `
  -Name $osDiskName `
  -VhdUri $OsDiskUri `
  -CreateOption FromImage | `
  Add-AzVMNetworkInterface -Id $nic.Id

# Create the VM
New-AzVM `
  -ResourceGroupName $ResourceGroupName `
 -Location $location `
  -VM $VirtualMachine

Anslut till VM:enConnect to the VM

När du har distribuerat den virtuella datorn konfigurerar du en SSH-anslutning för den.After you've deployed the VM, configure an SSH connection for it. Använd kommandot Get-AzPublicIpAddress för att hämta den offentliga IP-adressen för den virtuella datorn:To get the public IP address of the VM, use the Get-AzPublicIpAddress command:

Get-AzPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress

Använd följande kommando för att ansluta till den virtuella datorn från ett klient system med SSH installerat.From a client system with SSH installed, use the following command to connect to the VM. Om du arbetar med Windows kan du använda SparaTillFil för att skapa anslutningen.If you're working on Windows, you can use PuTTY to create the connection.

ssh <Public IP Address>

När du uppmanas till det loggar du in som azureuser.When you're prompted, sign in as azureuser. Om du använde en lösen fras när du skapade SSH-nycklarna måste du ange lösen frasen.If you used a passphrase when you created the SSH keys, you'll have to provide the passphrase.

Installera NGINX-webbservernInstall the NGINX web server

Kör följande skript för att uppdatera paket resurser och installera det senaste NGINX-paketet:To update package resources and install the latest NGINX package, run the following script:

#!/bin/bash

# update package source
apt-get -y update

# install NGINX
apt-get -y install nginx

Visa NGINX-välkomstsidanView the NGINX welcome page

Med NGINX-webbservern installerad och port 80 öppen på den virtuella datorn kan du komma åt webb servern genom att använda den virtuella datorns offentliga IP-adress.With the NGINX web server installed, and port 80 open on your VM, you can access the web server by using the VM's public IP address. Öppna en webbläsare och gå till http://<public IP address> .Open a web browser, and go to http://<public IP address>.

Välkomst sidan för NGINX-webbservern

Rensa resurserClean up resources

Du kan rensa de resurser som du inte längre behöver genom att använda kommandot Remove-AzResourceGroup .You can clean up the resources that you don't need any longer by using the Remove-AzResourceGroup command. Om du vill ta bort resurs gruppen och alla dess resurser kör du följande kommando:To delete the resource group and all its resources, run the following command:

Remove-AzResourceGroup -Name myResourceGroup

Nästa stegNext steps

I den här snabb starten har du distribuerat en enkel Linux server-VM.In this quickstart, you deployed a basic Linux server VM. Om du vill veta mer om virtuella datorer med Azure Stack hubb går du till överväganden för virtuella datorer i Azure Stack Hub.To learn more about Azure Stack Hub VMs, go to Considerations for VMs in Azure Stack Hub.