Use VM Public IP for Outbound Traffic Instead of NAT Gateway IP

Clive Gerada 1 Reputation point
2022-11-09T10:58:20.723+00:00

We have a VM which is in a Subnet associated with a NAT gateway. We have assigned a Public IP to this VM and we can reach it from outside through this public IP. The problem is we need it to use this Public IP also for outbound traffic but it is still using the NAT Gateway public IP.

We have tried restarting the VM. We also don't have any UDR.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,375 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Alan Kinane 16,796 Reputation points MVP
    2022-11-09T12:33:07.423+00:00

    The NAT Gateway will take priority here for outbound traffic. See below documentation that confirms.

    I would recommend to move this VM to another subnet on your virtual network and do not associate that subnet with your NAT Gateway. This will route outbound through the IP address.

    https://learn.microsoft.com/en-us/azure/virtual-network/nat-gateway/nat-gateway-resource#nat-and-vm-with-an-instance-level-public-ip

    258727-image.png

    0 comments No comments

  2. Clive Gerada 1 Reputation point
    2022-11-09T14:08:54.83+00:00

    Thanks for the reply. The issue here is the VM was already being used and we already have many other services pointing to it via the private IP. Is there no way to change this behaviour just for this VM and have it retain the same Private IP via Route tables maybe or something?

    Or would it be possible to try adding another interface on another subnet with the Public IP and make that default?

    0 comments No comments

  3. Alan Kinane 16,796 Reputation points MVP
    2022-11-09T14:20:56.19+00:00

    NAT Gateway always takes precedence so you would need to use another subnet. If your VM supports multiple NICs then yes, you should be able to put add a NIC on another subnet and move the Public IP address there and route outbound through that NIC but note you would want to make this the primary NIC on the VM as otherwise you need to configure outbound routes in your OS.

    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/multiple-nics#configure-guest-os-for-multiple-nics

    0 comments No comments