你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在一个 Azure 虚拟机 (VM) 上可以附加一个或多个网络接口 (NIC)。 可为任何 NIC 分配一个或多个静态或动态的公共与专用 IP 地址。
为 VM 分配多个 IP 地址可实现以下功能:
在一台服务器上托管具有不同 IP 地址和 TLS/SSL 证书的多个网站或服务。
用作网络虚拟设备,例如防火墙或负载均衡器。
可将任何 NIC 的任何专用 IP 地址添加到 Azure 负载均衡器后端池。 以往,只能将主要 NIC 的主要 IP 地址添加到后端池。 有关负载均衡多个 IP 配置的详细信息,请参阅负载均衡多个 IP 配置。
附加到 VM 的每个 NIC 都具有一个或多个关联的 IP 配置。 每个配置分配有一个静态或动态专用 IP 地址。 每个配置还可以具有一个关联的公共 IP 地址资源。 若要详细了解 Azure 中的 IP 地址,请参阅 Azure 中的 IP 地址。
Note
单个 NIC 上的所有 IP 配置都必须关联到同一子网。 如果需要不同子网上的多个 IP,可以使用 VM 上的多个 NIC。 若要详细了解 Azure 中 VM 上的多个 NIC,请阅读创建具有多个 NIC 的 VM。
分配给 NIC 的专用 IP 地址数目存在限制。 能够在 Azure 订阅中使用的公共 IP 地址数也存在限制。 See the Azure limits article for details.
本文介绍如何使用 PowerShell 将多个 IP 地址添加到虚拟机。
Prerequisites
具有活动订阅的 Azure 帐户。 免费创建帐户。
Azure Cloud Shell 中的环境或本地安装的 Azure PowerShell。 若要详细了解如何在 Azure Cloud Shell 中使用 PowerShell,请参阅 Azure Cloud Shell 快速入门。
- 如果选择在本地安装并使用 PowerShell,则本文需要 Azure PowerShell 模块 5.4.1 或更高版本。 运行
Get-InstalledModule -Name Az
查找已安装的版本。 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。 确保 Az.Network 模块为 4.3.0 或更高版本。 若要验证已安装的模块,请使用命令Get-InstalledModule -Name "Az.Network"
。 如果模块需要更新,必要时请使用命令Update-Module -Name "Az.Network"
。
- 如果选择在本地安装并使用 PowerShell,则本文需要 Azure PowerShell 模块 5.4.1 或更高版本。 运行
登录到 Azure PowerShell,并确保已选择要使用此功能的订阅。 有关详细信息,请参阅使用 Azure PowerShell 登录。
Note
尽管本文是将所有 IP 配置分配到单个 NIC,但也可以将多个 IP 配置分配到具有多个 NIC 的 VM 中的任意 NIC。 若要了解如何创建具有多个 NIC 的 VM,请参阅创建具有多个 NIC 的 VM。
图:此操作说明文章中创建的网络配置资源的示意图。
创建资源组
Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。
Create a resource group with New-AzResourceGroup named myResourceGroup in the eastus2 location.
$rg =@{
Name = 'myResourceGroup'
Location = 'eastus2'
}
New-AzResourceGroup @rg
创建虚拟网络
在本部分中,为虚拟机创建一个虚拟网络。
Use New-AzVirtualNetwork and New-AzVirtualNetworkSubnetConfig to create a virtual network with one subnet.
## Create backend subnet config ##
$subnet = @{
Name = 'myBackendSubnet'
AddressPrefix = '10.1.0.0/24'
}
$subnetConfig = New-AzVirtualNetworkSubnetConfig @subnet
## Create the virtual network ##
$vnet = @{
Name = 'myVNet'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
AddressPrefix = '10.1.0.0/16'
Subnet = $subnetConfig
}
New-AzVirtualNetwork @vnet
创建主公共 IP 地址
Use New-AzPublicIpAddress to create a primary public IP address.
$ip1 = @{
Name = 'myPublicIP-1'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
Sku = 'Standard'
AllocationMethod = 'Static'
IpAddressVersion = 'IPv4'
Zone = 1,2,3
}
New-AzPublicIpAddress @ip1
创建网络安全组
在本部分中,为虚拟机和虚拟网络创建网络安全组。 创建一个规则,以允许连接到适用于 SSH 的端口 22 上的虚拟机。
Use New-AzNetworkSecurityGroup and New-AzNetworkSecurityRuleConfig to create the network security group and rules.
## Create rule for network security group and place in variable. ##
$nsgrule1 = @{
Name = 'myNSGRuleSSH'
Description = 'Allow SSH'
Protocol = '*'
SourcePortRange = '*'
DestinationPortRange = '22'
SourceAddressPrefix = 'Internet'
DestinationAddressPrefix = '*'
Access = 'Allow'
Priority = '200'
Direction = 'Inbound'
}
$rule1 = New-AzNetworkSecurityRuleConfig @nsgrule1
## Create network security group ##
$nsg = @{
Name = 'myNSG'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
SecurityRules = $rule1
}
New-AzNetworkSecurityGroup @nsg
创建网络接口
Use New-AzNetworkInterface and New-AzNetworkInterfaceIpConfig to create a network interface (NIC) for the virtual machine. 之前创建的公共 IP 地址和网络安全组将与网络接口相关联。 网络接口已连接到之前创建的虚拟网络。
## Place the virtual network into a variable. ##
$net = @{
Name = 'myVNet'
ResourceGroupName = 'myResourceGroup'
}
$vnet = Get-AzVirtualNetwork @net
## Place the network security group into a variable. ##
$ns = @{
Name = 'myNSG'
ResourceGroupName = 'myResourceGroup'
}
$nsg = Get-AzNetworkSecurityGroup @ns
## Place the primary public IP address into a variable. ##
$pub1 = @{
Name = 'myPublicIP-1'
ResourceGroupName = 'myResourceGroup'
}
$pubIP1 = Get-AzPublicIPAddress @pub1
## Create a primary IP configuration for the network interface. ##
$IP1 = @{
Name = 'ipconfig1'
Subnet = $vnet.Subnets[0]
PrivateIpAddressVersion = 'IPv4'
PublicIPAddress = $pubIP1
}
$IP1Config = New-AzNetworkInterfaceIpConfig @IP1 -Primary
## Create a secondary IP configuration for the network interface. ##
$IP3 = @{
Name = 'ipconfig3'
Subnet = $vnet.Subnets[0]
PrivateIpAddressVersion = 'IPv4'
PrivateIpAddress = '10.1.0.6'
}
$IP3Config = New-AzNetworkInterfaceIpConfig @IP3
## Command to create a network interface. ##
$nic = @{
Name = 'myNIC1'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
NetworkSecurityGroup = $nsg
IpConfiguration = $IP1Config,$IP3Config
}
New-AzNetworkInterface @nic
Note
在添加静态 IP 地址时,必须在 NIC 连接到的子网中指定未使用的有效地址。
创建虚拟机
使用以下命令创建虚拟机:
$cred = Get-Credential
## Place network interface into a variable. ##
$nic = @{
Name = 'myNIC1'
ResourceGroupName = 'myResourceGroup'
}
$nicVM = Get-AzNetworkInterface @nic
## Create a virtual machine configuration for VMs ##
$vmsz = @{
VMName = 'myVM'
VMSize = 'Standard_DS1_v2'
}
$vmos = @{
ComputerName = 'myVM'
Credential = $cred
}
$vmimage = @{
PublisherName = 'Debian'
Offer = 'debian-11'
Skus = '11'
Version = 'latest'
}
$vmConfig = New-AzVMConfig @vmsz `
| Set-AzVMOperatingSystem @vmos -Linux `
| Set-AzVMSourceImage @vmimage `
| Add-AzVMNetworkInterface -Id $nicVM.Id
## Create the virtual machine for VMs ##
$vm = @{
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
VM = $vmConfig
SshKeyName = 'mySSHKey'
}
New-AzVM @vm -GenerateSshKey
添加辅助专用和公共 IP 地址
Use New-AzPublicIpAddress to create a secondary public IP address.
$ip2 = @{
Name = 'myPublicIP-2'
ResourceGroupName = 'myResourceGroup'
Location = 'eastus2'
Sku = 'Standard'
AllocationMethod = 'Static'
IpAddressVersion = 'IPv4'
Zone = 1,2,3
}
New-AzPublicIpAddress @ip2
Use New-AzNetworkInterfaceIpConfig to create the secondary IP configuration for the virtual machine.
## Place the virtual network into a variable. ##
$net = @{
Name = 'myVNet'
ResourceGroupName = 'myResourceGroup'
}
$vnet = Get-AzVirtualNetwork @net
## Place your virtual network subnet into a variable. ##
$sub = @{
Name = 'myBackendSubnet'
VirtualNetwork = $vnet
}
$subnet = Get-AzVirtualNetworkSubnetConfig @sub
## Place the secondary public IP address you created previously into a variable. ##
$pip = @{
Name = 'myPublicIP-2'
ResourceGroupName = 'myResourceGroup'
}
$pubIP2 = Get-AzPublicIPAddress @pip
## Place the network interface into a variable. ##
$net = @{
Name = 'myNIC1'
ResourceGroupName = 'myResourceGroup'
}
$nic = Get-AzNetworkInterface @net
## Create a secondary IP configuration for the network interface. ##
$IPc2 = @{
Name = 'ipconfig2'
Subnet = $vnet.Subnets[0]
PrivateIpAddressVersion = 'IPv4'
PrivateIpAddress = '10.1.0.5'
PublicIPAddress = $pubIP2
}
$IP2Config = New-AzNetworkInterfaceIpConfig @IPc2
## Add the IP configuration to the network interface. ##
$nic.IpConfigurations.Add($IP2Config)
## Save the configuration to the network interface. ##
$nic | Set-AzNetworkInterface
将 IP 地址添加到 VM 操作系统
连接并登录到使用多个专用 IP 地址创建的 VM。 必须手动添加 VM 中的所有专用 IP 地址,包括主要地址。 根据 VM 操作系统完成以下步骤。
Windows Server
Expand
打开命令提示符或 PowerShell。
在命令行上输入
ipconfig /all
。 You see the Primary private IP address that was assigned through DHCP.Enter
ncpa.cpl
at the command line to open the Network Connections configuration.Open the Properties for the network adapter assigned the new IP addresses.
双击“Internet 协议版本 4 (TCP/IPv4)”。
选择“使用下面的 IP 地址:”。 输入以下值。
Setting Value IP address: Enter the Primary private IP address. Subnet mask: 根据 IP 地址输入子网掩码。
For example, if the subnet is a /24 subnet then the subnet mask is 255.255.255.0.Default gateway: 子网中的第一个 IP 地址。
If your subnet is 10.0.0.0/24, then the gateway IP address is 10.0.0.1.选择“使用下面的 DNS 服务器地址:”。 输入以下值。
Setting Value 首选 DNS 服务器: 输入主 DNS 服务器。
Enter the IP address of 168.63.129.16 to use the default Azure provided DNS.Select the Advanced button.
Select Add.
Enter the private IP address you added to the Azure network interface. Enter the corresponding Subnet mask. Select Add.
重复上述步骤,添加添加到 Azure 网络接口的任何更多专用 IP 地址。
Important
切勿在虚拟机的操作系统中手动分配已分配给 Azure 虚拟机的公共 IP 地址。 在操作系统中手动设置该 IP 地址时,请确保它与分配给 Azure 网络接口的专用 IP 地址是同一地址。 未能正确分配地址可能会导致与虚拟机的连接丢失。 有关详细信息,请参阅更改 IP 地址设置。
有关专用 IP 地址的详细信息,请参阅专用 IP 地址。
Select OK to close the secondary IP address settings.
Select OK to close the adapter settings. RDP 连接重新建立。
打开命令提示符或 PowerShell。
在命令行上输入
ipconfig /all
。验证配置中是否存在主要和辅助专用 IP 地址。
PS C:\Users\azureuser> ipconfig /all Windows IP Configuration Host Name . . . . . . . . . . . . : myVM Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Hybrid IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No Ethernet adapter Ethernet: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter Physical Address. . . . . . . . . : 00-0D-3A-E6-CE-A3 DHCP Enabled. . . . . . . . . . . : No Autoconfiguration Enabled . . . . : Yes Link-local IPv6 Address . . . . . : fe80::a8d1:11d5:3ab2:6a51%5(Preferred) IPv4 Address. . . . . . . . . . . : 10.1.0.4(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 IPv4 Address. . . . . . . . . . . : 10.1.0.5(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 IPv4 Address. . . . . . . . . . . : 10.1.0.6(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 10.1.0.1 DHCPv6 IAID . . . . . . . . . . . : 100666682 DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-2A-A8-26-B1-00-0D-3A-E6-CE-A3 DNS Servers . . . . . . . . . . . : 168.63.129.16 NetBIOS over Tcpip. . . . . . . . : Enabled
确保 Windows 中使用的主专用 IP 地址与 Azure VM 网络接口的主 IP 地址相同。 有关详细信息,请参阅无法通过具有多个 IP 地址的 Azure Windows VM 访问 Internet。
验证 (Windows Server)
若要验证通过公共 IP 从辅助 IP 配置到 internet 的连接,请使用以下命令。 将 10.1.0.5 替换为添加到 Azure VM 网络接口的辅助专用 IP 地址。
ping -S 10.1.0.5 outlook.com
Note
对于辅助 IP 配置,如果配置存在关联的公共 IP 地址,则可以 ping Internet。 对于主 IP 配置,不需公共 IP 地址也可 ping Internet。
SUSE Linux Enterprise 和 openSUSE
Expand
基于 SUSE 的分发版使用cloud-netconfig
包中的 cloud-netconfig-azure
插件来管理 IP 配置。 管理员无需手动步骤。 平台上设置的接口的第一个 IP 地址是通过 DHCP 分配的。 然后,cloud-netconfig 插件会持续(每分钟一次)探测 Azure 实例元数据服务 API,以获取分配给接口的更多 IP 地址,并自动将其添加/删除为辅助 IP 地址。
默认情况下,应在新映像上安装并启用此插件。 可在此处找到旧工作负载的配置步骤:https://www.suse.com/c/multi-nic-cloud-netconfig-ec2-azure/。
Ubuntu 14/16
Expand
我们建议你查看 Linux 分发版的最新文档。
打开终端窗口。
请确保以根用户身份操作。 否则,请输入以下命令:
sudo -i
Update the configuration file of the network interface (assuming ‘eth0’).
保留 dhcp 的现有行项。 主要 IP 地址将保留以前的配置。
使用以下命令为其他静态 IP 地址添加配置:
cd /etc/network/interfaces.d/ ls
应会看到一个 .cfg 文件。
打开 文件。 该文件的末尾应会显示以下命令行:
auto eth0 iface eth0 inet dhcp
在此文件包含的命令行后面添加以下命令行。 将
10.1.0.5
替换为专用 IP 地址和子网掩码。iface eth0 inet static address 10.1.0.5 netmask 255.255.255.0
若要添加其他专用 IP 地址,请编辑该文件,并在后续行中添加新的专用 IP 地址:
iface eth0 inet static address 10.1.0.5 netmask 255.255.255.0 iface eth0 inet static address 10.1.0.6 netmask 255.255.255.0
使用以下命令保存该文件:
:wq
使用以下命令重置网络接口:
ifdown eth0 && ifup eth0
Important
如果使用远程连接,请在同一行中同时执行 ifdown 和 ifup。
使用以下命令验证 IP 地址是否已添加到网络接口:
ip addr list eth0
应会在列表中看到添加的 IP 地址。 Example:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0d:3a:04:45:16 brd ff:ff:ff:ff:ff:ff inet 10.1.0.5/24 brd 10.1.0.255 scope global eth0 valid_lft forever preferred_lft forever inet 10.1.0.6/24 brd 10.1.0.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet 10.1.0.4/24 brd 10.1.0.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::20d:3aff:fe04:4516/64 scope link valid_lft forever preferred_lft forever
验证 (Ubuntu 14/16)
若要确保能够从辅助 IP 配置通过与之关联的公共 IP 连接到 Internet,请使用以下命令:
ping -I 10.1.0.5 outlook.com
Note
对于辅助 IP 配置,仅当该配置存在关联的公共 IP 地址的情况下,才能 ping Internet。 对于主 IP 配置,不需公共 IP 地址也可 ping Internet。
对于 Linux VM,在尝试验证来自辅助 NIC 的出站连接时,可能需要添加适当的路由。 请参阅针对 Linux 分发的相应文档。 实现此目标的以下方法:
echo 150 custom >> /etc/iproute2/rt_tables
ip rule add from 10.1.0.5 lookup custom
ip route add default via 10.1.0.1 dev eth2 table custom
请务必将:
10.1.0.5 with the private IP address that has a public IP address associated to it
10.1.0.1 to your default gateway
eth2 to the name of your secondary NIC
Ubuntu 18.04+
Expand
从 18.04 开始, netplan
Ubuntu 中用于网络管理。 我们建议你查看 Linux 分发版的最新文档。
打开终端窗口。
请确保以根用户身份操作。 否则,请输入以下命令:
sudo -i
为第二个接口创建一个文件,并在文本编辑器打开该文件:
vi /etc/netplan/60-static.yaml
将以下行添加到文件中,并将
10.1.0.5/24
替换为 IP 和子网掩码:network: version: 2 ethernets: eth0: addresses: - 10.1.0.5/24
若要添加专用 IP 地址,请编辑该文件,并在后续行中添加新的专用 IP 地址:
network: version: 2 ethernets: eth0: addresses: - 10.1.0.5/24 - 10.1.0.6/24
使用以下命令保存该文件:
:wq
Test the changes with netplan try to confirm syntax:
netplan try
Note
netplan try
将暂时应用更改,并在 120 秒后回滚更改。 如果连接丢失,请等待 2 分钟,然后重新连接。 此时更改已回退。假设
netplan try
没有问题,请应用配置更改:netplan apply
使用以下命令验证 IP 地址是否已添加到网络接口:
ip addr list eth0
应会在列表中看到添加的 IP 地址。 Example:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0d:3a:04:45:16 brd ff:ff:ff:ff:ff:ff inet 10.1.0.5/24 brd 10.1.0.255 scope global eth0 valid_lft forever preferred_lft forever inet 10.1.0.6/24 brd 10.1.0.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet 10.1.0.4/24 brd 10.1.0.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::20d:3aff:fe04:4516/64 scope link valid_lft forever preferred_lft forever
验证 (Ubuntu 18.04+)
若要确保能够从辅助 IP 配置通过与之关联的公共 IP 连接到 Internet,请使用以下命令:
ping -I 10.1.0.5 outlook.com
Note
对于辅助 IP 配置,仅当该配置存在关联的公共 IP 地址的情况下,才能 ping Internet。 对于主 IP 配置,不需公共 IP 地址也可 ping Internet。
对于 Linux VM,在尝试验证来自辅助 NIC 的出站连接时,可能需要添加适当的路由。 按照适用于 Linux 分发版的相应文档进行作。 以下方法是实现此目标的一种方法:
echo 150 custom >> /etc/iproute2/rt_tables
ip rule add from 10.1.0.5 lookup custom
ip route add default via 10.1.0.1 dev eth2 table custom
请确保将:
10.1.0.5 with the private IP address that has a public IP address associated to it
10.1.0.1 to your default gateway
eth2 to the name of your secondary NIC
Red Hat Enterprise Linux 及其他
Expand
Note
若要在 RHEL10.x 中配置额外的 IP 地址,它足以重启 NetworkManger, systemctl restart NetworkManger.service
或者重新启动系统。 无需执行其他步骤。
打开终端窗口。
请确保以根用户身份操作。 否则,请输入以下命令:
sudo -i
输入密码,根据提示的说明操作。 成为 root 用户后,使用以下命令转到网络脚本文件夹:
cd /etc/sysconfig/network-scripts
使用以下命令列出相关的 ifcfg 文件:
ls ifcfg-*
You should see ifcfg-eth0 as one of the files.
为添加到系统的每个 IP 创建新的配置文件。
touch ifcfg-eth0:0
Open the ifcfg-eth0:0 file with the following command:
vi ifcfg-eth0:0
Add content to the file, eth0:0 in this case, with the following command. 替换为
10.1.0.5
新的专用 IP 地址和子网掩码。DEVICE=eth0:0 BOOTPROTO=static ONBOOT=yes IPADDR=10.1.0.5 NETMASK=255.255.255.0
使用以下命令保存该文件:
:wq
为每个 IP 地址创建配置文件,以添加相应的值:
touch ifcfg-eth0:1
vi ifcfg-eth0:1
DEVICE=eth0:1 BOOTPROTO=static ONBOOT=yes IPADDR=10.1.0.6 NETMASK=255.255.255.0
:wq
运行以下命令重新启动网络服务,确保更改成功:
systemctl restart NetworkManager.service ifconfig
应会在返回的列表中看到添加的 IP 地址。
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.1.0.4 netmask 255.255.255.0 broadcast 10.1.0.255 inet6 fe80::6245:bdff:fe7d:704a prefixlen 64 scopeid 0x20<link> ether 60:45:bd:7d:70:4a txqueuelen 1000 (Ethernet) RX packets 858 bytes 244215 (238.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1021 bytes 262077 (255.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.1.0.5 netmask 255.255.255.0 broadcast 10.1.0.255 ether 60:45:bd:7d:70:4a txqueuelen 1000 (Ethernet) eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.1.0.6 netmask 255.255.255.0 broadcast 10.1.0.255 ether 60:45:bd:7d:70:4a txqueuelen 1000 (Ethernet)
验证(Red Hat 和其他)
若要确保能够从辅助 IP 配置通过与之关联的公共 IP 连接到 Internet,请使用以下命令:
ping -I 10.0.0.5 outlook.com
Note
对于辅助 IP 配置,仅当该配置存在关联的公共 IP 地址的情况下,才能 ping Internet。 对于主 IP 配置,不需公共 IP 地址也可 ping Internet。
对于 Linux VM,在尝试验证来自辅助 NIC 的出站连接时,可能需要添加适当的路由。 请参阅适用于 Linux 分发版的相应文档。 实现此目标的以下方法:
echo 150 custom >> /etc/iproute2/rt_tables
ip rule add from 10.1.0.5 lookup custom
ip route add default via 10.1.0.1 dev eth2 table custom
请务必将:
10.0.0.5 with the private IP address that has a public IP address associated to it
10.0.0.1 to your default gateway
eth2 to the name of your secondary NIC
Debian GNU/Linux
Expand
我们建议你查看 Linux 分发版的最新文档。
打开终端窗口。
请确保以根用户身份操作。 否则,请输入以下命令:
sudo -i
Update the configuration file of the network interface (assuming ‘eth0’).
保留 dhcp 的现有行项。 主要 IP 地址将保留以前的配置。
使用以下命令为每个静态 IP 地址添加配置:
cd /etc/network/interfaces.d/ ls
应会看到一个 .cfg 文件。
打开 文件。 该文件的末尾应会显示以下命令行:
auto eth0 iface eth0 inet dhcp
在此文件包含的命令行后面添加以下命令行。 将
10.1.0.5
替换为专用 IP 地址和子网掩码。iface eth0 inet static address 10.1.0.5 netmask 255.255.255.0
在配置文件中添加新的 IP 地址信息:
iface eth0 inet static address 10.1.0.5 netmask 255.255.255.0 iface eth0 inet static address 10.1.0.6 netmask 255.255.255.0
使用以下命令保存该文件:
:wq
重启网络服务以使更改生效。 对于 Debian 8 及更高版本,请使用:
systemctl restart networking
对于早期版本的 Debian,可以使用以下命令:
service networking restart
使用以下命令验证 IP 地址是否已添加到网络接口:
ip addr list eth0
应会在列表中看到添加的 IP 地址。 Example:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0d:3a:04:45:16 brd ff:ff:ff:ff:ff:ff inet 10.1.0.5/24 brd 10.1.0.255 scope global eth0 valid_lft forever preferred_lft forever inet 10.1.0.6/24 brd 10.1.0.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet 10.1.0.4/24 brd 10.1.0.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::20d:3aff:fe04:4516/64 scope link valid_lft forever preferred_lft forever
验证 (Debian GNU/Linux)
若要确保能够从辅助 IP 配置通过与之关联的公共 IP 连接到 Internet,请使用以下命令:
ping -I 10.1.0.5 outlook.com
Note
对于辅助 IP 配置,仅当该配置存在关联的公共 IP 地址的情况下,才能 ping Internet。 对于主 IP 配置,不需公共 IP 地址也可 ping Internet。
对于 Linux VM,在尝试验证来自辅助 NIC 的出站连接时,可能需要添加适当的路由。 请参阅针对 Linux 分发的相应文档。 实现此目标的以下方法:
echo 150 custom >> /etc/iproute2/rt_tables
ip rule add from 10.1.0.5 lookup custom
ip route add default via 10.1.0.1 dev eth2 table custom
请务必将:
10.1.0.5 with the private IP address that has a public IP address associated to it
10.1.0.1 to your default gateway
eth2 to the name of your secondary NIC
Next steps
- 详细了解 Azure 中的公共 IP 地址。
- 详细了解 Azure 中的专用 IP 地址。
- 了解如何为 Azure 网络接口配置 IP 地址。