要解决Azure VM上新添加的网络接口在现有NIC正常工作时无法连接到互联网的问题,请执行以下步骤-
创建虚拟网络和子网
az network vnet create \
--resource-group arkorg \
--name myVNet \
--address-prefix 10.0.0.0/16 \
--subnet-name mySubnet \
--subnet-prefix 10.0.0.0/24
创建网络安全组(NSG)
az network nsg create \
--resource-group arkorg \
--name myNSG
将HTTP和SSH的入站规则添加到NSG
az network nsg rule create \
--resource-group arkorg \
--nsg-name myNSG \
--name AllowInternetInBound \
--priority 1000 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--destination-port-range 80 \
--source-address-prefix Internet \
--destination-address-prefix '*'
az network nsg rule create \
--resource-group arkorg \
--nsg-name myNSG \
--name AllowSSH \
--priority 1100 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--destination-port-range 22 \
--source-address-prefix Internet \
--destination-address-prefix '*'
为NIC创建公共IP地址
az network public-ip create \
--resource-group arkorg \
--name myExistingPublicIP \
--sku Basic \
--allocation-method Dynamic
az network public-ip create \
--resource-group arkorg \
--name myNewPublicIP \
--sku Basic \
--allocation-method Dynamic
创建网络接口并将其与公共IP相关联
az network nic create \
--resource-group arkorg \
--name myExistingNIC \
--vnet-name myVNet \
--subnet mySubnet \
--network-security-group myNSG \
--public-ip-address myExistingPublicIP
az network nic create \
--resource-group arkorg \
--name myNewNIC \
--vnet-name myVNet \
--subnet mySubnet \
--network-security-group myNSG \
--public-ip-address myNewPublicIP
使用现有NIC创建VM
az vm create \
--resource-group arkorg \
--name myVM \
--nics myExistingNIC \
--image Ubuntu2204 \
--admin-username azureuser \
--generate-ssh-keys
现在,您的主要问题是用新的IP更新VM,它应该能够连接到网络。
所以,首先释放旧的
az vm deallocate \
--resource-group arkorg \
--name myVM
然后添加新的NIC并重新启动VM
az vm nic add \
--resource-group arkorg \
--vm-name myVM \
--nics myNewNIC
az vm start \
--resource-group arkorg \
--name myVM
如果在这里完成,那么你就被解决了。现在,您只需使用现有NIC的公共IP通过SSH连接到VM
ssh azureuser@<existing-public-ip>
运行以下命令以设置基于源的路由:
sudo su
echo "200 eth0" >> /etc/iproute2/rt_tables
echo "201 eth1" >> /etc/iproute2/rt_tables
ip rule add from 10.0.0.4/32 table eth0
ip rule add from 10.0.0.5/32 table eth1
ip route add 10.0.0.0/24 dev eth0 src 10.0.0.4 table eth0
ip route add default via 10.0.0.1 dev eth0 table eth0
ip route add 10.0.0.0/24 dev eth1 src 10.0.0.5 table eth1
ip route add default via 10.0.0.1 dev eth1 table eth1
验证路由规则
ip rule show
ip route show table eth0
ip route show table eth1
测试连接性
curl --interface 10.0.0.4 http://example.com
curl --interface 10.0.0.5 http://example.com