Create a virtual machine with a static private IP address
บทความ
When you create a virtual machine (VM), it's automatically assigned a private IP address from a range that you specify. This IP address is based on the subnet in which the VM is deployed, and the VM keeps this address until the VM is deleted. Azure dynamically assigns the next available private IP address from the subnet you create a VM in. If you want to assign a specific IP address in this subnet for your VM, use a static IP address.
Azure PowerShell installed locally or Azure Cloud Shell.
If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 5.4.1 or later.
Run Get-Module -ListAvailable Az to find the installed version.
If you need to upgrade, see Install Azure PowerShell module. If you're running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
In the portal, search for and select Virtual machines.
Select Create > Azure virtual machine.
On the Basics tab of the Create a virtual machine screen, enter or select the following values:
Setting
Value
Subscription
Keep the default or select a different subscription
Resource group
Select Create new, and then name the group myResourceGroup
Virtual machine name
Enter myVM
Region
Select (US) East US
Availability options
Select No infrastructure redundancy required
Image
Select Windows Server 2019 Datacenter - x64 Gen2
Size
Accept the default, or drop down and select a size
Username
Enter an admin username for the VM
Password
Enter a password for the VM
Confirm password
Confirm the password for the VM
Public inbound ports
Select Allow selected ports
Select inbound ports
Select RDP (3389)
Warning
In this example, you open port 3389 to enable remote access to the Windows Server VM from the internet. However, opening port 3389 to the internet is not recommended to manage production workloads. For information about secure access to Azure VMs, see What is Azure Bastion?.
Select the Networking tab at the top of the page.
On the Networking page, enter or select the following values:
Virtual network: Accept the default network name.
Subnet: Select default if not already selected.
Public IP: Accept the default public IP configuration.
Public inbound ports: Select Allow selected ports.
Select inbound ports: Select RDP (3389).
Select Review + create. Review the settings, and then select Create.
Note
Azure provides a default outbound access IP for VMs that either aren't assigned a public IP address or are in the backend pool of an internal basic Azure load balancer. The default outbound access IP mechanism provides an outbound IP address that isn't configurable.
The default outbound access IP is disabled when one of the following events happens:
A public IP address is assigned to the VM.
The VM is placed in the backend pool of a standard load balancer, with or without outbound rules.
The following command creates a Windows Server virtual machine with New-AzVM. When prompted, provide a username and password to be used as the credentials for the virtual machine:
Use the following steps to create a resource group and a virtual machine.
Create a resource group
The following command creates a resource group with az group create:
az group create --name myResourceGroup --location eastus2
Create a virtual machine
The following command creates a Windows Server virtual machine with az vm create. When prompted, provide a username and password to be used as the credentials for the virtual machine:
az vm create \
--name myVM \
--resource-group myResourceGroup \
--public-ip-address myPublicIP \
--public-ip-sku Standard \
--image MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest \
--admin-username azureuser
In the following steps, you change the private IP address static for the VM created previously:
In the portal, search for and select Virtual machines.
In Virtual machines, select myVM from the list.
On the myVM page, under Settings, select Networking.
In Networking, select the name of the network interface next to Network interface.
On the Network interface page, under Settings, select IP configurations.
In IP configurations, select ipconfig1 in the list.
Under Assignment, select Static. Change the private IP address if you want a different one, and then select Save.
Warning
If you change the private IP address, the VM associated with the network interface will be restarted to utilize the new IP address.
Warning
From within the operating system of a VM, avoid associating a static private IP address on an Azure VM. Only assign a static private IP when it's necessary, such as when assigning many IP addresses to VMs.
If you manually set the private IP address within the operating system, make sure it matches the private IP address assigned to the Azure network interface. Otherwise, you can lose connectivity to the VM. For more information, see private IP address settings.
Azure PowerShell cmdlets used to change the private IP address to static are as follows:
Command
Description
Get-AzVirtualNetwork
Use Get-AzVirtualNetwork to place the virtual network configuration into a variable.
With the following commands, you change the private IP address of the virtual machine to static:
## Place virtual network configuration into a variable. ##
$net = @{
Name = 'myVM'
ResourceGroupName = 'myResourceGroup'
}
$vnet = Get-AzVirtualNetwork @net
## Place subnet configuration into a variable. ##
$sub = @{
Name = 'myVM'
VirtualNetwork = $vnet
}
$subnet = Get-AzVirtualNetworkSubnetConfig @sub
## Get name of network interface and place into a variable ##
$int1 = @{
Name = 'myVM'
ResourceGroupName = 'myResourceGroup'
}
$vm = Get-AzVM @int1
## Place network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces.Id
## Set interface configuration. ##
$config =@{
Name = 'myVM'
PrivateIpAddress = '192.168.1.4'
Subnet = $subnet
}
$nic | Set-AzNetworkInterfaceIpConfig @config -Primary
## Save interface configuration. ##
$nic | Set-AzNetworkInterface
Warning
From within the operating system of a VM, you shouldn't statically assign the private IP that's assigned to the Azure VM. Only do static assignment of a private IP when it's necessary, such as when assigning many IP addresses to VMs.
If you manually set the private IP address within the operating system, make sure it matches the private IP address assigned to the Azure network interface. Otherwise, you can lose connectivity to the VM. Learn more about private IP address settings.
With the following commands, you change the private IP address of the virtual machine to static:
az network nic ip-config update \
--name ipconfigmyVM \
--resource-group myResourceGroup \
--nic-name myVMVMNic \
--private-ip-address 10.0.0.4
Warning
From within the operating system of a VM, you shouldn't statically assign the private IP that's assigned to the Azure VM. Only do static assignment of a private IP when it's necessary, such as when assigning many IP addresses to VMs.
If you manually set the private IP address within the operating system, make sure it matches the private IP address assigned to the Azure network interface. Otherwise, you can lose connectivity to the VM. Learn more about private IP address settings.